Skip to main content

bincode_next/de/
decoder.rs

1use super::BorrowDecoder;
2use super::Decoder;
3use super::read::BorrowReader;
4use super::read::Reader;
5use crate::config::Config;
6use crate::error::DecodeError;
7use crate::error_path::BincodeErrorPathCovered;
8use crate::utils::Sealed;
9
10impl<R, C: Config, Context> BincodeErrorPathCovered<0> for DecoderImpl<R, C, Context> {}
11impl<C, D: Decoder + ?Sized> BincodeErrorPathCovered<0> for WithContext<'_, D, C> {}
12
13/// A Decoder that reads bytes from a given reader `R`.
14///
15/// This struct should rarely be used.
16/// In most cases, prefer any of the `decode` functions.
17///
18/// The `ByteOrder` that is chosen will impact the endianness that
19/// is used to read integers out of the reader.
20///
21/// ```
22/// # let slice: &[u8] = &[0, 0, 0, 0];
23/// # let some_reader = bincode_next::de::read::SliceReader::new(slice);
24/// use bincode_next::de::Decode;
25/// use bincode_next::de::DecoderImpl;
26/// let mut context = ();
27/// let mut decoder = DecoderImpl::new(some_reader, bincode_next::config::standard(), &mut context);
28/// // this u32 can be any Decode
29/// let value = u32::decode(&mut decoder).unwrap();
30/// ```
31pub struct DecoderImpl<R, C: Config, Context> {
32    reader: R,
33    config: C,
34    bytes_read: usize,
35    context: Context,
36}
37
38impl<R: Reader, C: Config, Context> DecoderImpl<R, C, Context> {
39    /// Construct a new Decoder
40    #[inline(always)]
41    pub const fn new(
42        reader: R,
43        config: C,
44        context: Context,
45    ) -> Self {
46        Self {
47            reader,
48            config,
49            bytes_read: 0,
50            context,
51        }
52    }
53}
54
55impl<R, C: Config, Context> Sealed for DecoderImpl<R, C, Context> {}
56
57impl<'de, R: BorrowReader<'de>, C: Config, Context> BorrowDecoder<'de>
58    for DecoderImpl<R, C, Context>
59{
60    type BR = R;
61
62    #[inline(always)]
63    fn borrow_reader(&mut self) -> &mut Self::BR {
64        &mut self.reader
65    }
66}
67
68impl<R: Reader, C: Config, Context> Decoder for DecoderImpl<R, C, Context> {
69    type C = C;
70    type Context = Context;
71    type R = R;
72
73    #[inline(always)]
74    fn reader(&mut self) -> &mut Self::R {
75        &mut self.reader
76    }
77
78    #[inline(always)]
79    fn config(&self) -> &Self::C {
80        &self.config
81    }
82
83    #[inline(always)]
84    fn claim_bytes_read(
85        &mut self,
86        n: usize,
87    ) -> Result<(), DecodeError> {
88        Self::assert_covered();
89        // C::LIMIT is a const so this check should get compiled away
90        if let Some(limit) = C::LIMIT {
91            // Make sure we don't accidentally overflow `bytes_read`
92            if n > limit - self.bytes_read {
93                return crate::error::cold_decode_error_limit_exceeded();
94            }
95            self.bytes_read += n;
96        }
97        Ok(())
98    }
99
100    #[inline(always)]
101    fn unclaim_bytes_read(
102        &mut self,
103        n: usize,
104    ) {
105        // C::LIMIT is a const so this check should get compiled away
106        if C::LIMIT.is_some() {
107            // We should always be claiming more than we unclaim, so this should never underflow
108            self.bytes_read -= n;
109        }
110    }
111
112    #[inline(always)]
113    fn context(&mut self) -> &mut Self::Context {
114        &mut self.context
115    }
116}
117
118pub struct WithContext<'a, D: ?Sized, C> {
119    pub(crate) decoder: &'a mut D,
120    pub(crate) context: C,
121}
122
123impl<C, D: Decoder + ?Sized> Sealed for WithContext<'_, D, C> {}
124
125impl<Context, D: Decoder + ?Sized> Decoder for WithContext<'_, D, Context> {
126    type C = D::C;
127    type Context = Context;
128    type R = D::R;
129
130    #[inline(always)]
131    fn context(&mut self) -> &mut Self::Context {
132        &mut self.context
133    }
134
135    #[inline(always)]
136    fn reader(&mut self) -> &mut Self::R {
137        self.decoder.reader()
138    }
139
140    #[inline(always)]
141    fn config(&self) -> &Self::C {
142        self.decoder.config()
143    }
144
145    #[inline(always)]
146    fn claim_bytes_read(
147        &mut self,
148        n: usize,
149    ) -> Result<(), DecodeError> {
150        Self::assert_covered();
151        self.decoder.claim_bytes_read(n)
152    }
153
154    #[inline(always)]
155    fn unclaim_bytes_read(
156        &mut self,
157        n: usize,
158    ) {
159        self.decoder.unclaim_bytes_read(n);
160    }
161}
162
163impl<'de, C, D: BorrowDecoder<'de>> BorrowDecoder<'de> for WithContext<'_, D, C> {
164    type BR = D::BR;
165
166    #[inline(always)]
167    fn borrow_reader(&mut self) -> &mut Self::BR {
168        self.decoder.borrow_reader()
169    }
170}