Function bao::decode::parse_and_check_content_len
source · Expand description
Parse the length of an encoded slice and convert it to a usize
. This is useful if you need to
allocate space for decoding.
If the encoded slice isn’t long enough to represent the reported length – which includes the
case where the length can’t fit in a usize
– this returns Error::Truncated
.
Example
let input = b"foobar";
let (_, encoded) = bao::encode::encode_to_vec(input);
let content_len = bao::decode::parse_and_check_content_len(&encoded).unwrap();
assert_eq!(input.len(), content_len);
let err = bao::decode::parse_and_check_content_len(&encoded[..encoded.len()/2]).unwrap_err();
assert_eq!(bao::decode::Error::Truncated, err);