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