pub trait LayoutAs<U> {
    fn read(v: U) -> Self;
    fn write(v: Self) -> U;
}
Expand description

Implementing the LayoutAs trait for a custom type allows that custom type to be used as the type of a layout field. Note that the value of this type is copied each time it is accessed, so this is only recommended for primitive wrappers of primitive types, not for types that are expensive to copy.

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 ...
});

Required Methods

Implement this to define how the custom type is constructed from the underlying type after it was read from a layouted binary slice.

Implement this to define how the custom type is converted into the underlying type so it can be written into a layouted binary slice.

Implementors