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