Skip to content

Reorder Vec fields #18302

Closed
Closed
@cgaebel

Description

@cgaebel

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions