Trait buf_trait::Buf

source ·
pub unsafe trait Buf {
    type Element: Copy;

    // Provided methods
    fn elem_len(&self) -> usize { ... }
    fn byte_len(&self) -> usize { ... }
    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 thin wrapper over a [T] for some Copy type T. In particular, the following must be valid operations:

  1. Transmute &impl Buf to &[T], where T has no uninitialized bits (no padding, etc).
  2. Transmute &[T] to &impl Buf if the contents of that &[T] originated from operation (1).
  3. Copy &impl Buf to an appropriately-aligned buffer, and then transmute the resulting &[T] to that &impl Buf again.

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

Required Associated Types§

source

type Element: 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 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).

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Buf for str

§

type Element = u8

source§

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

§

type Element = T

Implementors§