Function abomonation::verify [] [src]

pub fn verify<T: Abomonation>(bytes: &[u8]) -> Option<(&T, &[u8])>

Decodes an immutable binary slice into an immutable typed reference by validating the data .

verify is meant to be used on buffers that have already had decode called on them. Unline decode, verify can take a shared reference, as it does not attempt to mutate the underlying buffer. The return value is either a pair of the typed reference &T and the remaining &[u8] binary data, or None if decoding failed due to lack of data.

Examples

use abomonation::{encode, decode, verify};

// 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
assert!(decode::<Vec<(u64, String)>>(&mut bytes).is_some());

// remove mutability
let bytes = bytes;
if let Some((result, remaining)) = verify::<Vec<(u64, String)>>(&bytes) {
    assert!(result == &vector);
    assert!(remaining.len() == 0);
}