Buf

Trait Buf 

Source
pub unsafe trait Buf {
    type Element: AsBytes + Copy;

    // Provided methods
    fn elem_len(&self) -> usize { ... }
    fn byte_len(&self) -> usize { ... }
    fn empty<'a, B: ?Sized + Buf>() -> &'a B { ... }
    fn as_bytes(&self) -> &[u8] { ... }
    unsafe fn from_bytes(bytes: &[u8]) -> &Self { ... }
    fn as_bytes_mut(&mut self) -> &mut [u8] { ... }
    unsafe fn from_bytes_mut(bytes: &mut [u8]) -> &mut Self { ... }
    unsafe fn slice_along_bytes<Idx>(&self, index: Idx) -> Option<&Self>
       where Idx: SliceIndex<[u8], Output = [u8]> { ... }
}
Expand description

A trait for abstracting over str, [u8], and other byte-string-like types.

See the crate docs for more information.

§Safety

This trait should only be implemented on types that are, essentially, a repr(transpartent) wrapper over a [T] for some Copy type T.

In particular, B: Buf the requires that the following must hold:

  1. Transmute &B to &[T], where T is zerocopy::AsBytes. Transmute here is quite literal: mem::transmute<&B, &[T]> MUST be a valid way to convert between them.

  2. Transmute &[T] to &B if the contents of that &[T] originated from operation (1).

  3. Byte-copy &B to a T-aligned buffer, and then transmute the resulting &[T] to &B again.

  4. x == y implies that x.as_bytes() == y.as_bytes().

  5. B::from_bytes(&[]) and B::from_bytes_mut(&mut []) always produce valid values.

Notably, none of CStr, OsStr, or Path can implement Buf because their layout as slices is not part of their interface.

T may be zero-sized, but functions will panic in this case.

Required Associated Types§

Source

type Element: AsBytes + Copy

The element type of the underlying type. This is used for computing e.g. alignment and stride.

Provided Methods§

Source

fn elem_len(&self) -> usize

The length of this value, in elements.

Source

fn byte_len(&self) -> usize

The length of this value, in bytes.

Source

fn empty<'a, B: ?Sized + Buf>() -> &'a B

Creates a new empty Buf.

Source

fn as_bytes(&self) -> &[u8]

Converts a reference to a Buf into its underlying bytes.

Source

unsafe fn from_bytes(bytes: &[u8]) -> &Self

Converts a byte slice to a reference to a Buf.

§Safety

bytes must have been either constructed via transmuting from &Self, or a bytewise copy of a Self.

Source

fn as_bytes_mut(&mut self) -> &mut [u8]

Converts a reference to a Buf into its underlying bytes.

Source

unsafe fn from_bytes_mut(bytes: &mut [u8]) -> &mut Self

Converts a byte slice to a reference to a Buf.

§Safety

bytes must have been either constructed via transmuting from &Self, or a bytewise copy of a Self.

Source

unsafe fn slice_along_bytes<Idx>(&self, index: Idx) -> Option<&Self>
where Idx: SliceIndex<[u8], Output = [u8]>,

Performs a slicing operation on self with respect to byte indices.

§Safety

This function does not perform any checking beyonds bounds checking. For example, if called on str, this function may slice through a multi-byte Unicode scalar, producing a &str that violate’s str’s validity constraints (i.e., Undefined Behavior).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Buf for str

Source§

impl<T: AsBytes + Copy> Buf for [T]

Implementors§