Macro abomonation::unsafe_abomonate [] [src]

macro_rules! unsafe_abomonate {
    ($t:ty) => { ... };
    ($t:ty : $($field:ident),*) => { ... };
}

The unsafe_abomonate! macro takes a type name with an optional list of fields, and implements Abomonation for the type, following the pattern of the tuple implementations: each method calls the equivalent method on each of its fields.

#Safety unsafe_abomonate is unsafe because if you fail to specify a field it will not be properly re-initialized from binary data. This can leave you with a dangling pointer, or worse.

#Examples

#[macro_use]
extern crate abomonation;
use abomonation::{encode, decode, Abomonation};

#[derive(Eq, PartialEq)]
struct MyStruct {
    a: String,
    b: u64,
    c: Vec<u8>,
}

unsafe_abomonate!(MyStruct : a, b, c);

fn main() {

    // create some test data out of recently-abomonable types
    let my_struct = MyStruct { a: "grawwwwrr".to_owned(), b: 0, c: vec![1,2,3] };

    // encode a &MyStruct into a Vec<u8>
    let mut bytes = Vec::new();
    unsafe { encode(&my_struct, &mut bytes); }

    // decode a &MyStruct from &mut [u8] binary data
    if let Some((result, remaining)) = unsafe { decode::<MyStruct>(&mut bytes) } {
        assert!(result == &my_struct);
        assert!(remaining.len() == 0);
    }
}