pub struct WrappedField<U, T: LayoutAs<U>, F: Field> { /* private fields */ }
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

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.