1use de::read::SliceReader;
2use {ErrorKind, Result};
34/// A trait for erroring deserialization if not all bytes were read.
5pub trait TrailingBytes {
6/// Checks a given slice reader to determine if deserialization used all bytes in the slice.
7fn check_end(reader: &SliceReader) -> Result<()>;
8}
910/// A TrailingBytes config that will allow trailing bytes in slices after deserialization.
11#[derive(Copy, Clone)]
12pub struct AllowTrailing;
1314/// A TrailingBytes config that will cause bincode to produce an error if bytes are left over in the slice when deserialization is complete.
1516#[derive(Copy, Clone)]
17pub struct RejectTrailing;
1819impl TrailingBytes for AllowTrailing {
20#[inline(always)]
21fn check_end(_reader: &SliceReader) -> Result<()> {
22Ok(())
23 }
24}
2526impl TrailingBytes for RejectTrailing {
27#[inline(always)]
28fn check_end(reader: &SliceReader) -> Result<()> {
29if reader.is_finished() {
30Ok(())
31 } else {
32Err(Box::new(ErrorKind::Custom(
33"Slice had bytes remaining after deserialization".to_string(),
34 )))
35 }
36 }
37}