Trait binary_layout::FieldCopyAccess
source · pub trait FieldCopyAccess: Field {
type ReadError;
type WriteError;
type HighLevelType;
// Required methods
fn try_read(storage: &[u8]) -> Result<Self::HighLevelType, Self::ReadError>;
fn try_write(
storage: &mut [u8],
v: Self::HighLevelType
) -> Result<(), Self::WriteError>;
}
Expand description
This trait is implemented for fields with “try copy access”, i.e. fields that read/write data by copying it from/to the binary blob, but where reading or writing can fail. Examples of this are primitive types like NonZeroU8, NonZeroI32, …
Required Associated Types§
sourcetype ReadError
type ReadError
Error type that can be thrown from FieldCopyAccess::try_read. You can set this to Infallible if the function does not throw an error.
sourcetype WriteError
type WriteError
Error type that can be thrown from FieldCopyAccess::try_write. You can set this to Infallible if the function does not throw an error.
sourcetype HighLevelType
type HighLevelType
The data type that is returned from read calls and has to be passed in to write calls. This can be different from the primitive type used in the binary blob, since that primitive type can be wrapped (see WrappedField ) into a high level type before being returned from read calls (or vice versa unwrapped when writing).
Required Methods§
sourcefn try_read(storage: &[u8]) -> Result<Self::HighLevelType, Self::ReadError>
fn try_read(storage: &[u8]) -> Result<Self::HighLevelType, Self::ReadError>
Read the field from a given data region, assuming the defined layout, using the Field API.
§Example:
use binary_layout::prelude::*;
use core::num::NonZeroU16;
binary_layout!(my_layout, LittleEndian, {
//... other fields ...
some_integer_field: core::num::NonZeroU16,
//... other fields ...
});
fn func(storage_data: &[u8]) -> Result<NonZeroU16, NonZeroIsZeroError> {
let read: NonZeroU16 = my_layout::some_integer_field::try_read(storage_data)?;
Ok(read)
}
sourcefn try_write(
storage: &mut [u8],
v: Self::HighLevelType
) -> Result<(), Self::WriteError>
fn try_write( storage: &mut [u8], v: Self::HighLevelType ) -> Result<(), Self::WriteError>
Write the field to a given data region, assuming the defined layout, using the Field API.
§Example:
use binary_layout::prelude::*;
use core::num::NonZeroU16;
use core::convert::Infallible;
binary_layout!(my_layout, LittleEndian, {
//... other fields ...
some_integer_field: core::num::NonZeroU16,
//... other fields ...
});
fn func(storage_data: &mut [u8]) -> Result<(), Infallible> {
let value = NonZeroU16::new(10).unwrap();
my_layout::some_integer_field::try_write(storage_data, value)?;
Ok(())
}