1#![allow(clippy::used_underscore_binding)]
3
4mod decoder;
5#[doc(hidden)]
6pub(crate) mod deterministic;
7mod impl_core;
8mod impl_tuples;
9mod impls;
10
11use self::decoder::WithContext;
12use self::read::BorrowReader;
13use self::read::Reader;
14use crate::config::Config;
15use crate::config::Endianness;
16use crate::config::Format;
17use crate::config::IntEncoding;
18use crate::config::InternalLimitConfig;
19use crate::error::DecodeError;
20use crate::utils::Sealed;
21
22#[cfg(feature = "async-fiber")]
24pub mod async_fiber;
25pub mod bit_reader;
27pub(crate) mod cbor;
28pub mod read;
29
30pub use self::decoder::DecoderImpl;
31
32pub trait Decode<Context>: Sized {
115 fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError>;
121}
122
123pub trait BorrowDecode<'de, Context>: Sized {
129 fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>(
135 decoder: &mut D
136 ) -> Result<Self, DecodeError>;
137}
138
139#[macro_export]
141#[doc(hidden)]
142macro_rules! impl_borrow_decode {
143 ($ty:ty $(, $param:tt)*) => {
144 impl<'de $(, $param)*, __Context> $crate::BorrowDecode<'de, __Context> for $ty {
145 #[inline(always)]
146 fn borrow_decode<D: $crate::de::BorrowDecoder<'de, Context = __Context>>(
147 decoder: &mut D,
148 ) -> core::result::Result<Self, $crate::error::DecodeError> {
149 $crate::Decode::decode(decoder)
150 }
151 }
152 };
153}
154
155#[macro_export]
157#[doc(hidden)]
158macro_rules! impl_borrow_decode_with_context {
159 ($ty:ty, $context:ty $(, $param:tt)*) => {
160 impl<'de $(, $param)*> $crate::BorrowDecode<'de, $context> for $ty {
161 #[inline(always)]
162 fn borrow_decode<D: $crate::de::BorrowDecoder<'de, Context = $context>>(
163 decoder: &mut D,
164 ) -> core::result::Result<Self, $crate::error::DecodeError> {
165 $crate::Decode::decode(decoder)
166 }
167 }
168 };
169}
170
171pub trait Decoder: Sealed + crate::error_path::BincodeErrorPathCovered<0> {
173 type R: Reader;
175
176 type C: Config;
178
179 type Context;
181
182 fn context(&mut self) -> &mut Self::Context;
184
185 #[inline(always)]
187 fn with_context<C>(
188 &mut self,
189 context: C,
190 ) -> WithContext<'_, Self, C> {
191 WithContext {
192 decoder: self,
193 context,
194 }
195 }
196
197 fn reader(&mut self) -> &mut Self::R;
199
200 fn config(&self) -> &Self::C;
202
203 fn claim_bytes_read(
210 &mut self,
211 n: usize,
212 ) -> Result<(), DecodeError>;
213
214 #[inline(always)]
221 fn claim_container_read<T>(
222 &mut self,
223 len: usize,
224 ) -> Result<(), DecodeError> {
225 Self::assert_covered();
226 if <Self::C as InternalLimitConfig>::LIMIT.is_some() {
227 len.checked_mul(core::mem::size_of::<T>()).map_or_else(
228 || crate::error::cold_decode_error_limit_exceeded(),
229 |val| self.claim_bytes_read(val),
230 )
231 } else {
232 Ok(())
233 }
234 }
235
236 fn unclaim_bytes_read(
303 &mut self,
304 n: usize,
305 );
306
307 #[inline(always)]
313 fn decode_u8(&mut self) -> Result<u8, DecodeError> {
314 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
315 | Format::Bincode | Format::BincodeDeterministic => self.reader().read_u8(),
316 | Format::Cbor | Format::CborDeterministic => cbor::decode_u8(self.reader()),
317 }
318 }
319
320 #[inline(always)]
326 fn decode_u16(&mut self) -> Result<u16, DecodeError> {
327 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
328 | Format::Bincode | Format::BincodeDeterministic => {
329 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
330 | IntEncoding::Variable => {
331 crate::varint::varint_decode_u16(
332 self.reader(),
333 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
334 )
335 },
336 | IntEncoding::Fixed => {
337 let val = self.reader().read_u16()?;
338 match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
339 | Endianness::Big => Ok(u16::from_be(val)),
340 | Endianness::Little => Ok(u16::from_le(val)),
341 }
342 },
343 }
344 },
345 | Format::Cbor | Format::CborDeterministic => cbor::decode_u16(self.reader()),
346 }
347 }
348
349 #[inline(always)]
355 fn decode_u32(&mut self) -> Result<u32, DecodeError> {
356 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
357 | Format::Bincode | Format::BincodeDeterministic => {
358 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
359 | IntEncoding::Variable => {
360 crate::varint::varint_decode_u32(
361 self.reader(),
362 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
363 )
364 },
365 | IntEncoding::Fixed => {
366 let val = self.reader().read_u32()?;
367 match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
368 | Endianness::Big => Ok(u32::from_be(val)),
369 | Endianness::Little => Ok(u32::from_le(val)),
370 }
371 },
372 }
373 },
374 | Format::Cbor | Format::CborDeterministic => cbor::decode_u32(self.reader()),
375 }
376 }
377
378 #[inline(always)]
384 fn decode_u64(&mut self) -> Result<u64, DecodeError> {
385 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
386 | Format::Bincode | Format::BincodeDeterministic => {
387 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
388 | IntEncoding::Variable => {
389 crate::varint::varint_decode_u64(
390 self.reader(),
391 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
392 )
393 },
394 | IntEncoding::Fixed => {
395 let val = self.reader().read_u64()?;
396 match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
397 | Endianness::Big => Ok(u64::from_be(val)),
398 | Endianness::Little => Ok(u64::from_le(val)),
399 }
400 },
401 }
402 },
403 | Format::Cbor | Format::CborDeterministic => cbor::decode_u64(self.reader()),
404 }
405 }
406
407 #[inline(always)]
413 fn decode_u128(&mut self) -> Result<u128, DecodeError> {
414 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
415 | Format::Bincode | Format::BincodeDeterministic => {
416 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
417 | IntEncoding::Variable => {
418 crate::varint::varint_decode_u128(
419 self.reader(),
420 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
421 )
422 },
423 | IntEncoding::Fixed => {
424 let val = self.reader().read_u128()?;
425 match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
426 | Endianness::Big => Ok(u128::from_be(val)),
427 | Endianness::Little => Ok(u128::from_le(val)),
428 }
429 },
430 }
431 },
432 | Format::Cbor | Format::CborDeterministic => cbor::decode_u128(self.reader()),
433 }
434 }
435
436 #[inline(always)]
442 fn decode_usize(&mut self) -> Result<usize, DecodeError> {
443 self.claim_bytes_read(8)?;
444 let v = self.decode_u64()?;
445 v.try_into()
446 .map_err(|_| crate::error::cold_decode_error_outside_usize_range::<()>(v).unwrap_err())
447 }
448
449 #[inline(always)]
455 fn decode_i8(&mut self) -> Result<i8, DecodeError> {
456 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
457 | Format::Bincode | Format::BincodeDeterministic => {
458 self.reader().read_u8().map(|v| v as i8)
459 },
460 | Format::Cbor | Format::CborDeterministic => cbor::decode_i8(self.reader()),
461 }
462 }
463
464 #[inline(always)]
470 fn decode_i16(&mut self) -> Result<i16, DecodeError> {
471 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
472 | Format::Bincode | Format::BincodeDeterministic => {
473 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
474 | IntEncoding::Variable => {
475 crate::varint::varint_decode_i16(
476 self.reader(),
477 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
478 )
479 },
480 | IntEncoding::Fixed => {
481 let val = self.reader().read_u16()?;
482 match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
483 | Endianness::Big => Ok(u16::from_be(val) as i16),
484 | Endianness::Little => Ok(u16::from_le(val) as i16),
485 }
486 },
487 }
488 },
489 | Format::Cbor | Format::CborDeterministic => cbor::decode_i16(self.reader()),
490 }
491 }
492
493 #[inline(always)]
499 fn decode_i32(&mut self) -> Result<i32, DecodeError> {
500 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
501 | Format::Bincode | Format::BincodeDeterministic => {
502 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
503 | IntEncoding::Variable => {
504 crate::varint::varint_decode_i32(
505 self.reader(),
506 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
507 )
508 },
509 | IntEncoding::Fixed => {
510 let val = self.reader().read_u32()?;
511 match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
512 | Endianness::Big => Ok(u32::from_be(val) as i32),
513 | Endianness::Little => Ok(u32::from_le(val) as i32),
514 }
515 },
516 }
517 },
518 | Format::Cbor | Format::CborDeterministic => cbor::decode_i32(self.reader()),
519 }
520 }
521
522 #[inline(always)]
528 fn decode_i64(&mut self) -> Result<i64, DecodeError> {
529 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
530 | Format::Bincode | Format::BincodeDeterministic => {
531 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
532 | IntEncoding::Variable => {
533 crate::varint::varint_decode_i64(
534 self.reader(),
535 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
536 )
537 },
538 | IntEncoding::Fixed => {
539 let val = self.reader().read_u64()?;
540 match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
541 | Endianness::Big => Ok(u64::from_be(val) as i64),
542 | Endianness::Little => Ok(u64::from_le(val) as i64),
543 }
544 },
545 }
546 },
547 | Format::Cbor | Format::CborDeterministic => cbor::decode_i64(self.reader()),
548 }
549 }
550
551 #[inline(always)]
557 fn decode_i128(&mut self) -> Result<i128, DecodeError> {
558 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
559 | Format::Bincode | Format::BincodeDeterministic => {
560 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
561 | IntEncoding::Variable => {
562 crate::varint::varint_decode_i128(
563 self.reader(),
564 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
565 )
566 },
567 | IntEncoding::Fixed => {
568 let val = self.reader().read_u128()?;
569 match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
570 | Endianness::Big => Ok(u128::from_be(val) as i128),
571 | Endianness::Little => Ok(u128::from_le(val) as i128),
572 }
573 },
574 }
575 },
576 | Format::Cbor | Format::CborDeterministic => cbor::decode_i128(self.reader()),
577 }
578 }
579
580 #[inline(always)]
586 fn decode_isize(&mut self) -> Result<isize, DecodeError> {
587 self.claim_bytes_read(8)?;
588 let v = self.decode_i64()?;
589 v.try_into()
590 .map_err(|_| crate::error::cold_decode_error_outside_isize_range::<()>(v).unwrap_err())
591 }
592
593 #[inline(always)]
599 fn decode_f32(&mut self) -> Result<f32, DecodeError> {
600 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
601 | Format::Bincode | Format::BincodeDeterministic => {
602 let val = self.reader().read_u32()?;
603 Ok(f32::from_bits(
604 match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
605 | Endianness::Big => u32::from_be(val),
606 | Endianness::Little => u32::from_le(val),
607 },
608 ))
609 },
610 | Format::Cbor | Format::CborDeterministic => cbor::decode_f32(self.reader()),
611 }
612 }
613
614 #[inline(always)]
620 fn decode_f64(&mut self) -> Result<f64, DecodeError> {
621 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
622 | Format::Bincode | Format::BincodeDeterministic => {
623 let val = self.reader().read_u64()?;
624 Ok(f64::from_bits(
625 match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
626 | Endianness::Big => u64::from_be(val),
627 | Endianness::Little => u64::from_le(val),
628 },
629 ))
630 },
631 | Format::Cbor | Format::CborDeterministic => cbor::decode_f64(self.reader()),
632 }
633 }
634
635 #[inline(always)]
641 fn decode_bool(&mut self) -> Result<bool, DecodeError> {
642 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
643 | Format::Bincode | Format::BincodeDeterministic => {
644 match self.reader().read_u8()? {
645 | 0 => Ok(false),
646 | 1 => Ok(true),
647 | x => crate::error::cold_decode_error_invalid_boolean_value(x),
648 }
649 },
650 | Format::Cbor | Format::CborDeterministic => cbor::decode_bool(self.reader()),
651 }
652 }
653
654 #[inline(always)]
660 fn decode_slice_len(&mut self) -> Result<usize, DecodeError> {
661 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
662 | Format::Bincode | Format::BincodeDeterministic => {
663 self.claim_bytes_read(8)?;
664 let v = self.decode_u64()?;
665 v.try_into().map_err(|_| {
666 crate::error::cold_decode_error_outside_usize_range::<()>(v).unwrap_err()
667 })
668 },
669 | Format::Cbor | Format::CborDeterministic => {
670 self.claim_bytes_read(9)?;
671 cbor::decode_slice_len(self.reader())
672 },
673 }
674 }
675
676 #[inline(always)]
682 fn decode_array_len(&mut self) -> Result<usize, DecodeError> {
683 self.decode_slice_len()
684 }
685
686 #[inline(always)]
692 fn decode_map_len(&mut self) -> Result<usize, DecodeError> {
693 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
694 | Format::Bincode | Format::BincodeDeterministic => self.decode_slice_len(),
695 | Format::Cbor | Format::CborDeterministic => {
696 self.claim_bytes_read(9)?;
697 cbor::decode_map_len(self.reader())
698 },
699 }
700 }
701
702 #[inline(always)]
708 fn decode_variant_index(&mut self) -> Result<u32, DecodeError> {
709 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
710 | Format::Bincode | Format::BincodeDeterministic => {
711 self.claim_bytes_read(1)?;
712 self.decode_u8().map(u32::from)
713 },
714 | Format::Cbor | Format::CborDeterministic => {
715 self.claim_bytes_read(5)?;
716 cbor::decode_u32(self.reader())
717 },
718 }
719 }
720
721 #[inline(always)]
727 fn decode_byte_slice_len(&mut self) -> Result<usize, DecodeError> {
728 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
729 | Format::Bincode | Format::BincodeDeterministic => self.decode_slice_len(),
730 | Format::Cbor | Format::CborDeterministic => {
731 self.claim_bytes_read(9)?;
732 cbor::decode_byte_slice_len(self.reader())
733 },
734 }
735 }
736
737 #[inline(always)]
743 fn decode_byte_slice_or_array_len(&mut self) -> Result<(u8, usize), DecodeError> {
744 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
745 | Format::Bincode | Format::BincodeDeterministic => {
746 self.decode_slice_len().map(|len| (0, len))
747 },
748 | Format::Cbor | Format::CborDeterministic => {
749 self.claim_bytes_read(9)?;
750 cbor::decode_byte_slice_or_array_len(self.reader())
751 },
752 }
753 }
754
755 #[inline(always)]
761 fn decode_str_len(&mut self) -> Result<usize, DecodeError> {
762 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
763 | Format::Bincode | Format::BincodeDeterministic => self.decode_slice_len(),
764 | Format::Cbor | Format::CborDeterministic => {
765 self.claim_bytes_read(9)?;
766 cbor::decode_str_len(self.reader())
767 },
768 }
769 }
770
771 #[inline(always)]
777 fn decode_struct_header(
778 &mut self,
779 _len: usize,
780 ) -> Result<(), DecodeError> {
781 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
782 | Format::Bincode | Format::BincodeDeterministic => Ok(()),
783 | Format::Cbor | Format::CborDeterministic => {
784 let actual_len = cbor::decode_slice_len(self.reader())?;
785 if actual_len != _len && actual_len != usize::MAX {
786 return Err(DecodeError::Other("struct length mismatch"));
787 }
788 Ok(())
789 },
790 }
791 }
792}
793
794pub trait BorrowDecoder<'de>: Decoder {
798 type BR: BorrowReader<'de>;
800
801 fn borrow_reader(&mut self) -> &mut Self::BR;
803}
804
805impl<T> crate::error_path::BincodeErrorPathCovered<0> for &mut T where
806 T: crate::error_path::BincodeErrorPathCovered<0>
807{
808}
809
810impl<T> Decoder for &mut T
811where
812 T: Decoder,
813{
814 type C = T::C;
815 type Context = T::Context;
816 type R = T::R;
817
818 #[inline(always)]
819 fn reader(&mut self) -> &mut Self::R {
820 T::reader(self)
821 }
822
823 #[inline(always)]
824 fn config(&self) -> &Self::C {
825 T::config(self)
826 }
827
828 #[inline(always)]
829 fn claim_bytes_read(
830 &mut self,
831 n: usize,
832 ) -> Result<(), DecodeError> {
833 T::claim_bytes_read(self, n)
834 }
835
836 #[inline(always)]
837 fn unclaim_bytes_read(
838 &mut self,
839 n: usize,
840 ) {
841 T::unclaim_bytes_read(self, n);
842 }
843
844 #[inline(always)]
845 fn context(&mut self) -> &mut Self::Context {
846 T::context(self)
847 }
848
849 #[inline(always)]
850 fn decode_u8(&mut self) -> Result<u8, DecodeError> {
851 T::decode_u8(self)
852 }
853
854 #[inline(always)]
855 fn decode_u16(&mut self) -> Result<u16, DecodeError> {
856 T::decode_u16(self)
857 }
858
859 #[inline(always)]
860 fn decode_u32(&mut self) -> Result<u32, DecodeError> {
861 T::decode_u32(self)
862 }
863
864 #[inline(always)]
865 fn decode_u64(&mut self) -> Result<u64, DecodeError> {
866 T::decode_u64(self)
867 }
868
869 #[inline(always)]
870 fn decode_u128(&mut self) -> Result<u128, DecodeError> {
871 T::decode_u128(self)
872 }
873
874 #[inline(always)]
875 fn decode_usize(&mut self) -> Result<usize, DecodeError> {
876 T::decode_usize(self)
877 }
878
879 #[inline(always)]
880 fn decode_i8(&mut self) -> Result<i8, DecodeError> {
881 T::decode_i8(self)
882 }
883
884 #[inline(always)]
885 fn decode_i16(&mut self) -> Result<i16, DecodeError> {
886 T::decode_i16(self)
887 }
888
889 #[inline(always)]
890 fn decode_i32(&mut self) -> Result<i32, DecodeError> {
891 T::decode_i32(self)
892 }
893
894 #[inline(always)]
895 fn decode_i64(&mut self) -> Result<i64, DecodeError> {
896 T::decode_i64(self)
897 }
898
899 #[inline(always)]
900 fn decode_i128(&mut self) -> Result<i128, DecodeError> {
901 T::decode_i128(self)
902 }
903
904 #[inline(always)]
905 fn decode_isize(&mut self) -> Result<isize, DecodeError> {
906 T::decode_isize(self)
907 }
908
909 #[inline(always)]
910 fn decode_f32(&mut self) -> Result<f32, DecodeError> {
911 T::decode_f32(self)
912 }
913
914 #[inline(always)]
915 fn decode_f64(&mut self) -> Result<f64, DecodeError> {
916 T::decode_f64(self)
917 }
918
919 #[inline(always)]
920 fn decode_bool(&mut self) -> Result<bool, DecodeError> {
921 T::decode_bool(self)
922 }
923
924 #[inline(always)]
925 fn decode_slice_len(&mut self) -> Result<usize, DecodeError> {
926 T::decode_slice_len(self)
927 }
928
929 #[inline(always)]
930 fn decode_array_len(&mut self) -> Result<usize, DecodeError> {
931 T::decode_array_len(self)
932 }
933
934 #[inline(always)]
935 fn decode_map_len(&mut self) -> Result<usize, DecodeError> {
936 T::decode_map_len(self)
937 }
938
939 #[inline(always)]
940 fn decode_variant_index(&mut self) -> Result<u32, DecodeError> {
941 T::decode_variant_index(self)
942 }
943
944 #[inline(always)]
945 fn decode_byte_slice_len(&mut self) -> Result<usize, DecodeError> {
946 T::decode_byte_slice_len(self)
947 }
948
949 #[inline(always)]
950 fn decode_str_len(&mut self) -> Result<usize, DecodeError> {
951 T::decode_str_len(self)
952 }
953
954 #[inline(always)]
955 fn decode_struct_header(
956 &mut self,
957 len: usize,
958 ) -> Result<(), DecodeError> {
959 T::decode_struct_header(self, len)
960 }
961}
962
963impl<'de, T> BorrowDecoder<'de> for &mut T
964where
965 T: BorrowDecoder<'de>,
966{
967 type BR = T::BR;
968
969 #[inline(always)]
970 fn borrow_reader(&mut self) -> &mut Self::BR {
971 T::borrow_reader(self)
972 }
973}
974
975#[inline(always)]
981pub(crate) fn decode_option_variant<D: Decoder>(
982 decoder: &mut D,
983 type_name: &'static str,
984) -> Result<Option<()>, DecodeError> {
985 D::assert_covered();
986 let is_some = u8::decode(decoder)?;
987 match is_some {
988 | 0 => Ok(None),
989 | 1 => Ok(Some(())),
990 | x => {
991 crate::error::cold_decode_error_unexpected_variant(
992 type_name,
993 &crate::error::AllowedEnumVariants::Range { max: 1, min: 0 },
994 u32::from(x),
995 )
996 },
997 }
998}
999
1000#[inline(always)]
1006#[allow(dead_code)]
1007pub(crate) fn decode_slice_len<D: Decoder>(decoder: &mut D) -> Result<usize, DecodeError> {
1008 D::assert_covered();
1009 decoder.decode_slice_len()
1010}