use bebytes::BeBytes;
#[derive(BeBytes, Debug, PartialEq, Clone)]
struct WithBoundedVector {
size: u8,
#[FromField(size)]
bounded_vec: Vec<u8>, }
#[derive(BeBytes, Debug, PartialEq, Clone)]
struct SafeNestedVector {
first_field: u8,
nested_field: WithBoundedVector, last_field: u32,
}
#[derive(BeBytes, Debug, PartialEq, Clone)]
struct WithFixedSizeVector {
#[With(size(4))]
fixed_vec: Vec<u8>, }
#[derive(BeBytes, Debug, PartialEq, Clone)]
struct AnotherSafeNested {
some_field: u16,
another_nested: WithFixedSizeVector,
trailing_data: u64,
}
fn main() {
let safe = SafeNestedVector {
first_field: 1,
nested_field: WithBoundedVector {
size: 3,
bounded_vec: vec![2, 3, 4],
},
last_field: 5,
};
let bytes = safe.to_be_bytes();
let (deserialized, _) = SafeNestedVector::try_from_be_bytes(&bytes).unwrap();
assert_eq!(safe, deserialized);
let another = AnotherSafeNested {
some_field: 10,
another_nested: WithFixedSizeVector {
fixed_vec: vec![1, 2, 3, 4],
},
trailing_data: 42,
};
let bytes = another.to_be_bytes();
let (deserialized, _) = AnotherSafeNested::try_from_be_bytes(&bytes).unwrap();
assert_eq!(another, deserialized);
}