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