Function abomonation::decode
[−]
[src]
pub fn decode<T: Abomonation>(bytes: &mut [u8]) -> Result<&T, &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.
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) = decode::<Vec<(u64, String)>>(&mut bytes) { assert!(result == &vector); }