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::Format;
16use crate::config::IntEncoding;
17use crate::config::InternalLimitConfig;
18use crate::error::DecodeError;
19use crate::utils::Sealed;
20
21#[cfg(feature = "async-fiber")]
23pub mod async_fiber;
24pub mod bit_reader;
26pub(crate) mod cbor;
27pub(crate) mod fixed;
28pub mod read;
29
30pub use self::decoder::DecoderImpl;
31
32pub trait Decode<Context>: Sized {
123 fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError>;
129}
130
131pub trait BorrowDecode<'de, Context>: Sized {
137 fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>(
143 decoder: &mut D
144 ) -> Result<Self, DecodeError>;
145}
146
147#[macro_export]
149#[doc(hidden)]
150macro_rules! impl_borrow_decode {
151 ($ty:ty $(, $param:tt)*) => {
152 impl<'de $(, $param)*, __Context> $crate::BorrowDecode<'de, __Context> for $ty {
153 #[inline(always)]
154 fn borrow_decode<D: $crate::de::BorrowDecoder<'de, Context = __Context>>(
155 decoder: &mut D,
156 ) -> core::result::Result<Self, $crate::error::DecodeError> {
157 $crate::Decode::decode(decoder)
158 }
159 }
160 };
161}
162
163#[macro_export]
165#[doc(hidden)]
166macro_rules! impl_borrow_decode_with_context {
167 ($ty:ty, $context:ty $(, $param:tt)*) => {
168 impl<'de $(, $param)*> $crate::BorrowDecode<'de, $context> for $ty {
169 #[inline(always)]
170 fn borrow_decode<D: $crate::de::BorrowDecoder<'de, Context = $context>>(
171 decoder: &mut D,
172 ) -> core::result::Result<Self, $crate::error::DecodeError> {
173 $crate::Decode::decode(decoder)
174 }
175 }
176 };
177}
178
179#[inline(always)]
187pub(crate) fn claim_indefinite_item<T, D: Decoder + ?Sized>(
188 decoder: &mut D
189) -> Result<(), DecodeError> {
190 if core::mem::size_of::<T>() > 1 {
191 decoder.claim_container_read::<T>(1)
192 } else {
193 Ok(())
194 }
195}
196
197pub trait Decoder: Sealed + crate::error_path::BincodeErrorPathCovered<0> {
199 type R: Reader;
201
202 type C: Config;
204
205 type Context;
207
208 fn context(&mut self) -> &mut Self::Context;
210
211 #[inline(always)]
213 fn with_context<C>(
214 &mut self,
215 context: C,
216 ) -> WithContext<'_, Self, C> {
217 WithContext {
218 decoder: self,
219 context,
220 }
221 }
222
223 fn reader(&mut self) -> &mut Self::R;
225
226 fn config(&self) -> &Self::C;
228
229 fn claim_bytes_read(
236 &mut self,
237 n: usize,
238 ) -> Result<(), DecodeError>;
239
240 #[inline(always)]
247 fn claim_container_read<T>(
248 &mut self,
249 len: usize,
250 ) -> Result<(), DecodeError> {
251 Self::assert_covered();
252 if <Self::C as InternalLimitConfig>::LIMIT.is_some() {
253 len.checked_mul(core::mem::size_of::<T>()).map_or_else(
254 || crate::error::cold_decode_error_limit_exceeded(),
255 |val| self.claim_bytes_read(val),
256 )
257 } else {
258 Ok(())
259 }
260 }
261
262 fn unclaim_bytes_read(
329 &mut self,
330 n: usize,
331 );
332
333 #[inline(always)]
339 fn decode_u8(&mut self) -> Result<u8, DecodeError> {
340 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
341 | Format::Bincode | Format::BincodeDeterministic => self.reader().read_u8(),
342 | Format::Cbor | Format::CborDeterministic => cbor::decode_u8(self.reader()),
343 }
344 }
345
346 #[inline(always)]
352 fn decode_u16(&mut self) -> Result<u16, DecodeError> {
353 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
354 | Format::Bincode | Format::BincodeDeterministic => {
355 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
356 | IntEncoding::Variable => {
357 crate::varint::varint_decode_u16(
358 self.reader(),
359 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
360 )
361 },
362 | IntEncoding::Fixed => {
363 fixed::decode_u16(
364 self.reader(),
365 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
366 )
367 },
368 }
369 },
370 | Format::Cbor | Format::CborDeterministic => cbor::decode_u16(self.reader()),
371 }
372 }
373
374 #[inline(always)]
380 fn decode_u32(&mut self) -> Result<u32, DecodeError> {
381 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
382 | Format::Bincode | Format::BincodeDeterministic => {
383 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
384 | IntEncoding::Variable => {
385 crate::varint::varint_decode_u32(
386 self.reader(),
387 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
388 )
389 },
390 | IntEncoding::Fixed => {
391 fixed::decode_u32(
392 self.reader(),
393 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
394 )
395 },
396 }
397 },
398 | Format::Cbor | Format::CborDeterministic => cbor::decode_u32(self.reader()),
399 }
400 }
401
402 #[inline(always)]
408 fn decode_u64(&mut self) -> Result<u64, DecodeError> {
409 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
410 | Format::Bincode | Format::BincodeDeterministic => {
411 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
412 | IntEncoding::Variable => {
413 crate::varint::varint_decode_u64(
414 self.reader(),
415 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
416 )
417 },
418 | IntEncoding::Fixed => {
419 fixed::decode_u64(
420 self.reader(),
421 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
422 )
423 },
424 }
425 },
426 | Format::Cbor | Format::CborDeterministic => cbor::decode_u64(self.reader()),
427 }
428 }
429
430 #[inline(always)]
436 fn decode_u128(&mut self) -> Result<u128, DecodeError> {
437 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
438 | Format::Bincode | Format::BincodeDeterministic => {
439 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
440 | IntEncoding::Variable => {
441 crate::varint::varint_decode_u128(
442 self.reader(),
443 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
444 )
445 },
446 | IntEncoding::Fixed => {
447 fixed::decode_u128(
448 self.reader(),
449 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
450 )
451 },
452 }
453 },
454 | Format::Cbor | Format::CborDeterministic => cbor::decode_u128(self.reader()),
455 }
456 }
457
458 #[inline(always)]
464 fn decode_usize(&mut self) -> Result<usize, DecodeError> {
465 self.claim_bytes_read(8)?;
466 let v = self.decode_u64()?;
467 v.try_into()
468 .map_err(|_| crate::error::cold_decode_error_outside_usize_range::<()>(v).unwrap_err())
469 }
470
471 #[inline(always)]
477 fn decode_i8(&mut self) -> Result<i8, DecodeError> {
478 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
479 | Format::Bincode | Format::BincodeDeterministic => {
480 self.reader().read_u8().map(|v| v as i8)
481 },
482 | Format::Cbor | Format::CborDeterministic => cbor::decode_i8(self.reader()),
483 }
484 }
485
486 #[inline(always)]
492 fn decode_i16(&mut self) -> Result<i16, DecodeError> {
493 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
494 | Format::Bincode | Format::BincodeDeterministic => {
495 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
496 | IntEncoding::Variable => {
497 crate::varint::varint_decode_i16(
498 self.reader(),
499 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
500 )
501 },
502 | IntEncoding::Fixed => {
503 fixed::decode_i16(
504 self.reader(),
505 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
506 )
507 },
508 }
509 },
510 | Format::Cbor | Format::CborDeterministic => cbor::decode_i16(self.reader()),
511 }
512 }
513
514 #[inline(always)]
520 fn decode_i32(&mut self) -> Result<i32, DecodeError> {
521 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
522 | Format::Bincode | Format::BincodeDeterministic => {
523 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
524 | IntEncoding::Variable => {
525 crate::varint::varint_decode_i32(
526 self.reader(),
527 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
528 )
529 },
530 | IntEncoding::Fixed => {
531 fixed::decode_i32(
532 self.reader(),
533 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
534 )
535 },
536 }
537 },
538 | Format::Cbor | Format::CborDeterministic => cbor::decode_i32(self.reader()),
539 }
540 }
541
542 #[inline(always)]
548 fn decode_i64(&mut self) -> Result<i64, DecodeError> {
549 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
550 | Format::Bincode | Format::BincodeDeterministic => {
551 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
552 | IntEncoding::Variable => {
553 crate::varint::varint_decode_i64(
554 self.reader(),
555 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
556 )
557 },
558 | IntEncoding::Fixed => {
559 fixed::decode_i64(
560 self.reader(),
561 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
562 )
563 },
564 }
565 },
566 | Format::Cbor | Format::CborDeterministic => cbor::decode_i64(self.reader()),
567 }
568 }
569
570 #[inline(always)]
576 fn decode_i128(&mut self) -> Result<i128, DecodeError> {
577 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
578 | Format::Bincode | Format::BincodeDeterministic => {
579 match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
580 | IntEncoding::Variable => {
581 crate::varint::varint_decode_i128(
582 self.reader(),
583 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
584 )
585 },
586 | IntEncoding::Fixed => {
587 fixed::decode_i128(
588 self.reader(),
589 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
590 )
591 },
592 }
593 },
594 | Format::Cbor | Format::CborDeterministic => cbor::decode_i128(self.reader()),
595 }
596 }
597
598 #[inline(always)]
604 fn decode_isize(&mut self) -> Result<isize, DecodeError> {
605 self.claim_bytes_read(8)?;
606 let v = self.decode_i64()?;
607 v.try_into()
608 .map_err(|_| crate::error::cold_decode_error_outside_isize_range::<()>(v).unwrap_err())
609 }
610
611 #[inline(always)]
617 fn decode_f32(&mut self) -> Result<f32, DecodeError> {
618 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
619 | Format::Bincode | Format::BincodeDeterministic => {
620 fixed::decode_f32(
621 self.reader(),
622 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
623 )
624 },
625 | Format::Cbor | Format::CborDeterministic => cbor::decode_f32(self.reader()),
626 }
627 }
628
629 #[inline(always)]
635 fn decode_f64(&mut self) -> Result<f64, DecodeError> {
636 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
637 | Format::Bincode | Format::BincodeDeterministic => {
638 fixed::decode_f64(
639 self.reader(),
640 <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
641 )
642 },
643 | Format::Cbor | Format::CborDeterministic => cbor::decode_f64(self.reader()),
644 }
645 }
646
647 #[inline(always)]
653 fn decode_bool(&mut self) -> Result<bool, DecodeError> {
654 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
655 | Format::Bincode | Format::BincodeDeterministic => {
656 match self.reader().read_u8()? {
657 | 0 => Ok(false),
658 | 1 => Ok(true),
659 | x => crate::error::cold_decode_error_invalid_boolean_value(x),
660 }
661 },
662 | Format::Cbor | Format::CborDeterministic => cbor::decode_bool(self.reader()),
663 }
664 }
665
666 #[inline(always)]
672 fn decode_slice_len(&mut self) -> Result<usize, DecodeError> {
673 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
674 | Format::Bincode | Format::BincodeDeterministic => {
675 self.claim_bytes_read(8)?;
676 let v = self.decode_u64()?;
677 v.try_into().map_err(|_| {
678 crate::error::cold_decode_error_outside_usize_range::<()>(v).unwrap_err()
679 })
680 },
681 | Format::Cbor | Format::CborDeterministic => {
682 self.claim_bytes_read(9)?;
683 cbor::decode_slice_len(self.reader())
684 },
685 }
686 }
687
688 #[inline(always)]
694 fn decode_array_len(&mut self) -> Result<usize, DecodeError> {
695 self.decode_slice_len()
696 }
697
698 #[inline(always)]
704 fn decode_map_len(&mut self) -> Result<usize, DecodeError> {
705 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
706 | Format::Bincode | Format::BincodeDeterministic => self.decode_slice_len(),
707 | Format::Cbor | Format::CborDeterministic => {
708 self.claim_bytes_read(9)?;
709 cbor::decode_map_len(self.reader())
710 },
711 }
712 }
713
714 #[inline(always)]
720 fn decode_variant_index(&mut self) -> Result<u32, DecodeError> {
721 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
722 | Format::Bincode | Format::BincodeDeterministic => self.decode_u32(),
724 | Format::Cbor | Format::CborDeterministic => {
725 self.claim_bytes_read(5)?;
726 cbor::decode_u32(self.reader())
727 },
728 }
729 }
730
731 #[inline(always)]
737 fn decode_byte_slice_len(&mut self) -> Result<usize, DecodeError> {
738 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
739 | Format::Bincode | Format::BincodeDeterministic => self.decode_slice_len(),
740 | Format::Cbor | Format::CborDeterministic => {
741 self.claim_bytes_read(9)?;
742 cbor::decode_byte_slice_len(self.reader())
743 },
744 }
745 }
746
747 #[inline(always)]
753 fn decode_byte_slice_or_array_len(&mut self) -> Result<(u8, usize), DecodeError> {
754 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
755 | Format::Bincode | Format::BincodeDeterministic => {
756 self.decode_slice_len().map(|len| (0, len))
757 },
758 | Format::Cbor | Format::CborDeterministic => {
759 self.claim_bytes_read(9)?;
760 cbor::decode_byte_slice_or_array_len(self.reader())
761 },
762 }
763 }
764
765 #[inline(always)]
771 fn decode_str_len(&mut self) -> Result<usize, DecodeError> {
772 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
773 | Format::Bincode | Format::BincodeDeterministic => self.decode_slice_len(),
774 | Format::Cbor | Format::CborDeterministic => {
775 self.claim_bytes_read(9)?;
776 cbor::decode_str_len(self.reader())
777 },
778 }
779 }
780
781 #[inline(always)]
787 fn decode_struct_header(
788 &mut self,
789 _len: usize,
790 ) -> Result<(), DecodeError> {
791 match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
792 | Format::Bincode | Format::BincodeDeterministic => Ok(()),
793 | Format::Cbor | Format::CborDeterministic => {
794 let actual_len = cbor::decode_slice_len(self.reader())?;
795 if actual_len != _len && actual_len != usize::MAX {
796 return crate::error::cold_decode_error_other("struct length mismatch");
797 }
798 Ok(())
799 },
800 }
801 }
802}
803
804pub trait BorrowDecoder<'de>: Decoder {
808 type BR: BorrowReader<'de>;
810
811 fn borrow_reader(&mut self) -> &mut Self::BR;
813}
814
815impl<T> crate::error_path::BincodeErrorPathCovered<0> for &mut T where
816 T: crate::error_path::BincodeErrorPathCovered<0>
817{
818}
819
820impl<T> Decoder for &mut T
821where
822 T: Decoder,
823{
824 type C = T::C;
825 type Context = T::Context;
826 type R = T::R;
827
828 #[inline(always)]
829 fn reader(&mut self) -> &mut Self::R {
830 T::reader(self)
831 }
832
833 #[inline(always)]
834 fn config(&self) -> &Self::C {
835 T::config(self)
836 }
837
838 #[inline(always)]
839 fn claim_bytes_read(
840 &mut self,
841 n: usize,
842 ) -> Result<(), DecodeError> {
843 T::claim_bytes_read(self, n)
844 }
845
846 #[inline(always)]
847 fn unclaim_bytes_read(
848 &mut self,
849 n: usize,
850 ) {
851 T::unclaim_bytes_read(self, n);
852 }
853
854 #[inline(always)]
855 fn context(&mut self) -> &mut Self::Context {
856 T::context(self)
857 }
858
859 #[inline(always)]
860 fn decode_u8(&mut self) -> Result<u8, DecodeError> {
861 T::decode_u8(self)
862 }
863
864 #[inline(always)]
865 fn decode_u16(&mut self) -> Result<u16, DecodeError> {
866 T::decode_u16(self)
867 }
868
869 #[inline(always)]
870 fn decode_u32(&mut self) -> Result<u32, DecodeError> {
871 T::decode_u32(self)
872 }
873
874 #[inline(always)]
875 fn decode_u64(&mut self) -> Result<u64, DecodeError> {
876 T::decode_u64(self)
877 }
878
879 #[inline(always)]
880 fn decode_u128(&mut self) -> Result<u128, DecodeError> {
881 T::decode_u128(self)
882 }
883
884 #[inline(always)]
885 fn decode_usize(&mut self) -> Result<usize, DecodeError> {
886 T::decode_usize(self)
887 }
888
889 #[inline(always)]
890 fn decode_i8(&mut self) -> Result<i8, DecodeError> {
891 T::decode_i8(self)
892 }
893
894 #[inline(always)]
895 fn decode_i16(&mut self) -> Result<i16, DecodeError> {
896 T::decode_i16(self)
897 }
898
899 #[inline(always)]
900 fn decode_i32(&mut self) -> Result<i32, DecodeError> {
901 T::decode_i32(self)
902 }
903
904 #[inline(always)]
905 fn decode_i64(&mut self) -> Result<i64, DecodeError> {
906 T::decode_i64(self)
907 }
908
909 #[inline(always)]
910 fn decode_i128(&mut self) -> Result<i128, DecodeError> {
911 T::decode_i128(self)
912 }
913
914 #[inline(always)]
915 fn decode_isize(&mut self) -> Result<isize, DecodeError> {
916 T::decode_isize(self)
917 }
918
919 #[inline(always)]
920 fn decode_f32(&mut self) -> Result<f32, DecodeError> {
921 T::decode_f32(self)
922 }
923
924 #[inline(always)]
925 fn decode_f64(&mut self) -> Result<f64, DecodeError> {
926 T::decode_f64(self)
927 }
928
929 #[inline(always)]
930 fn decode_bool(&mut self) -> Result<bool, DecodeError> {
931 T::decode_bool(self)
932 }
933
934 #[inline(always)]
935 fn decode_slice_len(&mut self) -> Result<usize, DecodeError> {
936 T::decode_slice_len(self)
937 }
938
939 #[inline(always)]
940 fn decode_array_len(&mut self) -> Result<usize, DecodeError> {
941 T::decode_array_len(self)
942 }
943
944 #[inline(always)]
945 fn decode_map_len(&mut self) -> Result<usize, DecodeError> {
946 T::decode_map_len(self)
947 }
948
949 #[inline(always)]
950 fn decode_variant_index(&mut self) -> Result<u32, DecodeError> {
951 T::decode_variant_index(self)
952 }
953
954 #[inline(always)]
955 fn decode_byte_slice_len(&mut self) -> Result<usize, DecodeError> {
956 T::decode_byte_slice_len(self)
957 }
958
959 #[inline(always)]
960 fn decode_str_len(&mut self) -> Result<usize, DecodeError> {
961 T::decode_str_len(self)
962 }
963
964 #[inline(always)]
965 fn decode_struct_header(
966 &mut self,
967 len: usize,
968 ) -> Result<(), DecodeError> {
969 T::decode_struct_header(self, len)
970 }
971}
972
973impl<'de, T> BorrowDecoder<'de> for &mut T
974where
975 T: BorrowDecoder<'de>,
976{
977 type BR = T::BR;
978
979 #[inline(always)]
980 fn borrow_reader(&mut self) -> &mut Self::BR {
981 T::borrow_reader(self)
982 }
983}
984
985#[inline(always)]
991pub(crate) fn decode_option_variant<D: Decoder>(
992 decoder: &mut D,
993 type_name: &'static str,
994) -> Result<Option<()>, DecodeError> {
995 D::assert_covered();
996 let is_some = u8::decode(decoder)?;
997 match is_some {
998 | 0 => Ok(None),
999 | 1 => Ok(Some(())),
1000 | x => {
1001 crate::error::cold_decode_error_unexpected_variant(
1002 type_name,
1003 &crate::error::AllowedEnumVariants::Range { max: 1, min: 0 },
1004 u32::from(x),
1005 )
1006 },
1007 }
1008}
1009
1010#[inline(always)]
1016#[allow(dead_code)]
1017pub(crate) fn decode_slice_len<D: Decoder>(decoder: &mut D) -> Result<usize, DecodeError> {
1018 D::assert_covered();
1019 decoder.decode_slice_len()
1020}