Trait binary_layout::LayoutAs
source · 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 ...
});