Trait vm_memory::bytes::ByteValued
source · pub unsafe trait ByteValued: Copy + Default + Send + Sync {
// Provided methods
fn from_slice(data: &[u8]) -> Option<&Self> { ... }
fn from_mut_slice(data: &mut [u8]) -> Option<&mut Self> { ... }
fn as_slice(&self) -> &[u8] ⓘ { ... }
fn as_mut_slice(&mut self) -> &mut [u8] ⓘ { ... }
fn as_bytes(&mut self) -> VolatileSlice<'_> { ... }
}
Expand description
Types for which it is safe to initialize from raw data.
§Safety
A type T
is ByteValued
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§
sourcefn from_slice(data: &[u8]) -> Option<&Self>
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
.
sourcefn from_mut_slice(data: &mut [u8]) -> Option<&mut 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
.
sourcefn as_slice(&self) -> &[u8] ⓘ
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.
sourcefn as_mut_slice(&mut self) -> &mut [u8] ⓘ
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.
sourcefn as_bytes(&mut self) -> VolatileSlice<'_>
fn as_bytes(&mut self) -> VolatileSlice<'_>
Converts a mutable reference to self
into a VolatileSlice
. This is
useful because VolatileSlice
provides a Bytes<usize>
implementation.
§Safety
Unlike most VolatileMemory
implementation, this method requires an exclusive
reference to self
; this trivially fulfills VolatileSlice::new
’s requirement
that all accesses to self
use volatile accesses (because there can
be no other accesses).