Struct binary_layout::WrappedField[][src]

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

TODO Doc

TODO Doc

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

Performs the conversion.

Performs the conversion.

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.