Function abomonation::decode
[−]
[src]
pub fn decode<T: Abomonation>(bytes: &mut [u8]) -> Option<(&T, &mut [u8])>
Decodes a mutable binary slice into an immutable typed reference.
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, or None
if decoding failed due to lack 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 Some((result, remaining)) = decode::<Vec<(u64, String)>>(&mut bytes) { assert!(result == &vector); assert!(remaining.len() == 0); }