Trait format_struct::FromByteSlice
source · pub unsafe trait FromByteSlice: Sized {
// Required methods
fn from_byte_slice(s: &[u8]) -> Result<&Self, InvalidSizeError>;
fn from_byte_slice_mut(s: &mut [u8]) -> Result<&mut Self, InvalidSizeError>;
fn slice_from_byte_slice(s: &[u8]) -> Result<&[Self], InvalidSizeError>;
fn slice_from_byte_slice_mut(
s: &mut [u8]
) -> Result<&mut [Self], InvalidSizeError>;
}
Expand description
An unsafe trait for types that byte slices may be transmuted into.
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 that has proper size. That means their alignment must be 1.
Required Methods§
sourcefn from_byte_slice(s: &[u8]) -> Result<&Self, InvalidSizeError>
fn from_byte_slice(s: &[u8]) -> Result<&Self, InvalidSizeError>
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.
sourcefn from_byte_slice_mut(s: &mut [u8]) -> Result<&mut Self, InvalidSizeError>
fn from_byte_slice_mut(s: &mut [u8]) -> Result<&mut Self, InvalidSizeError>
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.
sourcefn slice_from_byte_slice(s: &[u8]) -> Result<&[Self], InvalidSizeError>
fn slice_from_byte_slice(s: &[u8]) -> Result<&[Self], InvalidSizeError>
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.
sourcefn slice_from_byte_slice_mut(
s: &mut [u8]
) -> Result<&mut [Self], InvalidSizeError>
fn slice_from_byte_slice_mut( s: &mut [u8] ) -> Result<&mut [Self], InvalidSizeError>
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.