[−][src]Trait structview::View
Trait for viewing byte data as a higher-level representation.
By implementing View
a type declares that it is safe to be cast from
raw byte data.
Safety
Implementing this trait is unsafe since implementing types must fulfill some requirements that cannot be checked through the type system only:
- There must be no raw byte values that constitute invalid data for the
implementing type. For example, implementing
View
forstd::num::NonZeroI32
would be unsafe. - The implementing type must be 1-byte aligned.
- If the implementing type is a compound type, it must be ensured that
the compiler doesn't change the order of fields. This can be achieved
through
#[repr(C)]
.
It is recommended to use the custom View
derive also provided by this
crate instead of implementing this trait manually.
Provided methods
fn view(data: &[u8]) -> Result<&Self, Error>
View data
as a value of the implementing type.
This simply casts the &[u8]
reference to a reference of the
implementing type.
Errors
If data
is too short to fill the whole view,
Error::NotEnoughData
will be returned.
fn view_boxed_slice(data: Box<[u8]>) -> Result<Box<[Self]>, Error>
View boxed slice data
as a boxed slice of the implementing type.
This first reinterprets the data as a slice of the implementing type and then as a boxed slice
Errors
If the length of the u8 boxed slice is not a multiple of the struct's
size, Error::NotEnoughData
will be returned.
Panics
If the implementing type has a size of 0.
fn view_slice(data: &[u8]) -> Result<&[Self], Error>
View slice data
as a slice of the implementing type.
This simply reinterprets the data as a slice of the implementing type.
Errors
If the length of the u8 slice is not a multiple of the struct's size,
Error::NotEnoughData
will be returned.
Panics
If the implementing type has a size of 0.