Skip to content

Reorder Vec fields #18302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cgaebel opened this issue Oct 25, 2014 · 1 comment · Fixed by #18303
Closed

Reorder Vec fields #18302

cgaebel opened this issue Oct 25, 2014 · 1 comment · Fixed by #18303

Comments

@cgaebel
Copy link
Contributor

cgaebel commented Oct 25, 2014

Currently, raw::Slice is implemented implemented as len, data, and Vec is implemented as len, cap, data. This means that the following code requires a branch, which is extremely unfortunate for my use-case:

enum MaybeOwnedBuffer<'a> {
  OwnedBuffer(Vec<u8>),
  BorrowedBuffer(raw::Slice<u8>, ContravariantLifetime<'a>),
}

impl<'a> MaybeOwnedBuffer<'a> {
  #[inline(always)]
  unsafe fn as_raw_slice(&self) -> raw::Slice<u8> {
    match *self {
      OwnedBuffer(ref v)       => mem::transmute(v.as_slice()),
      BorrowedBuffer(ref s, _) => *s,
    }
  }
}

Ideally, the order of Vec's fields should be changed to len, data, cap to make conversion free.

Thoughts?

@thestinger
Copy link
Contributor

I think you made a minor typo, as slices are currently (ptr, len). I'll r+ a pull request switching Vec<T> to (ptr, len, cap) instead of (len, cap, ptr).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants