Struct binary_layout::WrappedField [−][src]
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
See Field::Endian
See Field::OFFSET
impl<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
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)); }
Write the field to a given data region, assuming the defined layout, using the Field API.
Example:
See FieldCopyAccess::read for an example
See SizedField::SIZE
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,