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:
-
Transmute
&Bto&[T], whereTiszerocopy::AsBytes. Transmute here is quite literal:mem::transmute<&B, &[T]>MUST be a valid way to convert between them. -
Transmute
&[T]to&Bif the contents of that&[T]originated from operation (1). -
Byte-copy
&Bto aT-aligned buffer, and then transmute the resulting&[T]to&Bagain. -
x == yimplies thatx.as_bytes() == y.as_bytes(). -
B::from_bytes(&[])andB::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§
Provided Methods§
Sourceunsafe fn from_bytes(bytes: &[u8]) -> &Self
unsafe fn from_bytes(bytes: &[u8]) -> &Self
Sourcefn as_bytes_mut(&mut self) -> &mut [u8]
fn as_bytes_mut(&mut self) -> &mut [u8]
Converts a reference to a Buf into its underlying bytes.
Sourceunsafe fn from_bytes_mut(bytes: &mut [u8]) -> &mut Self
unsafe fn from_bytes_mut(bytes: &mut [u8]) -> &mut Self
Sourceunsafe fn slice_along_bytes<Idx>(&self, index: Idx) -> Option<&Self>
unsafe fn slice_along_bytes<Idx>(&self, index: Idx) -> Option<&Self>
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.