Skip to main content

float_pigment_consistent_bincode/config/
trailing.rs

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