[−][src]Trait data_model::DataInit
Types for which it is safe to initialize from raw data.
A type T
is DataInit
if and only if it can be initialized by reading its contents from a
byte array. This is generally true for all plain-old-data structs. It is notably not true for
any type that includes a reference.
Implementing this trait guarantees that it is safe to instantiate the struct with random data.
Provided methods
fn from_slice(data: &[u8]) -> Option<&Self>
Converts a slice of raw data into a reference of Self
.
The value of data
is not copied. Instead a reference is made from the given slice. The
value of Self
will depend on the representation of the type in memory, and may change in
an unstable fashion.
This will return None
if the length of data does not match the size of Self
, or if the
data is not aligned for the type of Self
.
fn from_mut_slice(data: &mut [u8]) -> Option<&mut Self>
Converts a mutable slice of raw data into a mutable reference of Self
.
Because Self
is made from a reference to the mutable slice, mutations to the returned reference are immediately reflected in
data. The value of the returned
Self` will depend
on the representation of the type in memory, and may change in an unstable fashion.
This will return None
if the length of data does not match the size of Self
, or if the
data is not aligned for the type of Self
.
fn as_slice(&self) -> &[u8]
Converts a reference to self
into a slice of bytes.
The value of self
is not copied. Instead, the slice is made from a reference to self
.
The value of bytes in the returned slice will depend on the representation of the type in
memory, and may change in an unstable fashion.
fn as_mut_slice(&mut self) -> &mut [u8]
Converts a mutable reference to self
into a mutable slice of bytes.
Because the slice is made from a reference to self
, mutations to the returned slice are
immediately reflected in self
. The value of bytes in the returned slice will depend on
the representation of the type in memory, and may change in an unstable fashion.