bincode/config/
trailing.rs

1use de::read::SliceReader;
2use {ErrorKind, Result};
3
4/// 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.
7    fn check_end(reader: &SliceReader) -> Result<()>;
8}
9
10/// A TrailingBytes config that will allow trailing bytes in slices after deserialization.
11#[derive(Copy, Clone)]
12pub struct AllowTrailing;
13
14/// A TrailingBytes config that will cause bincode to produce an error if bytes are left over in the slice when deserialization is complete.
15
16#[derive(Copy, Clone)]
17pub struct RejectTrailing;
18
19impl TrailingBytes for AllowTrailing {
20    #[inline(always)]
21    fn check_end(_reader: &SliceReader) -> Result<()> {
22        Ok(())
23    }
24}
25
26impl TrailingBytes for RejectTrailing {
27    #[inline(always)]
28    fn check_end(reader: &SliceReader) -> Result<()> {
29        if reader.is_finished() {
30            Ok(())
31        } else {
32            Err(Box::new(ErrorKind::Custom(
33                "Slice had bytes remaining after deserialization".to_string(),
34            )))
35        }
36    }
37}