[][src]Trait bytes_cast::BytesCast

pub unsafe trait BytesCast: Sized {
    pub fn from_bytes(bytes: &[u8]) -> Result<(&Self, &[u8]), FromBytesError> { ... }
pub fn slice_from_bytes(
        bytes: &[u8],
        slice_len: usize
    ) -> Result<(&[Self], &[u8]), FromBytesError> { ... } }

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

Safety

For a type to implement this trait:

  • All bit patterns must be valid. (This excludes bool, enums, etc.)
  • There must not be an alignment requirement. (align_of() == 1)

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.

Finally, mandating BytesCast for all fields means they also have align_of() == 1 and therefore the struct does not have any padding. This makes memory layout / field offsets easier to reason about.

Provided methods

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

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.

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

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.

Loading content...

Implementations on Foreign Types

impl<T: ?Sized> BytesCast for PhantomData<T>[src]

Loading content...

Implementors

impl BytesCast for ()[src]

impl BytesCast for F32Be[src]

impl BytesCast for F32Le[src]

impl BytesCast for F32Ne[src]

impl BytesCast for F64Be[src]

impl BytesCast for F64Le[src]

impl BytesCast for F64Ne[src]

impl BytesCast for I16Be[src]

impl BytesCast for I16Le[src]

impl BytesCast for I16Ne[src]

impl BytesCast for I32Be[src]

impl BytesCast for I32Le[src]

impl BytesCast for I32Ne[src]

impl BytesCast for I64Be[src]

impl BytesCast for I64Le[src]

impl BytesCast for I64Ne[src]

impl BytesCast for I128Be[src]

impl BytesCast for I128Le[src]

impl BytesCast for I128Ne[src]

impl BytesCast for U16Be[src]

impl BytesCast for U16Le[src]

impl BytesCast for U16Ne[src]

impl BytesCast for U32Be[src]

impl BytesCast for U32Le[src]

impl BytesCast for U32Ne[src]

impl BytesCast for U64Be[src]

impl BytesCast for U64Le[src]

impl BytesCast for U64Ne[src]

impl BytesCast for U128Be[src]

impl BytesCast for U128Le[src]

impl BytesCast for U128Ne[src]

impl BytesCast for u8[src]

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading content...