/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bincode-2.0.1/src/de/impls.rs
Line | Count | Source |
1 | | use super::{ |
2 | | read::{BorrowReader, Reader}, |
3 | | BorrowDecode, BorrowDecoder, Decode, Decoder, |
4 | | }; |
5 | | use crate::{ |
6 | | config::{Endianness, IntEncoding, InternalEndianConfig, InternalIntEncodingConfig}, |
7 | | error::{DecodeError, IntegerType}, |
8 | | impl_borrow_decode, |
9 | | }; |
10 | | use core::{ |
11 | | cell::{Cell, RefCell}, |
12 | | cmp::Reverse, |
13 | | num::{ |
14 | | NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128, |
15 | | NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize, Wrapping, |
16 | | }, |
17 | | ops::{Bound, Range, RangeInclusive}, |
18 | | time::Duration, |
19 | | }; |
20 | | |
21 | | impl<Context> Decode<Context> for bool { |
22 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
23 | 0 | match u8::decode(decoder)? { |
24 | 0 | 0 => Ok(false), |
25 | 0 | 1 => Ok(true), |
26 | 0 | x => Err(DecodeError::InvalidBooleanValue(x)), |
27 | | } |
28 | 0 | } |
29 | | } |
30 | | impl_borrow_decode!(bool); |
31 | | |
32 | | impl<Context> Decode<Context> for u8 { |
33 | | #[inline] |
34 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
35 | 0 | decoder.claim_bytes_read(1)?; |
36 | 0 | if let Some(buf) = decoder.reader().peek_read(1) { |
37 | 0 | let byte = buf[0]; |
38 | 0 | decoder.reader().consume(1); |
39 | 0 | Ok(byte) |
40 | | } else { |
41 | 0 | let mut bytes = [0u8; 1]; |
42 | 0 | decoder.reader().read(&mut bytes)?; |
43 | 0 | Ok(bytes[0]) |
44 | | } |
45 | 0 | } Unexecuted instantiation: <u8 as bincode::de::Decode<()>>::decode::<&mut bincode::de::decoder::DecoderImpl<bincode::features::impl_std::IoReader<&mut std::io::buffered::bufreader::BufReader<std::fs::File>>, bincode::config::Configuration, ()>> Unexecuted instantiation: <u8 as bincode::de::Decode<()>>::decode::<&mut bincode::de::decoder::DecoderImpl<bincode::features::impl_std::IoReader<&mut surrealmx::compression::CompressedReader>, bincode::config::Configuration, ()>> Unexecuted instantiation: <u8 as bincode::de::Decode<_>>::decode::<_> |
46 | | } |
47 | | impl_borrow_decode!(u8); |
48 | | |
49 | | impl<Context> Decode<Context> for NonZeroU8 { |
50 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
51 | 0 | NonZeroU8::new(u8::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
52 | 0 | non_zero_type: IntegerType::U8, |
53 | 0 | }) |
54 | 0 | } |
55 | | } |
56 | | impl_borrow_decode!(NonZeroU8); |
57 | | |
58 | | impl<Context> Decode<Context> for u16 { |
59 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
60 | 0 | decoder.claim_bytes_read(2)?; |
61 | 0 | match D::C::INT_ENCODING { |
62 | | IntEncoding::Variable => { |
63 | 0 | crate::varint::varint_decode_u16(decoder.reader(), D::C::ENDIAN) |
64 | | } |
65 | | IntEncoding::Fixed => { |
66 | 0 | let mut bytes = [0u8; 2]; |
67 | 0 | decoder.reader().read(&mut bytes)?; |
68 | 0 | Ok(match D::C::ENDIAN { |
69 | 0 | Endianness::Little => u16::from_le_bytes(bytes), |
70 | 0 | Endianness::Big => u16::from_be_bytes(bytes), |
71 | | }) |
72 | | } |
73 | | } |
74 | 0 | } |
75 | | } |
76 | | impl_borrow_decode!(u16); |
77 | | |
78 | | impl<Context> Decode<Context> for NonZeroU16 { |
79 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
80 | 0 | NonZeroU16::new(u16::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
81 | 0 | non_zero_type: IntegerType::U16, |
82 | 0 | }) |
83 | 0 | } |
84 | | } |
85 | | impl_borrow_decode!(NonZeroU16); |
86 | | |
87 | | impl<Context> Decode<Context> for u32 { |
88 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
89 | 0 | decoder.claim_bytes_read(4)?; |
90 | 0 | match D::C::INT_ENCODING { |
91 | | IntEncoding::Variable => { |
92 | 0 | crate::varint::varint_decode_u32(decoder.reader(), D::C::ENDIAN) |
93 | | } |
94 | | IntEncoding::Fixed => { |
95 | 0 | let mut bytes = [0u8; 4]; |
96 | 0 | decoder.reader().read(&mut bytes)?; |
97 | 0 | Ok(match D::C::ENDIAN { |
98 | 0 | Endianness::Little => u32::from_le_bytes(bytes), |
99 | 0 | Endianness::Big => u32::from_be_bytes(bytes), |
100 | | }) |
101 | | } |
102 | | } |
103 | 0 | } |
104 | | } |
105 | | impl_borrow_decode!(u32); |
106 | | |
107 | | impl<Context> Decode<Context> for NonZeroU32 { |
108 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
109 | 0 | NonZeroU32::new(u32::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
110 | 0 | non_zero_type: IntegerType::U32, |
111 | 0 | }) |
112 | 0 | } |
113 | | } |
114 | | impl_borrow_decode!(NonZeroU32); |
115 | | |
116 | | impl<Context> Decode<Context> for u64 { |
117 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
118 | 0 | decoder.claim_bytes_read(8)?; |
119 | 0 | match D::C::INT_ENCODING { |
120 | | IntEncoding::Variable => { |
121 | 0 | crate::varint::varint_decode_u64(decoder.reader(), D::C::ENDIAN) |
122 | | } |
123 | | IntEncoding::Fixed => { |
124 | 0 | let mut bytes = [0u8; 8]; |
125 | 0 | decoder.reader().read(&mut bytes)?; |
126 | 0 | Ok(match D::C::ENDIAN { |
127 | 0 | Endianness::Little => u64::from_le_bytes(bytes), |
128 | 0 | Endianness::Big => u64::from_be_bytes(bytes), |
129 | | }) |
130 | | } |
131 | | } |
132 | 0 | } Unexecuted instantiation: <u64 as bincode::de::Decode<()>>::decode::<&mut bincode::de::decoder::DecoderImpl<bincode::features::impl_std::IoReader<&mut std::io::buffered::bufreader::BufReader<std::fs::File>>, bincode::config::Configuration, ()>> Unexecuted instantiation: <u64 as bincode::de::Decode<()>>::decode::<&mut bincode::de::decoder::DecoderImpl<bincode::features::impl_std::IoReader<&mut surrealmx::compression::CompressedReader>, bincode::config::Configuration, ()>> Unexecuted instantiation: <u64 as bincode::de::Decode<_>>::decode::<_> |
133 | | } |
134 | | impl_borrow_decode!(u64); |
135 | | |
136 | | impl<Context> Decode<Context> for NonZeroU64 { |
137 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
138 | 0 | NonZeroU64::new(u64::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
139 | 0 | non_zero_type: IntegerType::U64, |
140 | 0 | }) |
141 | 0 | } |
142 | | } |
143 | | impl_borrow_decode!(NonZeroU64); |
144 | | |
145 | | impl<Context> Decode<Context> for u128 { |
146 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
147 | 0 | decoder.claim_bytes_read(16)?; |
148 | 0 | match D::C::INT_ENCODING { |
149 | | IntEncoding::Variable => { |
150 | 0 | crate::varint::varint_decode_u128(decoder.reader(), D::C::ENDIAN) |
151 | | } |
152 | | IntEncoding::Fixed => { |
153 | 0 | let mut bytes = [0u8; 16]; |
154 | 0 | decoder.reader().read(&mut bytes)?; |
155 | 0 | Ok(match D::C::ENDIAN { |
156 | 0 | Endianness::Little => u128::from_le_bytes(bytes), |
157 | 0 | Endianness::Big => u128::from_be_bytes(bytes), |
158 | | }) |
159 | | } |
160 | | } |
161 | 0 | } |
162 | | } |
163 | | impl_borrow_decode!(u128); |
164 | | |
165 | | impl<Context> Decode<Context> for NonZeroU128 { |
166 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
167 | 0 | NonZeroU128::new(u128::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
168 | 0 | non_zero_type: IntegerType::U128, |
169 | 0 | }) |
170 | 0 | } |
171 | | } |
172 | | impl_borrow_decode!(NonZeroU128); |
173 | | |
174 | | impl<Context> Decode<Context> for usize { |
175 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
176 | 0 | decoder.claim_bytes_read(8)?; |
177 | 0 | match D::C::INT_ENCODING { |
178 | | IntEncoding::Variable => { |
179 | 0 | crate::varint::varint_decode_usize(decoder.reader(), D::C::ENDIAN) |
180 | | } |
181 | | IntEncoding::Fixed => { |
182 | 0 | let mut bytes = [0u8; 8]; |
183 | 0 | decoder.reader().read(&mut bytes)?; |
184 | | |
185 | 0 | let value = match D::C::ENDIAN { |
186 | 0 | Endianness::Little => u64::from_le_bytes(bytes), |
187 | 0 | Endianness::Big => u64::from_be_bytes(bytes), |
188 | | }; |
189 | | |
190 | 0 | value |
191 | 0 | .try_into() |
192 | 0 | .map_err(|_| DecodeError::OutsideUsizeRange(value)) Unexecuted instantiation: <usize as bincode::de::Decode<()>>::decode::<&mut bincode::de::decoder::DecoderImpl<bincode::features::impl_std::IoReader<&mut surrealmx::compression::CompressedReader>, bincode::config::Configuration, ()>>::{closure#0}Unexecuted instantiation: <usize as bincode::de::Decode<_>>::decode::<_>::{closure#0} |
193 | | } |
194 | | } |
195 | 0 | } Unexecuted instantiation: <usize as bincode::de::Decode<()>>::decode::<&mut bincode::de::decoder::DecoderImpl<bincode::features::impl_std::IoReader<&mut surrealmx::compression::CompressedReader>, bincode::config::Configuration, ()>> Unexecuted instantiation: <usize as bincode::de::Decode<_>>::decode::<_> |
196 | | } |
197 | | impl_borrow_decode!(usize); |
198 | | |
199 | | impl<Context> Decode<Context> for NonZeroUsize { |
200 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
201 | 0 | NonZeroUsize::new(usize::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
202 | 0 | non_zero_type: IntegerType::Usize, |
203 | 0 | }) |
204 | 0 | } |
205 | | } |
206 | | impl_borrow_decode!(NonZeroUsize); |
207 | | |
208 | | impl<Context> Decode<Context> for i8 { |
209 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
210 | 0 | decoder.claim_bytes_read(1)?; |
211 | 0 | let mut bytes = [0u8; 1]; |
212 | 0 | decoder.reader().read(&mut bytes)?; |
213 | 0 | Ok(bytes[0] as i8) |
214 | 0 | } |
215 | | } |
216 | | impl_borrow_decode!(i8); |
217 | | |
218 | | impl<Context> Decode<Context> for NonZeroI8 { |
219 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
220 | 0 | NonZeroI8::new(i8::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
221 | 0 | non_zero_type: IntegerType::I8, |
222 | 0 | }) |
223 | 0 | } |
224 | | } |
225 | | impl_borrow_decode!(NonZeroI8); |
226 | | |
227 | | impl<Context> Decode<Context> for i16 { |
228 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
229 | 0 | decoder.claim_bytes_read(2)?; |
230 | 0 | match D::C::INT_ENCODING { |
231 | | IntEncoding::Variable => { |
232 | 0 | crate::varint::varint_decode_i16(decoder.reader(), D::C::ENDIAN) |
233 | | } |
234 | | IntEncoding::Fixed => { |
235 | 0 | let mut bytes = [0u8; 2]; |
236 | 0 | decoder.reader().read(&mut bytes)?; |
237 | 0 | Ok(match D::C::ENDIAN { |
238 | 0 | Endianness::Little => i16::from_le_bytes(bytes), |
239 | 0 | Endianness::Big => i16::from_be_bytes(bytes), |
240 | | }) |
241 | | } |
242 | | } |
243 | 0 | } |
244 | | } |
245 | | impl_borrow_decode!(i16); |
246 | | |
247 | | impl<Context> Decode<Context> for NonZeroI16 { |
248 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
249 | 0 | NonZeroI16::new(i16::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
250 | 0 | non_zero_type: IntegerType::I16, |
251 | 0 | }) |
252 | 0 | } |
253 | | } |
254 | | impl_borrow_decode!(NonZeroI16); |
255 | | |
256 | | impl<Context> Decode<Context> for i32 { |
257 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
258 | 0 | decoder.claim_bytes_read(4)?; |
259 | 0 | match D::C::INT_ENCODING { |
260 | | IntEncoding::Variable => { |
261 | 0 | crate::varint::varint_decode_i32(decoder.reader(), D::C::ENDIAN) |
262 | | } |
263 | | IntEncoding::Fixed => { |
264 | 0 | let mut bytes = [0u8; 4]; |
265 | 0 | decoder.reader().read(&mut bytes)?; |
266 | 0 | Ok(match D::C::ENDIAN { |
267 | 0 | Endianness::Little => i32::from_le_bytes(bytes), |
268 | 0 | Endianness::Big => i32::from_be_bytes(bytes), |
269 | | }) |
270 | | } |
271 | | } |
272 | 0 | } |
273 | | } |
274 | | impl_borrow_decode!(i32); |
275 | | |
276 | | impl<Context> Decode<Context> for NonZeroI32 { |
277 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
278 | 0 | NonZeroI32::new(i32::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
279 | 0 | non_zero_type: IntegerType::I32, |
280 | 0 | }) |
281 | 0 | } |
282 | | } |
283 | | impl_borrow_decode!(NonZeroI32); |
284 | | |
285 | | impl<Context> Decode<Context> for i64 { |
286 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
287 | 0 | decoder.claim_bytes_read(8)?; |
288 | 0 | match D::C::INT_ENCODING { |
289 | | IntEncoding::Variable => { |
290 | 0 | crate::varint::varint_decode_i64(decoder.reader(), D::C::ENDIAN) |
291 | | } |
292 | | IntEncoding::Fixed => { |
293 | 0 | let mut bytes = [0u8; 8]; |
294 | 0 | decoder.reader().read(&mut bytes)?; |
295 | 0 | Ok(match D::C::ENDIAN { |
296 | 0 | Endianness::Little => i64::from_le_bytes(bytes), |
297 | 0 | Endianness::Big => i64::from_be_bytes(bytes), |
298 | | }) |
299 | | } |
300 | | } |
301 | 0 | } |
302 | | } |
303 | | impl_borrow_decode!(i64); |
304 | | |
305 | | impl<Context> Decode<Context> for NonZeroI64 { |
306 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
307 | 0 | NonZeroI64::new(i64::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
308 | 0 | non_zero_type: IntegerType::I64, |
309 | 0 | }) |
310 | 0 | } |
311 | | } |
312 | | impl_borrow_decode!(NonZeroI64); |
313 | | |
314 | | impl<Context> Decode<Context> for i128 { |
315 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
316 | 0 | decoder.claim_bytes_read(16)?; |
317 | 0 | match D::C::INT_ENCODING { |
318 | | IntEncoding::Variable => { |
319 | 0 | crate::varint::varint_decode_i128(decoder.reader(), D::C::ENDIAN) |
320 | | } |
321 | | IntEncoding::Fixed => { |
322 | 0 | let mut bytes = [0u8; 16]; |
323 | 0 | decoder.reader().read(&mut bytes)?; |
324 | 0 | Ok(match D::C::ENDIAN { |
325 | 0 | Endianness::Little => i128::from_le_bytes(bytes), |
326 | 0 | Endianness::Big => i128::from_be_bytes(bytes), |
327 | | }) |
328 | | } |
329 | | } |
330 | 0 | } |
331 | | } |
332 | | impl_borrow_decode!(i128); |
333 | | |
334 | | impl<Context> Decode<Context> for NonZeroI128 { |
335 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
336 | 0 | NonZeroI128::new(i128::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
337 | 0 | non_zero_type: IntegerType::I128, |
338 | 0 | }) |
339 | 0 | } |
340 | | } |
341 | | impl_borrow_decode!(NonZeroI128); |
342 | | |
343 | | impl<Context> Decode<Context> for isize { |
344 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
345 | 0 | decoder.claim_bytes_read(8)?; |
346 | 0 | match D::C::INT_ENCODING { |
347 | | IntEncoding::Variable => { |
348 | 0 | crate::varint::varint_decode_isize(decoder.reader(), D::C::ENDIAN) |
349 | | } |
350 | | IntEncoding::Fixed => { |
351 | 0 | let mut bytes = [0u8; 8]; |
352 | 0 | decoder.reader().read(&mut bytes)?; |
353 | 0 | Ok(match D::C::ENDIAN { |
354 | 0 | Endianness::Little => i64::from_le_bytes(bytes), |
355 | 0 | Endianness::Big => i64::from_be_bytes(bytes), |
356 | | } as isize) |
357 | | } |
358 | | } |
359 | 0 | } |
360 | | } |
361 | | impl_borrow_decode!(isize); |
362 | | |
363 | | impl<Context> Decode<Context> for NonZeroIsize { |
364 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
365 | 0 | NonZeroIsize::new(isize::decode(decoder)?).ok_or(DecodeError::NonZeroTypeIsZero { |
366 | 0 | non_zero_type: IntegerType::Isize, |
367 | 0 | }) |
368 | 0 | } |
369 | | } |
370 | | impl_borrow_decode!(NonZeroIsize); |
371 | | |
372 | | impl<Context> Decode<Context> for f32 { |
373 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
374 | 0 | decoder.claim_bytes_read(4)?; |
375 | 0 | let mut bytes = [0u8; 4]; |
376 | 0 | decoder.reader().read(&mut bytes)?; |
377 | 0 | Ok(match D::C::ENDIAN { |
378 | 0 | Endianness::Little => f32::from_le_bytes(bytes), |
379 | 0 | Endianness::Big => f32::from_be_bytes(bytes), |
380 | | }) |
381 | 0 | } |
382 | | } |
383 | | impl_borrow_decode!(f32); |
384 | | |
385 | | impl<Context> Decode<Context> for f64 { |
386 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
387 | 0 | decoder.claim_bytes_read(8)?; |
388 | 0 | let mut bytes = [0u8; 8]; |
389 | 0 | decoder.reader().read(&mut bytes)?; |
390 | 0 | Ok(match D::C::ENDIAN { |
391 | 0 | Endianness::Little => f64::from_le_bytes(bytes), |
392 | 0 | Endianness::Big => f64::from_be_bytes(bytes), |
393 | | }) |
394 | 0 | } |
395 | | } |
396 | | impl_borrow_decode!(f64); |
397 | | |
398 | | impl<Context, T: Decode<Context>> Decode<Context> for Wrapping<T> { |
399 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
400 | 0 | Ok(Wrapping(T::decode(decoder)?)) |
401 | 0 | } |
402 | | } |
403 | | impl<'de, Context, T: BorrowDecode<'de, Context>> BorrowDecode<'de, Context> for Wrapping<T> { |
404 | 0 | fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
405 | 0 | decoder: &mut D, |
406 | 0 | ) -> Result<Self, DecodeError> { |
407 | 0 | Ok(Wrapping(T::borrow_decode(decoder)?)) |
408 | 0 | } |
409 | | } |
410 | | |
411 | | impl<Context, T: Decode<Context>> Decode<Context> for Reverse<T> { |
412 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
413 | 0 | Ok(Reverse(T::decode(decoder)?)) |
414 | 0 | } |
415 | | } |
416 | | |
417 | | impl<'de, Context, T: BorrowDecode<'de, Context>> BorrowDecode<'de, Context> for Reverse<T> { |
418 | 0 | fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
419 | 0 | decoder: &mut D, |
420 | 0 | ) -> Result<Self, DecodeError> { |
421 | 0 | Ok(Reverse(T::borrow_decode(decoder)?)) |
422 | 0 | } |
423 | | } |
424 | | |
425 | | impl<Context> Decode<Context> for char { |
426 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
427 | 0 | let mut array = [0u8; 4]; |
428 | | |
429 | | // Look at the first byte to see how many bytes must be read |
430 | 0 | decoder.reader().read(&mut array[..1])?; |
431 | | |
432 | 0 | let width = utf8_char_width(array[0]); |
433 | 0 | if width == 0 { |
434 | 0 | return Err(DecodeError::InvalidCharEncoding(array)); |
435 | 0 | } |
436 | | // Normally we have to `.claim_bytes_read` before reading, however in this |
437 | | // case the amount of bytes read from `char` can vary wildly, and it should |
438 | | // only read up to 4 bytes too much. |
439 | 0 | decoder.claim_bytes_read(width)?; |
440 | 0 | if width == 1 { |
441 | 0 | return Ok(array[0] as char); |
442 | 0 | } |
443 | | |
444 | | // read the remaining pain |
445 | 0 | decoder.reader().read(&mut array[1..width])?; |
446 | 0 | let res = core::str::from_utf8(&array[..width]) |
447 | 0 | .ok() |
448 | 0 | .and_then(|s| s.chars().next()) |
449 | 0 | .ok_or(DecodeError::InvalidCharEncoding(array))?; |
450 | 0 | Ok(res) |
451 | 0 | } |
452 | | } |
453 | | impl_borrow_decode!(char); |
454 | | |
455 | | impl<'a, 'de: 'a, Context> BorrowDecode<'de, Context> for &'a [u8] { |
456 | 0 | fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
457 | 0 | decoder: &mut D, |
458 | 0 | ) -> Result<Self, DecodeError> { |
459 | 0 | let len = super::decode_slice_len(decoder)?; |
460 | 0 | decoder.claim_bytes_read(len)?; |
461 | 0 | decoder.borrow_reader().take_bytes(len) |
462 | 0 | } |
463 | | } |
464 | | |
465 | | impl<'a, 'de: 'a, Context> BorrowDecode<'de, Context> for &'a str { |
466 | 0 | fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
467 | 0 | decoder: &mut D, |
468 | 0 | ) -> Result<Self, DecodeError> { |
469 | 0 | let slice = <&[u8]>::borrow_decode(decoder)?; |
470 | 0 | core::str::from_utf8(slice).map_err(|inner| DecodeError::Utf8 { inner }) |
471 | 0 | } |
472 | | } |
473 | | |
474 | | impl<Context, T, const N: usize> Decode<Context> for [T; N] |
475 | | where |
476 | | T: Decode<Context>, |
477 | | { |
478 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
479 | 0 | decoder.claim_bytes_read(core::mem::size_of::<[T; N]>())?; |
480 | | |
481 | 0 | if unty::type_equal::<T, u8>() { |
482 | 0 | let mut buf = [0u8; N]; |
483 | 0 | decoder.reader().read(&mut buf)?; |
484 | 0 | let ptr = &mut buf as *mut _ as *mut [T; N]; |
485 | | |
486 | | // Safety: we know that T is a u8, so it is perfectly safe to |
487 | | // translate an array of u8 into an array of T |
488 | 0 | let res = unsafe { ptr.read() }; |
489 | 0 | Ok(res) |
490 | | } else { |
491 | 0 | let result = super::impl_core::collect_into_array(&mut (0..N).map(|_| { |
492 | | // See the documentation on `unclaim_bytes_read` as to why we're doing this here |
493 | 0 | decoder.unclaim_bytes_read(core::mem::size_of::<T>()); |
494 | 0 | T::decode(decoder) |
495 | 0 | })); |
496 | | |
497 | | // result is only None if N does not match the values of `(0..N)`, which it always should |
498 | | // So this unwrap should never occur |
499 | 0 | result.unwrap() |
500 | | } |
501 | 0 | } |
502 | | } |
503 | | |
504 | | impl<'de, T, const N: usize, Context> BorrowDecode<'de, Context> for [T; N] |
505 | | where |
506 | | T: BorrowDecode<'de, Context>, |
507 | | { |
508 | 0 | fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
509 | 0 | decoder: &mut D, |
510 | 0 | ) -> Result<Self, DecodeError> { |
511 | 0 | decoder.claim_bytes_read(core::mem::size_of::<[T; N]>())?; |
512 | | |
513 | 0 | if unty::type_equal::<T, u8>() { |
514 | 0 | let mut buf = [0u8; N]; |
515 | 0 | decoder.reader().read(&mut buf)?; |
516 | 0 | let ptr = &mut buf as *mut _ as *mut [T; N]; |
517 | | |
518 | | // Safety: we know that T is a u8, so it is perfectly safe to |
519 | | // translate an array of u8 into an array of T |
520 | 0 | let res = unsafe { ptr.read() }; |
521 | 0 | Ok(res) |
522 | | } else { |
523 | 0 | let result = super::impl_core::collect_into_array(&mut (0..N).map(|_| { |
524 | | // See the documentation on `unclaim_bytes_read` as to why we're doing this here |
525 | 0 | decoder.unclaim_bytes_read(core::mem::size_of::<T>()); |
526 | 0 | T::borrow_decode(decoder) |
527 | 0 | })); |
528 | | |
529 | | // result is only None if N does not match the values of `(0..N)`, which it always should |
530 | | // So this unwrap should never occur |
531 | 0 | result.unwrap() |
532 | | } |
533 | 0 | } |
534 | | } |
535 | | |
536 | | impl<Context> Decode<Context> for () { |
537 | 0 | fn decode<D: Decoder<Context = Context>>(_: &mut D) -> Result<Self, DecodeError> { |
538 | 0 | Ok(()) |
539 | 0 | } |
540 | | } |
541 | | impl_borrow_decode!(()); |
542 | | |
543 | | impl<Context, T> Decode<Context> for core::marker::PhantomData<T> { |
544 | 0 | fn decode<D: Decoder<Context = Context>>(_: &mut D) -> Result<Self, DecodeError> { |
545 | 0 | Ok(core::marker::PhantomData) |
546 | 0 | } |
547 | | } |
548 | | impl_borrow_decode!(core::marker::PhantomData<T>, T); |
549 | | |
550 | | impl<Context, T> Decode<Context> for Option<T> |
551 | | where |
552 | | T: Decode<Context>, |
553 | | { |
554 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
555 | 0 | match super::decode_option_variant(decoder, core::any::type_name::<Option<T>>())? { |
556 | | Some(_) => { |
557 | 0 | let val = T::decode(decoder)?; |
558 | 0 | Ok(Some(val)) |
559 | | } |
560 | 0 | None => Ok(None), |
561 | | } |
562 | 0 | } |
563 | | } |
564 | | |
565 | | impl<'de, T, Context> BorrowDecode<'de, Context> for Option<T> |
566 | | where |
567 | | T: BorrowDecode<'de, Context>, |
568 | | { |
569 | 0 | fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
570 | 0 | decoder: &mut D, |
571 | 0 | ) -> Result<Self, DecodeError> { |
572 | 0 | match super::decode_option_variant(decoder, core::any::type_name::<Option<T>>())? { |
573 | | Some(_) => { |
574 | 0 | let val = T::borrow_decode(decoder)?; |
575 | 0 | Ok(Some(val)) |
576 | | } |
577 | 0 | None => Ok(None), |
578 | | } |
579 | 0 | } |
580 | | } |
581 | | |
582 | | impl<Context, T, U> Decode<Context> for Result<T, U> |
583 | | where |
584 | | T: Decode<Context>, |
585 | | U: Decode<Context>, |
586 | | { |
587 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
588 | 0 | let is_ok = u32::decode(decoder)?; |
589 | 0 | match is_ok { |
590 | | 0 => { |
591 | 0 | let t = T::decode(decoder)?; |
592 | 0 | Ok(Ok(t)) |
593 | | } |
594 | | 1 => { |
595 | 0 | let u = U::decode(decoder)?; |
596 | 0 | Ok(Err(u)) |
597 | | } |
598 | 0 | x => Err(DecodeError::UnexpectedVariant { |
599 | 0 | found: x, |
600 | 0 | allowed: &crate::error::AllowedEnumVariants::Range { max: 1, min: 0 }, |
601 | 0 | type_name: core::any::type_name::<Result<T, U>>(), |
602 | 0 | }), |
603 | | } |
604 | 0 | } |
605 | | } |
606 | | |
607 | | impl<'de, T, U, Context> BorrowDecode<'de, Context> for Result<T, U> |
608 | | where |
609 | | T: BorrowDecode<'de, Context>, |
610 | | U: BorrowDecode<'de, Context>, |
611 | | { |
612 | 0 | fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
613 | 0 | decoder: &mut D, |
614 | 0 | ) -> Result<Self, DecodeError> { |
615 | 0 | let is_ok = u32::decode(decoder)?; |
616 | 0 | match is_ok { |
617 | | 0 => { |
618 | 0 | let t = T::borrow_decode(decoder)?; |
619 | 0 | Ok(Ok(t)) |
620 | | } |
621 | | 1 => { |
622 | 0 | let u = U::borrow_decode(decoder)?; |
623 | 0 | Ok(Err(u)) |
624 | | } |
625 | 0 | x => Err(DecodeError::UnexpectedVariant { |
626 | 0 | found: x, |
627 | 0 | allowed: &crate::error::AllowedEnumVariants::Range { max: 1, min: 0 }, |
628 | 0 | type_name: core::any::type_name::<Result<T, U>>(), |
629 | 0 | }), |
630 | | } |
631 | 0 | } |
632 | | } |
633 | | |
634 | | impl<Context, T> Decode<Context> for Cell<T> |
635 | | where |
636 | | T: Decode<Context>, |
637 | | { |
638 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
639 | 0 | let t = T::decode(decoder)?; |
640 | 0 | Ok(Cell::new(t)) |
641 | 0 | } |
642 | | } |
643 | | |
644 | | impl<'de, T, Context> BorrowDecode<'de, Context> for Cell<T> |
645 | | where |
646 | | T: BorrowDecode<'de, Context>, |
647 | | { |
648 | 0 | fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
649 | 0 | decoder: &mut D, |
650 | 0 | ) -> Result<Self, DecodeError> { |
651 | 0 | let t = T::borrow_decode(decoder)?; |
652 | 0 | Ok(Cell::new(t)) |
653 | 0 | } |
654 | | } |
655 | | |
656 | | impl<Context, T> Decode<Context> for RefCell<T> |
657 | | where |
658 | | T: Decode<Context>, |
659 | | { |
660 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
661 | 0 | let t = T::decode(decoder)?; |
662 | 0 | Ok(RefCell::new(t)) |
663 | 0 | } |
664 | | } |
665 | | |
666 | | impl<'de, T, Context> BorrowDecode<'de, Context> for RefCell<T> |
667 | | where |
668 | | T: BorrowDecode<'de, Context>, |
669 | | { |
670 | 0 | fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
671 | 0 | decoder: &mut D, |
672 | 0 | ) -> Result<Self, DecodeError> { |
673 | 0 | let t = T::borrow_decode(decoder)?; |
674 | 0 | Ok(RefCell::new(t)) |
675 | 0 | } |
676 | | } |
677 | | |
678 | | impl<Context> Decode<Context> for Duration { |
679 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
680 | | const NANOS_PER_SEC: u64 = 1_000_000_000; |
681 | 0 | let secs: u64 = Decode::decode(decoder)?; |
682 | 0 | let nanos: u32 = Decode::decode(decoder)?; |
683 | 0 | if secs.checked_add(u64::from(nanos) / NANOS_PER_SEC).is_none() { |
684 | 0 | return Err(DecodeError::InvalidDuration { secs, nanos }); |
685 | 0 | } |
686 | 0 | Ok(Duration::new(secs, nanos)) |
687 | 0 | } |
688 | | } |
689 | | impl_borrow_decode!(Duration); |
690 | | |
691 | | impl<Context, T> Decode<Context> for Range<T> |
692 | | where |
693 | | T: Decode<Context>, |
694 | | { |
695 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
696 | 0 | let min = T::decode(decoder)?; |
697 | 0 | let max = T::decode(decoder)?; |
698 | 0 | Ok(min..max) |
699 | 0 | } |
700 | | } |
701 | | impl<'de, T, Context> BorrowDecode<'de, Context> for Range<T> |
702 | | where |
703 | | T: BorrowDecode<'de, Context>, |
704 | | { |
705 | 0 | fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
706 | 0 | decoder: &mut D, |
707 | 0 | ) -> Result<Self, DecodeError> { |
708 | 0 | let min = T::borrow_decode(decoder)?; |
709 | 0 | let max = T::borrow_decode(decoder)?; |
710 | 0 | Ok(min..max) |
711 | 0 | } |
712 | | } |
713 | | |
714 | | impl<Context, T> Decode<Context> for RangeInclusive<T> |
715 | | where |
716 | | T: Decode<Context>, |
717 | | { |
718 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
719 | 0 | let min = T::decode(decoder)?; |
720 | 0 | let max = T::decode(decoder)?; |
721 | 0 | Ok(RangeInclusive::new(min, max)) |
722 | 0 | } |
723 | | } |
724 | | |
725 | | impl<'de, T, Context> BorrowDecode<'de, Context> for RangeInclusive<T> |
726 | | where |
727 | | T: BorrowDecode<'de, Context>, |
728 | | { |
729 | 0 | fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
730 | 0 | decoder: &mut D, |
731 | 0 | ) -> Result<Self, DecodeError> { |
732 | 0 | let min = T::borrow_decode(decoder)?; |
733 | 0 | let max = T::borrow_decode(decoder)?; |
734 | 0 | Ok(RangeInclusive::new(min, max)) |
735 | 0 | } |
736 | | } |
737 | | |
738 | | impl<T, Context> Decode<Context> for Bound<T> |
739 | | where |
740 | | T: Decode<Context>, |
741 | | { |
742 | 0 | fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> { |
743 | 0 | match u32::decode(decoder)? { |
744 | 0 | 0 => Ok(Bound::Unbounded), |
745 | 0 | 1 => Ok(Bound::Included(T::decode(decoder)?)), |
746 | 0 | 2 => Ok(Bound::Excluded(T::decode(decoder)?)), |
747 | 0 | x => Err(DecodeError::UnexpectedVariant { |
748 | 0 | allowed: &crate::error::AllowedEnumVariants::Range { max: 2, min: 0 }, |
749 | 0 | found: x, |
750 | 0 | type_name: core::any::type_name::<Bound<T>>(), |
751 | 0 | }), |
752 | | } |
753 | 0 | } |
754 | | } |
755 | | |
756 | | impl<'de, T, Context> BorrowDecode<'de, Context> for Bound<T> |
757 | | where |
758 | | T: BorrowDecode<'de, Context>, |
759 | | { |
760 | 0 | fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>( |
761 | 0 | decoder: &mut D, |
762 | 0 | ) -> Result<Self, DecodeError> { |
763 | 0 | match u32::decode(decoder)? { |
764 | 0 | 0 => Ok(Bound::Unbounded), |
765 | 0 | 1 => Ok(Bound::Included(T::borrow_decode(decoder)?)), |
766 | 0 | 2 => Ok(Bound::Excluded(T::borrow_decode(decoder)?)), |
767 | 0 | x => Err(DecodeError::UnexpectedVariant { |
768 | 0 | allowed: &crate::error::AllowedEnumVariants::Range { max: 2, min: 0 }, |
769 | 0 | found: x, |
770 | 0 | type_name: core::any::type_name::<Bound<T>>(), |
771 | 0 | }), |
772 | | } |
773 | 0 | } |
774 | | } |
775 | | |
776 | | const UTF8_CHAR_WIDTH: [u8; 256] = [ |
777 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
778 | | 1, // 0x1F |
779 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
780 | | 1, // 0x3F |
781 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
782 | | 1, // 0x5F |
783 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
784 | | 1, // 0x7F |
785 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
786 | | 0, // 0x9F |
787 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
788 | | 0, // 0xBF |
789 | | 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
790 | | 2, // 0xDF |
791 | | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xEF |
792 | | 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xFF |
793 | | ]; |
794 | | |
795 | | // This function is a copy of core::str::utf8_char_width |
796 | 0 | const fn utf8_char_width(b: u8) -> usize { |
797 | 0 | UTF8_CHAR_WIDTH[b as usize] as usize |
798 | 0 | } |