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 let Some(sum) = self.bytes_read.checked_add(n) {
93                if sum > limit {
94                    return crate::error::cold_decode_error_limit_exceeded();
95                }
96                self.bytes_read = sum;
97                Ok(())
98            } else {
99                crate::error::cold_decode_error_limit_exceeded()
100            }
101        } else {
102            Ok(())
103        }
104    }
105
106    #[inline(always)]
107    fn unclaim_bytes_read(
108        &mut self,
109        n: usize,
110    ) {
111        // C::LIMIT is a const so this check should get compiled away
112        if C::LIMIT.is_some() {
113            // We should always be claiming more than we unclaim, so this should never underflow
114            self.bytes_read -= n;
115        }
116    }
117
118    #[inline(always)]
119    fn context(&mut self) -> &mut Self::Context {
120        &mut self.context
121    }
122}
123
124pub struct WithContext<'a, D: ?Sized, C> {
125    pub(crate) decoder: &'a mut D,
126    pub(crate) context: C,
127}
128
129impl<C, D: Decoder + ?Sized> Sealed for WithContext<'_, D, C> {}
130
131impl<Context, D: Decoder + ?Sized> Decoder for WithContext<'_, D, Context> {
132    type C = D::C;
133    type Context = Context;
134    type R = D::R;
135
136    #[inline(always)]
137    fn context(&mut self) -> &mut Self::Context {
138        &mut self.context
139    }
140
141    #[inline(always)]
142    fn reader(&mut self) -> &mut Self::R {
143        self.decoder.reader()
144    }
145
146    #[inline(always)]
147    fn config(&self) -> &Self::C {
148        self.decoder.config()
149    }
150
151    #[inline(always)]
152    fn claim_bytes_read(
153        &mut self,
154        n: usize,
155    ) -> Result<(), DecodeError> {
156        Self::assert_covered();
157        self.decoder.claim_bytes_read(n)
158    }
159
160    #[inline(always)]
161    fn unclaim_bytes_read(
162        &mut self,
163        n: usize,
164    ) {
165        self.decoder.unclaim_bytes_read(n);
166    }
167}
168
169impl<'de, C, D: BorrowDecoder<'de>> BorrowDecoder<'de> for WithContext<'_, D, C> {
170    type BR = D::BR;
171
172    #[inline(always)]
173    fn borrow_reader(&mut self) -> &mut Self::BR {
174        self.decoder.borrow_reader()
175    }
176}