Skip to main content

BytesCast

Trait BytesCast 

Source
pub unsafe trait BytesCast {
    // Provided methods
    fn from_bytes(bytes: &[u8]) -> Result<(&Self, &[u8]), FromBytesError>
       where Self: Sized { ... }
    fn slice_from_bytes(
        bytes: &[u8],
        slice_len: usize,
    ) -> Result<(&[Self], &[u8]), FromBytesError>
       where Self: Sized { ... }
    fn as_bytes(&self) -> &[u8] { ... }
}
Expand description

Marks a type as safe to interpret from and to bytes without copying.

§Safety

For a type to implement this trait:

  • All initialized bit patterns must be valid. (This excludes bool, enums, etc.)
  • There must not be an alignment requirement. (align_of() == 1)
  • There must be no padding or otherwise uninitialized bytes

§Deriving

Instead of writing unsafe impl blocks this trait should be derived. #[derive(BytesCast)] on a type definition invokes a procedural macro that implements the trait after checking that the type:

  • Is a struct
  • Is not generic
  • Has a #[repr(C)] or #[repr(transparent)] attribute
  • Has align_of() == 1
  • Only has fields whose respective type implement BytesCast.

Failing any of these checks causes a compile-time error. This excludes some types that could implement BytesCast without memory safety issue:

  • By choice: disabling field reordering with repr is not about memory safety but making memory layout / field offsets predictable.
  • By necessity: generics would make align_of potentially depend on type parameters and not possible to statically check at the struct definition site.

Provided Methods§

Source

fn from_bytes(bytes: &[u8]) -> Result<(&Self, &[u8]), FromBytesError>
where Self: Sized,

Interpret the start of the given slice of bytes as reference to this type.

If the given input is large enough, returns a tuple of the new reference and the remaining of the bytes.

Source

fn slice_from_bytes( bytes: &[u8], slice_len: usize, ) -> Result<(&[Self], &[u8]), FromBytesError>
where Self: Sized,

Interpret the start of the given slice of bytes as slice of this type.

If the given input is large enough, returns a tuple of the new slice and the remaining of the bytes.

Source

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

Interpret this value as the bytes of its memory representation.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl BytesCast for u8

Source§

impl BytesCast for ()

Source§

impl<T: BytesCast> BytesCast for [T; 0]

Source§

impl<T: BytesCast> BytesCast for [T; 1]

Source§

impl<T: BytesCast> BytesCast for [T; 2]

Source§

impl<T: BytesCast> BytesCast for [T; 3]

Source§

impl<T: BytesCast> BytesCast for [T; 4]

Source§

impl<T: BytesCast> BytesCast for [T; 5]

Source§

impl<T: BytesCast> BytesCast for [T; 6]

Source§

impl<T: BytesCast> BytesCast for [T; 7]

Source§

impl<T: BytesCast> BytesCast for [T; 8]

Source§

impl<T: BytesCast> BytesCast for [T; 9]

Source§

impl<T: BytesCast> BytesCast for [T; 10]

Source§

impl<T: BytesCast> BytesCast for [T; 11]

Source§

impl<T: BytesCast> BytesCast for [T; 12]

Source§

impl<T: BytesCast> BytesCast for [T; 13]

Source§

impl<T: BytesCast> BytesCast for [T; 14]

Source§

impl<T: BytesCast> BytesCast for [T; 15]

Source§

impl<T: BytesCast> BytesCast for [T; 16]

Source§

impl<T: BytesCast> BytesCast for [T; 17]

Source§

impl<T: BytesCast> BytesCast for [T; 18]

Source§

impl<T: BytesCast> BytesCast for [T; 19]

Source§

impl<T: BytesCast> BytesCast for [T; 20]

Source§

impl<T: BytesCast> BytesCast for [T; 21]

Source§

impl<T: BytesCast> BytesCast for [T; 22]

Source§

impl<T: BytesCast> BytesCast for [T; 23]

Source§

impl<T: BytesCast> BytesCast for [T; 24]

Source§

impl<T: BytesCast> BytesCast for [T; 25]

Source§

impl<T: BytesCast> BytesCast for [T; 26]

Source§

impl<T: BytesCast> BytesCast for [T; 27]

Source§

impl<T: BytesCast> BytesCast for [T; 28]

Source§

impl<T: BytesCast> BytesCast for [T; 29]

Source§

impl<T: BytesCast> BytesCast for [T; 30]

Source§

impl<T: BytesCast> BytesCast for [T; 31]

Source§

impl<T: BytesCast> BytesCast for [T; 32]

Source§

impl<T: BytesCast> BytesCast for [T]

Source§

impl<T: ?Sized> BytesCast for PhantomData<T>

Implementors§