pub struct WrappedField<U, T: LayoutAs<U>, F: Field> { /* private fields */ }
Expand description

A WrappedField is a Field that, unlike PrimitiveField, does not directly represent 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§

source§

impl<U, T: LayoutAs<U>, F: Field> Field for WrappedField<U, T, F>

§

type Endian = <F as Field>::Endian

source§

const OFFSET: usize = F::OFFSET

source§

const SIZE: Option<usize> = F::SIZE

source§

impl<U, T: LayoutAs<U>, F: FieldCopyAccess<HighLevelType = U>> FieldCopyAccess for WrappedField<U, T, F>

§

type HighLevelType = T

source§

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));
}
source§

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§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.