Function abomonation::encode [] [src]

pub unsafe fn encode<T: Abomonation>(typed: &T, bytes: &mut Vec<u8>)

Encodes a typed reference into a binary buffer.

encode will transmute typed to binary and write its contents to bytes. It then offers the element the opportunity to serialize more data. Having done that, it offers the element the opportunity to "tidy up", in which the element can erasing things like local memory addresses that it would be impolite to share.

Examples

use abomonation::{encode, decode};

// create some test data out of abomonation-approved types
let vector = (0..256u64).map(|i| (i, format!("{}", i)))
                        .collect::<Vec<_>>();

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

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