Reimplement decode_len() with ShortU16 vistor helper

This commit is contained in:
Trent Nelson
2020-08-13 22:20:22 -06:00
committed by Trent Nelson
parent 6222fbcc66
commit 30dbe257cf
2 changed files with 18 additions and 7 deletions

View File

@ -199,10 +199,20 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for ShortVec<T> {
}
/// Return the decoded value and how many bytes it consumed.
pub fn decode_len(bytes: &[u8]) -> Result<(usize, usize), Box<bincode::ErrorKind>> {
let short_len: ShortU16 = bincode::deserialize(bytes)?;
let num_bytes = bincode::serialized_size(&short_len)?;
Ok((short_len.0 as usize, num_bytes as usize))
pub fn decode_len(bytes: &[u8]) -> Result<(usize, usize), ()> {
let mut len = 0;
let mut size = 0;
for byte in bytes.iter() {
match visit_byte(*byte, len, size) {
VisitResult::More(l, s) => {
len = l;
size = s;
}
VisitResult::Done(len, size) => return Ok((len, size)),
VisitResult::Err => return Err(()),
}
}
Err(())
}
#[cfg(test)]