Struct binary_layout::WrappedField
source · Expand description
A WrappedField is a Field that, unlike PrimitiveField, does not directly represents a primitive type. Instead, it represents a wrapper type that can be converted to/from a primitive type using the LayoutAs trait. See Field for more info on this API.
Example:
use binary_layout::{prelude::*, LayoutAs};
struct MyIdType(u64);
impl LayoutAs<u64> for MyIdType {
fn read(v: u64) -> MyIdType {
MyIdType(v)
}
fn write(v: MyIdType) -> u64 {
v.0
}
}
define_layout!(my_layout, BigEndian, {
// ... other fields ...
field: MyIdType as u64,
// ... other fields ...
});
fn func(storage_data: &mut [u8]) {
// read some data
let read_data: MyIdType = my_layout::field::read(storage_data);
// equivalent: let read_data = MyIdType(u16::from_le_bytes((&storage_data[0..2]).try_into().unwrap()));
// write some data
my_layout::field::write(storage_data, MyIdType(10));
// equivalent: data_slice[18..22].copy_from_slice(&10u32.to_le_bytes());
}
Trait Implementations
sourceimpl<U, T: LayoutAs<U>, F: Field> Field for WrappedField<U, T, F>
impl<U, T: LayoutAs<U>, F: Field> Field for WrappedField<U, T, F>
type Endian = <F as Field>::Endian
type Endian = <F as Field>::Endian
See Field::Endian
sourceconst OFFSET: usize = F::OFFSET
const OFFSET: usize = F::OFFSET
See Field::OFFSET
sourceconst SIZE: Option<usize> = F::SIZE
const SIZE: Option<usize> = F::SIZE
See Field::SIZE
sourceimpl<U, T: LayoutAs<U>, F: FieldCopyAccess<HighLevelType = U>> FieldCopyAccess for WrappedField<U, T, F>
impl<U, T: LayoutAs<U>, F: FieldCopyAccess<HighLevelType = U>> FieldCopyAccess for WrappedField<U, T, F>
type HighLevelType = T
type HighLevelType = T
sourcefn read(storage: &[u8]) -> Self::HighLevelType
fn read(storage: &[u8]) -> Self::HighLevelType
Read the field from a given data region, assuming the defined layout, using the Field API.
Example:
use binary_layout::{prelude::*, LayoutAs};
#[derive(Debug, PartialEq, Eq)]
struct MyIdType(u64);
impl LayoutAs<u64> for MyIdType {
fn read(v: u64) -> MyIdType {
MyIdType(v)
}
fn write(v: MyIdType) -> u64 {
v.0
}
}
define_layout!(my_layout, LittleEndian, {
//... other fields ...
some_integer_field: MyIdType as u64,
//... other fields ...
});
fn func(storage_data: &mut [u8]) {
my_layout::some_integer_field::write(storage_data, MyIdType(50));
assert_eq!(MyIdType(50), my_layout::some_integer_field::read(storage_data));
}
sourcefn write(storage: &mut [u8], v: Self::HighLevelType)
fn write(storage: &mut [u8], v: Self::HighLevelType)
Write the field to a given data region, assuming the defined layout, using the Field API.
Example:
See FieldCopyAccess::read for an example
Auto Trait Implementations
impl<U, T, F> RefUnwindSafe for WrappedField<U, T, F>where
F: RefUnwindSafe,
T: RefUnwindSafe,
U: RefUnwindSafe,
impl<U, T, F> Send for WrappedField<U, T, F>where
F: Send,
T: Send,
U: Send,
impl<U, T, F> Sync for WrappedField<U, T, F>where
F: Sync,
T: Sync,
U: Sync,
impl<U, T, F> Unpin for WrappedField<U, T, F>where
F: Unpin,
T: Unpin,
U: Unpin,
impl<U, T, F> UnwindSafe for WrappedField<U, T, F>where
F: UnwindSafe,
T: UnwindSafe,
U: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more