[][src]Macro ethox::byte_wrapper

macro_rules! byte_wrapper {
    (
        pub struct $name:ident([u8])$(;)*
    ) => { ... };
    (
        $( #[$attr:meta] )*
        pub struct $name:ident([u8])$(;)*
    ) => { ... };
    (
        @$( #[$attr:meta] )*
        pub struct $name:ident([u8])
    ) => { ... };
}

Declare a dynamically sized byte wrapper.

Use this to create byte slices with inner invariants. This macro performs two basic actions:

  • Define a type with the indicated structure, documentation, attributes. The type can not have any generic arguments and can only wrap a simple byte slice.
  • Define two new private methods for conversion from a byte slice:
    • fn __from_macro_new_unchecked(&[u8]) -> &Self
    • fn __from_macro_new_unchecked_mut(&mut [u8]) -> &mut Self

Usage

You can currently only use a tuple type with a single member, a [u8].

byte_wrapper! {
    /// A udp packet.
    pub struct udp([u8]);
}

impl udp {
    // Make the constructor public.
    pub fn from_slice(slice: &[u8]) -> &Self {
        Self::__from_macro_new_unchecked(slice)
    }
}

let data = [0x20, 0x00, 0x00, 0x20, 0x00, 0x00, 0x08, 0x00];
let _= udp::from_slice(&data);