musli_core/impls/
mod.rs

1#[cfg(feature = "alloc")]
2#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
3mod alloc;
4#[cfg(feature = "std")]
5#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
6mod net;
7mod range;
8mod tuples;
9
10#[cfg(feature = "std")]
11use core::any::TypeId;
12use core::ffi::CStr;
13use core::num::{
14    NonZeroI8, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI128, NonZeroIsize, NonZeroU8,
15    NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize, Saturating, Wrapping,
16};
17use core::{fmt, marker};
18
19use crate::de::{
20    Decode, DecodeBytes, DecodePacked, DecodeUnsized, DecodeUnsizedBytes, Decoder, SequenceDecoder,
21    UnsizedVisitor, VariantDecoder,
22};
23use crate::en::{Encode, EncodeBytes, EncodePacked, Encoder, SequenceEncoder, VariantEncoder};
24#[cfg(feature = "std")]
25use crate::mode::Text;
26use crate::{Allocator, Context};
27
28/// Platform tag used by certain platform-specific implementations.
29#[cfg(feature = "std")]
30enum PlatformTag {
31    Unix,
32    Windows,
33}
34
35#[cfg(feature = "std")]
36impl<M> Encode<M> for PlatformTag
37where
38    M: 'static,
39{
40    type Encode = Self;
41
42    const IS_BITWISE_ENCODE: bool = false;
43
44    #[inline]
45    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
46    where
47        E: Encoder<Mode = M>,
48    {
49        if TypeId::of::<M>() == TypeId::of::<Text>() {
50            match self {
51                PlatformTag::Unix => encoder.encode("unix"),
52                PlatformTag::Windows => encoder.encode("windows"),
53            }
54        } else {
55            // For binary encoding, we use the tag as a single byte.
56            let tag = match self {
57                PlatformTag::Unix => 0,
58                PlatformTag::Windows => 1,
59            };
60
61            encoder.encode_u8(tag)
62        }
63    }
64
65    #[inline]
66    fn as_encode(&self) -> &Self::Encode {
67        self
68    }
69}
70
71#[cfg(feature = "std")]
72impl<'de, M, A> Decode<'de, M, A> for PlatformTag
73where
74    M: 'static,
75    A: Allocator,
76{
77    // Unit is always packed, since it is a ZST.
78    const IS_BITWISE_DECODE: bool = true;
79
80    #[inline]
81    fn decode<D>(decoder: D) -> Result<Self, D::Error>
82    where
83        D: Decoder<'de, Allocator = A>,
84    {
85        let cx = decoder.cx();
86
87        if TypeId::of::<M>() == TypeId::of::<Text>() {
88            decoder.decode_unsized(|value: &str| match value {
89                "unix" => Ok(PlatformTag::Unix),
90                "windows" => Ok(PlatformTag::Windows),
91                _ => Err(cx.message(format_args!("Unsupported platform tag `{value}`",))),
92            })
93        } else {
94            match decoder.decode_u8()? {
95                0 => Ok(PlatformTag::Unix),
96                1 => Ok(PlatformTag::Windows),
97                _ => Err(cx.message("Unsupported platform tag")),
98            }
99        }
100    }
101}
102
103impl<M> Encode<M> for () {
104    type Encode = Self;
105
106    // Unit is always packed, since it is a ZST.
107    const IS_BITWISE_ENCODE: bool = true;
108
109    #[inline]
110    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
111    where
112        E: Encoder,
113    {
114        encoder.encode_empty()
115    }
116
117    #[inline]
118    fn as_encode(&self) -> &Self::Encode {
119        self
120    }
121}
122
123impl<'de, M, A> Decode<'de, M, A> for ()
124where
125    A: Allocator,
126{
127    // Unit is always packed, since it is a ZST.
128    const IS_BITWISE_DECODE: bool = true;
129
130    #[inline]
131    fn decode<D>(decoder: D) -> Result<Self, D::Error>
132    where
133        D: Decoder<'de, Allocator = A>,
134    {
135        decoder.decode_empty()
136    }
137}
138
139impl<T, M> Encode<M> for marker::PhantomData<T> {
140    type Encode = Self;
141
142    // PhantomData is always packed, since it is a ZST.
143    const IS_BITWISE_ENCODE: bool = true;
144
145    #[inline]
146    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
147    where
148        E: Encoder,
149    {
150        encoder.encode_empty()
151    }
152
153    #[inline]
154    fn as_encode(&self) -> &Self::Encode {
155        self
156    }
157}
158
159impl<'de, M, A, T> Decode<'de, M, A> for marker::PhantomData<T>
160where
161    A: Allocator,
162{
163    // PhantomData is always packed, since it is a ZST.
164    const IS_BITWISE_DECODE: bool = true;
165
166    #[inline]
167    fn decode<D>(decoder: D) -> Result<Self, D::Error>
168    where
169        D: Decoder<'de>,
170    {
171        decoder.decode_empty()?;
172        Ok(marker::PhantomData)
173    }
174}
175
176macro_rules! atomic_impl {
177    ($size:literal $(, $ty:ident)*) => {
178        $(
179            #[cfg(target_has_atomic = $size)]
180            impl<'de, M, A> Decode<'de, M, A> for core::sync::atomic::$ty
181            where
182                A: Allocator
183            {
184                const IS_BITWISE_DECODE: bool = true;
185
186                fn decode<D>(decoder: D) -> Result<Self, D::Error>
187                where
188                    D: Decoder<'de>,
189                {
190                    decoder.decode().map(Self::new)
191                }
192            }
193
194            #[cfg(target_has_atomic = $size)]
195            impl<M> Encode<M> for core::sync::atomic::$ty {
196                const IS_BITWISE_ENCODE: bool = false;
197
198                type Encode = Self;
199
200                #[inline]
201                fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
202                where
203                    E: Encoder,
204                {
205                    use core::sync::atomic::Ordering::Relaxed;
206
207                    self.load(Relaxed).encode(encoder)
208                }
209
210                #[inline]
211                fn as_encode(&self) -> &Self::Encode {
212                    self
213                }
214            }
215        )*
216    };
217}
218
219atomic_impl!("8", AtomicBool, AtomicI8, AtomicU8);
220atomic_impl!("16", AtomicI16, AtomicU16);
221atomic_impl!("32", AtomicI32, AtomicU32);
222atomic_impl!("64", AtomicI64, AtomicU64);
223atomic_impl!("ptr", AtomicIsize, AtomicUsize);
224
225macro_rules! non_zero {
226    ($ty:ty) => {
227        impl<M> Encode<M> for $ty {
228            const IS_BITWISE_ENCODE: bool = true;
229
230            type Encode = Self;
231
232            #[inline]
233            fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
234            where
235                E: Encoder,
236            {
237                self.get().encode(encoder)
238            }
239
240            #[inline]
241            fn as_encode(&self) -> &Self::Encode {
242                self
243            }
244        }
245
246        impl<'de, M, A> Decode<'de, M, A> for $ty
247        where
248            A: Allocator,
249        {
250            // Non zero types are not considered packed during decoding, because
251            // they cannot inhabit the bit pattern of all zeros.
252            const IS_BITWISE_DECODE: bool = false;
253
254            fn decode<D>(decoder: D) -> Result<Self, D::Error>
255            where
256                D: Decoder<'de, Allocator = A>,
257            {
258                let cx = decoder.cx();
259                let value = decoder.decode()?;
260
261                match Self::new(value) {
262                    Some(value) => Ok(value),
263                    None => Err(cx.message(NonZeroUnsupportedValue {
264                        type_name: stringify!($ty),
265                        value,
266                    })),
267                }
268            }
269        }
270    };
271}
272
273non_zero!(NonZeroI128);
274non_zero!(NonZeroI16);
275non_zero!(NonZeroI32);
276non_zero!(NonZeroI64);
277non_zero!(NonZeroI8);
278non_zero!(NonZeroIsize);
279non_zero!(NonZeroU128);
280non_zero!(NonZeroU16);
281non_zero!(NonZeroU32);
282non_zero!(NonZeroU64);
283non_zero!(NonZeroU8);
284non_zero!(NonZeroUsize);
285
286struct NonZeroUnsupportedValue<T> {
287    type_name: &'static str,
288    value: T,
289}
290
291impl<T> fmt::Display for NonZeroUnsupportedValue<T>
292where
293    T: fmt::Display,
294{
295    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
296        write!(
297            f,
298            "{}: unsupported non-zero value `{}`",
299            self.type_name, self.value
300        )
301    }
302}
303
304impl<M, T, const N: usize> Encode<M> for [T; N]
305where
306    T: Encode<M>,
307{
308    const IS_BITWISE_ENCODE: bool =
309        T::IS_BITWISE_ENCODE && core::mem::size_of::<T>() % core::mem::align_of::<T>() == 0;
310
311    type Encode = [T; N];
312
313    #[inline]
314    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
315    where
316        E: Encoder<Mode = M>,
317    {
318        encoder.encode_slice(self)
319    }
320
321    #[inline]
322    fn as_encode(&self) -> &Self::Encode {
323        self
324    }
325}
326
327impl<'de, M, T, A, const N: usize> Decode<'de, M, A> for [T; N]
328where
329    T: Decode<'de, M, A>,
330    A: Allocator,
331{
332    const IS_BITWISE_DECODE: bool =
333        T::IS_BITWISE_DECODE && core::mem::size_of::<T>() % core::mem::align_of::<T>() == 0;
334
335    #[inline]
336    fn decode<D>(decoder: D) -> Result<Self, D::Error>
337    where
338        D: Decoder<'de, Mode = M, Allocator = A>,
339    {
340        let cx = decoder.cx();
341        let mark = cx.mark();
342
343        decoder.decode_sequence(|seq| {
344            let mut array = crate::internal::FixedVec::new();
345
346            while let Some(item) = seq.try_decode_next()? {
347                array.try_push(item.decode()?).map_err(cx.map())?;
348            }
349
350            if array.len() != N {
351                return Err(cx.message_at(
352                    &mark,
353                    format_args!(
354                        "Array with length {} does not have the expected {N} number of elements",
355                        array.len()
356                    ),
357                ));
358            }
359
360            Ok(array.into_inner())
361        })
362    }
363}
364
365impl<M, T, const N: usize> EncodePacked<M> for [T; N]
366where
367    T: Encode<M>,
368{
369    #[inline]
370    fn encode_packed<E>(&self, encoder: E) -> Result<(), E::Error>
371    where
372        E: Encoder<Mode = M>,
373    {
374        encoder.encode_pack_fn(|seq| {
375            for value in self.iter() {
376                seq.push(value)?;
377            }
378
379            Ok(())
380        })
381    }
382}
383
384impl<'de, M, A, T, const N: usize> DecodePacked<'de, M, A> for [T; N]
385where
386    A: Allocator,
387    T: Decode<'de, M, A>,
388{
389    #[inline]
390    fn decode_packed<D>(decoder: D) -> Result<Self, D::Error>
391    where
392        D: Decoder<'de, Mode = M, Allocator = A>,
393    {
394        let cx = decoder.cx();
395
396        decoder.decode_pack(|pack| {
397            let mut array = crate::internal::FixedVec::new();
398
399            while array.len() < N {
400                let item = pack.decode_next()?;
401                array.try_push(item.decode()?).map_err(cx.map())?;
402            }
403
404            Ok(array.into_inner())
405        })
406    }
407}
408
409macro_rules! impl_number {
410    ($ty:ty, $read:ident, $write:ident) => {
411        impl<M> Encode<M> for $ty {
412            const IS_BITWISE_ENCODE: bool = true;
413
414            #[inline]
415            fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
416            where
417                E: Encoder,
418            {
419                encoder.$write(*self)
420            }
421
422            type Encode = Self;
423
424            #[inline]
425            fn as_encode(&self) -> &Self::Encode {
426                self
427            }
428        }
429
430        impl<'de, M, A> Decode<'de, M, A> for $ty
431        where
432            A: Allocator,
433        {
434            const IS_BITWISE_DECODE: bool = true;
435
436            #[inline]
437            fn decode<D>(decoder: D) -> Result<Self, D::Error>
438            where
439                D: Decoder<'de>,
440            {
441                decoder.$read()
442            }
443        }
444    };
445}
446
447impl<M> Encode<M> for bool {
448    type Encode = Self;
449
450    // A boolean is bitwise encodeable since it's guaranteed to inhabit a valid
451    // bit pattern of one byte without padding.
452    const IS_BITWISE_ENCODE: bool = true;
453
454    #[inline]
455    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
456    where
457        E: Encoder,
458    {
459        encoder.encode_bool(*self)
460    }
461
462    #[inline]
463    fn as_encode(&self) -> &Self::Encode {
464        self
465    }
466}
467
468impl<'de, M, A> Decode<'de, M, A> for bool
469where
470    A: Allocator,
471{
472    // A boolean is not packed during decoding since every bit pattern that
473    // comes in is not necessarily valid.
474    const IS_BITWISE_DECODE: bool = false;
475
476    #[inline]
477    fn decode<D>(decoder: D) -> Result<Self, D::Error>
478    where
479        D: Decoder<'de>,
480    {
481        decoder.decode_bool()
482    }
483}
484
485impl<M> Encode<M> for char {
486    type Encode = Self;
487
488    // A char bitwise encodeable since it's guaranteed to inhabit a valid bit
489    // pattern of a u32 and can be bitwise copied when encoded.
490    const IS_BITWISE_ENCODE: bool = true;
491
492    #[inline]
493    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
494    where
495        E: Encoder,
496    {
497        encoder.encode_char(*self)
498    }
499
500    #[inline]
501    fn as_encode(&self) -> &Self::Encode {
502        self
503    }
504}
505
506impl<'de, M, A> Decode<'de, M, A> for char
507where
508    A: Allocator,
509{
510    // A char is not packed during decoding since it's not guaranteed to inhabit
511    // a valid bit pattern of a u32 and can not be bitwise copied when encoded.
512    const IS_BITWISE_DECODE: bool = false;
513
514    #[inline]
515    fn decode<D>(decoder: D) -> Result<Self, D::Error>
516    where
517        D: Decoder<'de>,
518    {
519        decoder.decode_char()
520    }
521}
522
523impl_number!(usize, decode_usize, encode_usize);
524impl_number!(isize, decode_isize, encode_isize);
525impl_number!(u8, decode_u8, encode_u8);
526impl_number!(u16, decode_u16, encode_u16);
527impl_number!(u32, decode_u32, encode_u32);
528impl_number!(u64, decode_u64, encode_u64);
529impl_number!(u128, decode_u128, encode_u128);
530impl_number!(i8, decode_i8, encode_i8);
531impl_number!(i16, decode_i16, encode_i16);
532impl_number!(i32, decode_i32, encode_i32);
533impl_number!(i64, decode_i64, encode_i64);
534impl_number!(i128, decode_i128, encode_i128);
535impl_number!(f32, decode_f32, encode_f32);
536impl_number!(f64, decode_f64, encode_f64);
537
538impl<M> Encode<M> for str {
539    const IS_BITWISE_ENCODE: bool = false;
540    #[inline]
541    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
542    where
543        E: Encoder,
544    {
545        encoder.encode_string(self)
546    }
547
548    type Encode = Self;
549
550    #[inline]
551    fn as_encode(&self) -> &Self::Encode {
552        self
553    }
554}
555
556impl<'de, M, A> Decode<'de, M, A> for &'de str
557where
558    A: Allocator,
559{
560    const IS_BITWISE_DECODE: bool = false;
561
562    #[inline]
563    fn decode<D>(decoder: D) -> Result<Self, D::Error>
564    where
565        D: Decoder<'de>,
566    {
567        struct Visitor;
568
569        #[crate::trait_defaults(crate)]
570        impl<'de, C> UnsizedVisitor<'de, C, str> for Visitor
571        where
572            C: Context,
573        {
574            type Ok = &'de str;
575
576            #[inline]
577            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
578                write!(f, "string borrowed from source")
579            }
580
581            #[inline]
582            fn visit_borrowed(self, _: C, string: &'de str) -> Result<Self::Ok, Self::Error> {
583                Ok(string)
584            }
585        }
586
587        decoder.decode_string(Visitor)
588    }
589}
590
591impl<'de, M> DecodeUnsized<'de, M> for str {
592    #[inline]
593    fn decode_unsized<D, F, O>(decoder: D, f: F) -> Result<O, D::Error>
594    where
595        D: Decoder<'de>,
596        F: FnOnce(&Self) -> Result<O, D::Error>,
597    {
598        struct Visitor<F>(F);
599
600        #[crate::trait_defaults(crate)]
601        impl<C, F, O> UnsizedVisitor<'_, C, str> for Visitor<F>
602        where
603            C: Context,
604            F: FnOnce(&str) -> Result<O, C::Error>,
605        {
606            type Ok = O;
607
608            #[inline]
609            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
610                write!(f, "string visited from source")
611            }
612
613            #[inline]
614            fn visit_ref(self, _: C, string: &str) -> Result<Self::Ok, C::Error> {
615                (self.0)(string)
616            }
617        }
618
619        decoder.decode_string(Visitor(f))
620    }
621}
622
623impl<'de, M> DecodeUnsized<'de, M> for [u8] {
624    #[inline]
625    fn decode_unsized<D, F, O>(decoder: D, f: F) -> Result<O, D::Error>
626    where
627        D: Decoder<'de>,
628        F: FnOnce(&Self) -> Result<O, D::Error>,
629    {
630        struct Visitor<F>(F);
631
632        #[crate::trait_defaults(crate)]
633        impl<C, F, O> UnsizedVisitor<'_, C, [u8]> for Visitor<F>
634        where
635            C: Context,
636            F: FnOnce(&[u8]) -> Result<O, C::Error>,
637        {
638            type Ok = O;
639
640            #[inline]
641            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
642                write!(f, "bytes visited from source")
643            }
644
645            #[inline]
646            fn visit_ref(self, _: C, bytes: &[u8]) -> Result<Self::Ok, C::Error> {
647                (self.0)(bytes)
648            }
649        }
650
651        decoder.decode_bytes(Visitor(f))
652    }
653}
654
655impl<M, T> Encode<M> for [T]
656where
657    T: Encode<M>,
658{
659    type Encode = Self;
660
661    const IS_BITWISE_ENCODE: bool = false;
662
663    #[inline]
664    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
665    where
666        E: Encoder<Mode = M>,
667    {
668        encoder.encode_slice(self)
669    }
670
671    #[inline]
672    fn as_encode(&self) -> &Self::Encode {
673        self
674    }
675}
676
677impl<'de, M, A> Decode<'de, M, A> for &'de [u8]
678where
679    A: Allocator,
680{
681    const IS_BITWISE_DECODE: bool = false;
682
683    #[inline]
684    fn decode<D>(decoder: D) -> Result<Self, D::Error>
685    where
686        D: Decoder<'de>,
687    {
688        struct Visitor;
689
690        #[crate::trait_defaults(crate)]
691        impl<'de, C> UnsizedVisitor<'de, C, [u8]> for Visitor
692        where
693            C: Context,
694        {
695            type Ok = &'de [u8];
696
697            #[inline]
698            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
699                write!(f, "bytes borrowed from source")
700            }
701
702            #[inline]
703            fn visit_borrowed(self, _: C, bytes: &'de [u8]) -> Result<Self::Ok, Self::Error> {
704                Ok(bytes)
705            }
706        }
707
708        decoder.decode_bytes(Visitor)
709    }
710}
711
712impl<'de, M> DecodeUnsizedBytes<'de, M> for [u8] {
713    #[inline]
714    fn decode_unsized_bytes<D, F, O>(decoder: D, f: F) -> Result<O, D::Error>
715    where
716        D: Decoder<'de>,
717        F: FnOnce(&Self) -> Result<O, D::Error>,
718    {
719        struct Visitor<F>(F);
720
721        #[crate::trait_defaults(crate)]
722        impl<C, F, O> UnsizedVisitor<'_, C, [u8]> for Visitor<F>
723        where
724            C: Context,
725            F: FnOnce(&[u8]) -> Result<O, C::Error>,
726        {
727            type Ok = O;
728
729            #[inline]
730            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
731                write!(f, "bytes visited from source")
732            }
733
734            #[inline]
735            fn visit_ref(self, _: C, bytes: &[u8]) -> Result<Self::Ok, C::Error> {
736                (self.0)(bytes)
737            }
738        }
739
740        decoder.decode_bytes(Visitor(f))
741    }
742}
743
744impl<T, M> Encode<M> for Option<T>
745where
746    T: Encode<M>,
747{
748    type Encode = Self;
749
750    const IS_BITWISE_ENCODE: bool = false;
751
752    #[inline]
753    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
754    where
755        E: Encoder<Mode = M>,
756    {
757        match self {
758            Some(value) => encoder.encode_some()?.encode(value),
759            None => encoder.encode_none(),
760        }
761    }
762
763    #[inline]
764    fn as_encode(&self) -> &Self::Encode {
765        self
766    }
767}
768
769impl<'de, M, A, T> Decode<'de, M, A> for Option<T>
770where
771    A: Allocator,
772    T: Decode<'de, M, A>,
773{
774    const IS_BITWISE_DECODE: bool = false;
775
776    #[inline]
777    fn decode<D>(decoder: D) -> Result<Self, D::Error>
778    where
779        D: Decoder<'de, Mode = M, Allocator = A>,
780    {
781        if let Some(decoder) = decoder.decode_option()? {
782            Ok(Some(decoder.decode()?))
783        } else {
784            Ok(None)
785        }
786    }
787}
788
789#[derive(Encode, Decode)]
790#[musli(crate)]
791enum ResultTag {
792    Ok,
793    Err,
794}
795
796impl<T, U, M> Encode<M> for Result<T, U>
797where
798    T: Encode<M>,
799    U: Encode<M>,
800    ResultTag: Encode<M>,
801{
802    type Encode = Self;
803
804    const IS_BITWISE_ENCODE: bool = false;
805
806    #[inline]
807    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
808    where
809        E: Encoder<Mode = M>,
810    {
811        let variant = encoder.encode_variant()?;
812
813        match self {
814            Ok(ok) => variant.insert_variant(&ResultTag::Ok, ok),
815            Err(err) => variant.insert_variant(&ResultTag::Err, err),
816        }
817    }
818
819    #[inline]
820    fn as_encode(&self) -> &Self::Encode {
821        self
822    }
823}
824
825impl<'de, M, A, T, U> Decode<'de, M, A> for Result<T, U>
826where
827    A: Allocator,
828    T: Decode<'de, M, A>,
829    U: Decode<'de, M, A>,
830    ResultTag: Decode<'de, M, A>,
831{
832    const IS_BITWISE_DECODE: bool = false;
833
834    #[inline]
835    fn decode<D>(decoder: D) -> Result<Self, D::Error>
836    where
837        D: Decoder<'de, Mode = M, Allocator = A>,
838    {
839        decoder.decode_variant(|variant| {
840            let tag = variant.decode_tag()?.decode()?;
841
842            Ok(match tag {
843                ResultTag::Ok => Ok(variant.decode_value()?.decode()?),
844                ResultTag::Err => Err(variant.decode_value()?.decode()?),
845            })
846        })
847    }
848}
849
850impl<T, M> Encode<M> for Wrapping<T>
851where
852    T: Encode<M>,
853{
854    const IS_BITWISE_ENCODE: bool = T::IS_BITWISE_ENCODE;
855
856    type Encode = Self;
857
858    #[inline]
859    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
860    where
861        E: Encoder<Mode = M>,
862    {
863        self.0.encode(encoder)
864    }
865
866    #[inline]
867    fn as_encode(&self) -> &Self::Encode {
868        self
869    }
870}
871
872impl<'de, M, T, A> Decode<'de, M, A> for Wrapping<T>
873where
874    T: Decode<'de, M, A>,
875    A: Allocator,
876{
877    const IS_BITWISE_DECODE: bool = T::IS_BITWISE_DECODE;
878
879    #[inline]
880    fn decode<D>(decoder: D) -> Result<Self, D::Error>
881    where
882        D: Decoder<'de, Mode = M, Allocator = A>,
883    {
884        Ok(Wrapping(decoder.decode()?))
885    }
886}
887
888impl<T, M> Encode<M> for Saturating<T>
889where
890    T: Encode<M>,
891{
892    const IS_BITWISE_ENCODE: bool = T::IS_BITWISE_ENCODE;
893
894    type Encode = Self;
895
896    #[inline]
897    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
898    where
899        E: Encoder<Mode = M>,
900    {
901        self.0.encode(encoder)
902    }
903
904    #[inline]
905    fn as_encode(&self) -> &Self::Encode {
906        self
907    }
908}
909
910impl<'de, M, T, A> Decode<'de, M, A> for Saturating<T>
911where
912    T: Decode<'de, M, A>,
913    A: Allocator,
914{
915    const IS_BITWISE_DECODE: bool = T::IS_BITWISE_DECODE;
916
917    #[inline]
918    fn decode<D>(decoder: D) -> Result<Self, D::Error>
919    where
920        D: Decoder<'de, Mode = M, Allocator = A>,
921    {
922        Ok(Saturating(decoder.decode()?))
923    }
924}
925
926impl<M> Encode<M> for CStr {
927    type Encode = Self;
928
929    const IS_BITWISE_ENCODE: bool = false;
930
931    #[inline]
932    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
933    where
934        E: Encoder,
935    {
936        encoder.encode_bytes(self.to_bytes_with_nul())
937    }
938
939    #[inline]
940    fn as_encode(&self) -> &Self::Encode {
941        self
942    }
943}
944
945impl<'de, M, A> Decode<'de, M, A> for &'de CStr
946where
947    A: Allocator,
948{
949    const IS_BITWISE_DECODE: bool = false;
950
951    #[inline]
952    fn decode<D>(decoder: D) -> Result<Self, D::Error>
953    where
954        D: Decoder<'de>,
955    {
956        let cx = decoder.cx();
957        let bytes = decoder.decode()?;
958        CStr::from_bytes_with_nul(bytes).map_err(cx.map())
959    }
960}
961
962impl<'de, M> DecodeUnsized<'de, M> for CStr {
963    #[inline]
964    fn decode_unsized<D, F, O>(decoder: D, f: F) -> Result<O, D::Error>
965    where
966        D: Decoder<'de, Mode = M>,
967        F: FnOnce(&Self) -> Result<O, D::Error>,
968    {
969        let cx = decoder.cx();
970
971        DecodeUnsizedBytes::decode_unsized_bytes(decoder, |bytes: &[u8]| {
972            let cstr = CStr::from_bytes_with_nul(bytes).map_err(cx.map())?;
973            f(cstr)
974        })
975    }
976}
977
978impl<M> EncodeBytes<M> for [u8] {
979    const ENCODE_BYTES_PACKED: bool = false;
980
981    type EncodeBytes = [u8];
982
983    #[inline]
984    fn encode_bytes<E>(&self, encoder: E) -> Result<(), E::Error>
985    where
986        E: Encoder<Mode = M>,
987    {
988        encoder.encode_bytes(self)
989    }
990
991    #[inline]
992    fn as_encode_bytes(&self) -> &Self::EncodeBytes {
993        self
994    }
995}
996
997impl<const N: usize, M> EncodeBytes<M> for [u8; N] {
998    const ENCODE_BYTES_PACKED: bool = true;
999
1000    type EncodeBytes = [u8; N];
1001
1002    #[inline]
1003    fn encode_bytes<E>(&self, encoder: E) -> Result<(), E::Error>
1004    where
1005        E: Encoder<Mode = M>,
1006    {
1007        encoder.encode_array(self)
1008    }
1009
1010    #[inline]
1011    fn as_encode_bytes(&self) -> &Self::EncodeBytes {
1012        self
1013    }
1014}
1015
1016impl<'de, M, A> DecodeBytes<'de, M, A> for &'de [u8]
1017where
1018    A: Allocator,
1019{
1020    const DECODE_BYTES_PACKED: bool = false;
1021
1022    #[inline]
1023    fn decode_bytes<D>(decoder: D) -> Result<Self, D::Error>
1024    where
1025        D: Decoder<'de, Allocator = A>,
1026    {
1027        Decode::decode(decoder)
1028    }
1029}
1030
1031impl<'de, M, A, const N: usize> DecodeBytes<'de, M, A> for [u8; N]
1032where
1033    A: Allocator,
1034{
1035    const DECODE_BYTES_PACKED: bool = true;
1036
1037    #[inline]
1038    fn decode_bytes<D>(decoder: D) -> Result<Self, D::Error>
1039    where
1040        D: Decoder<'de, Allocator = A>,
1041    {
1042        decoder.decode_array()
1043    }
1044}