Trait ReprByteSlice

Source
pub unsafe trait ReprByteSlice: Sized {
    // Required methods
    fn from_byte_slice(s: &[u8]) -> Result<&Self, UnalignedSizeError>;
    fn from_byte_slice_mut(
        s: &mut [u8],
    ) -> Result<&mut Self, UnalignedSizeError>;
    fn as_byte_slice(&self) -> &[u8];
    fn as_byte_slice_mut(&mut self) -> &mut [u8];
    fn slice_from_byte_slice(s: &[u8]) -> Result<&[Self], UnalignedSizeError>;
    fn slice_from_byte_slice_mut(
        s: &mut [u8],
    ) -> Result<&mut [Self], UnalignedSizeError>;
    fn slice_as_byte_slice(
        slice: &[Self],
    ) -> Result<&[u8], SliceSizeOverflowError>;
    fn slice_as_byte_slice_mut(
        s: &mut [Self],
    ) -> Result<&mut [u8], SliceSizeOverflowError>;
    fn uninit_slice_from_byte_slice(
        bytes: &[MaybeUninit<u8>],
    ) -> Result<&[MaybeUninit<Self>], UnalignedSizeError>;
    fn uninit_slice_from_byte_slice_mut(
        bytes: &mut [MaybeUninit<u8>],
    ) -> Result<&mut [MaybeUninit<Self>], UnalignedSizeError>;
    fn uninit_slice_as_byte_slice(
        slice: &[MaybeUninit<Self>],
    ) -> Result<&[MaybeUninit<u8>], SliceSizeOverflowError>;
    fn uninit_slice_as_byte_slice_mut(
        s: &mut [MaybeUninit<Self>],
    ) -> Result<&mut [MaybeUninit<u8>], SliceSizeOverflowError>;
}
Expand description

An unsafe trait for types that may be safely transmuted from and to byte slices.

This trait is usually automatically implemented by the format_struct macro so there is no need to implement it manually.

All the trait’s methods could be implemented automatically but are not due to limitations of the Rust’s generics: using Self in a const context (array size on our case) isn’t possible in traits. Since the trait isn’t meant to be implemented manually that is considered a non-issue.

§Safety

Types implementing the trait must be safe to transmute from an arbitrary byte slice of the same size as the type itself. The alignment for the type must be 1.

Required Methods§

Source

fn from_byte_slice(s: &[u8]) -> Result<&Self, UnalignedSizeError>

Transmutes an immutable byte slice reference into an immutable Self reference.

§Errors

Returns an error in case the size doesn’t match the type’s size.

Source

fn from_byte_slice_mut(s: &mut [u8]) -> Result<&mut Self, UnalignedSizeError>

Transmutes a mutable byte slice reference into a mutable Self reference.

§Errors

Returns an error in case the size doesn’t match the type’s size.

Source

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

Transmutes an immutable reference to self into an immutable reference to a byte slice.

Source

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

Transmutes a mutable reference to self into a mutable reference to a byte slice.

Source

fn slice_from_byte_slice(s: &[u8]) -> Result<&[Self], UnalignedSizeError>

Transmutes an immutable byte slice reference into an immutable to a slice of Self.

§Errors

Returns an error in case the size isn’t a multiple of the type’s size.

Source

fn slice_from_byte_slice_mut( s: &mut [u8], ) -> Result<&mut [Self], UnalignedSizeError>

Transmutes a mutable byte slice reference into a mutable to a slice of Self.

§Errors

Returns an error in case the size isn’t a multiple of the type’s size.

Source

fn slice_as_byte_slice(slice: &[Self]) -> Result<&[u8], SliceSizeOverflowError>

Transmutes an immutable reference to a slice of Self into an immutable reference to a byte slice.

§Errors

Returns SliceSizeOverflowError in case the product of the slice length and the type’s size would be larger than isize::MAX.

Source

fn slice_as_byte_slice_mut( s: &mut [Self], ) -> Result<&mut [u8], SliceSizeOverflowError>

Transmutes a mutable reference to a slice of Self into a mutable reference to a byte slice.

§Errors

Returns SliceSizeOverflowError in case the product of the slice length and the type’s size would be larger than isize::MAX.

Source

fn uninit_slice_from_byte_slice( bytes: &[MaybeUninit<u8>], ) -> Result<&[MaybeUninit<Self>], UnalignedSizeError>

Transmutes an immutable reference to a slice of MaybeUninit<u8> into an immutable reference to a slice of MaybeUninit<Self>.

§Errors

Returns an error in case the size isn’t a multiple of the type’s size.

Source

fn uninit_slice_from_byte_slice_mut( bytes: &mut [MaybeUninit<u8>], ) -> Result<&mut [MaybeUninit<Self>], UnalignedSizeError>

Transmutes a mutable reference to a slice of MaybeUninit<u8> into a mutable reference to a slice of MaybeUninit<Self>.

§Errors

Returns an error in case the size isn’t a multiple of the type’s size.

Source

fn uninit_slice_as_byte_slice( slice: &[MaybeUninit<Self>], ) -> Result<&[MaybeUninit<u8>], SliceSizeOverflowError>

Transmutes an immutable reference to a slice of MaybeUninit<Self> into an immutable reference to a slice of MaybeUninit<u8>.

§Errors

Returns SliceSizeOverflowError in case the product of the slice length and the type’s size would be larger than isize::MAX.

Source

fn uninit_slice_as_byte_slice_mut( s: &mut [MaybeUninit<Self>], ) -> Result<&mut [MaybeUninit<u8>], SliceSizeOverflowError>

Transmutes a mutable reference to a slice of MaybeUninit<u8> into a mutable reference to a slice of MaybeUninit<Self>.

§Errors

Returns SliceSizeOverflowError in case the product of the slice length and the type’s size would be larger than isize::MAX.

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 ReprByteSlice for u8

Source§

impl<T: ReprByteSlice, const N: usize> ReprByteSlice for [T; N]

Implementors§