Function abomonation::decode [] [src]

pub fn decode<T: Abomonation>(bytes: &mut [u8]) -> Result<(&T, &mut [u8])&mut [u8]>

Decodes a binary buffer into a reference to a typed element.

decode treats the first mem::size_of::<T>() bytes as a T, and will then exhume the element, offering it the ability to consume prefixes of bytes to back any owned data. The return value is either a pair of the typed reference &T and the remaining &mut [u8] binary data if the deserialization found enough data, or the remaining &mut [u8] at the point where it ran out of data.

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();
encode(&vector, &mut bytes);

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