musli_core/de/
decoder.rs

1use core::fmt;
2
3use crate::expecting::{self, Expecting};
4use crate::hint::{MapHint, SequenceHint};
5use crate::{Allocator, Context};
6
7use super::{
8    utils, AsDecoder, Decode, DecodeSliceBuilder, DecodeUnsized, DecodeUnsizedBytes,
9    EntriesDecoder, MapDecoder, SequenceDecoder, Skip, UnsizedVisitor, VariantDecoder, Visitor,
10};
11
12/// An outcome of a fast decode attempt.
13#[non_exhaustive]
14pub enum TryFastDecode<T, D> {
15    /// The decode attempt was successful.
16    Ok(T),
17    /// The decode was unsupported.
18    Unsupported(D),
19}
20
21/// Trait governing the implementation of a decoder.
22#[must_use = "Decoders must be consumed through one of its decode_* methods"]
23#[allow(unused_variables)]
24pub trait Decoder<'de>: Sized {
25    /// Context associated with the decoder.
26    type Cx: Context<Error = Self::Error, Allocator = Self::Allocator>;
27    /// Error associated with decoding.
28    type Error;
29    /// The allocator associated with the decoder.
30    type Allocator: Allocator;
31    /// Mode associated with decoding.
32    type Mode: 'static;
33    /// Decoder returned by [`Decoder::decode_buffer`].
34    type DecodeBuffer: AsDecoder<
35        Cx = Self::Cx,
36        Error = Self::Error,
37        Allocator = Self::Allocator,
38        Mode = Self::Mode,
39    >;
40    /// Decoder returned by [`Decoder::decode_option`].
41    type DecodeSome: Decoder<
42        'de,
43        Cx = Self::Cx,
44        Error = Self::Error,
45        Allocator = Self::Allocator,
46        Mode = Self::Mode,
47    >;
48    /// Decoder used by [`Decoder::decode_pack`].
49    type DecodePack: SequenceDecoder<
50        'de,
51        Cx = Self::Cx,
52        Error = Self::Error,
53        Allocator = Self::Allocator,
54        Mode = Self::Mode,
55    >;
56    /// Decoder returned by [`Decoder::decode_sequence`].
57    type DecodeSequence: SequenceDecoder<
58        'de,
59        Cx = Self::Cx,
60        Error = Self::Error,
61        Allocator = Self::Allocator,
62        Mode = Self::Mode,
63    >;
64    /// Decoder returned by [`Decoder::decode_map`].
65    type DecodeMap: MapDecoder<
66        'de,
67        Cx = Self::Cx,
68        Error = Self::Error,
69        Allocator = Self::Allocator,
70        Mode = Self::Mode,
71    >;
72    /// Decoder returned by [`Decoder::decode_map_entries`].
73    type DecodeMapEntries: EntriesDecoder<
74        'de,
75        Cx = Self::Cx,
76        Error = Self::Error,
77        Allocator = Self::Allocator,
78        Mode = Self::Mode,
79    >;
80    /// Decoder used by [`Decoder::decode_variant`].
81    type DecodeVariant: VariantDecoder<
82        'de,
83        Cx = Self::Cx,
84        Error = Self::Error,
85        Allocator = Self::Allocator,
86        Mode = Self::Mode,
87    >;
88
89    /// This is a type argument used to hint to any future implementor that they
90    /// should be using the [`#[musli::decoder]`][musli::decoder] attribute
91    /// macro when implementing [`Decoder`].
92    #[doc(hidden)]
93    type __UseMusliDecoderAttributeMacro;
94
95    /// Access the context associated with the decoder.
96    fn cx(&self) -> Self::Cx;
97
98    /// Format the human-readable message that should occur if the decoder was
99    /// expecting to decode some specific kind of value.
100    ///
101    /// # Examples
102    ///
103    /// ```
104    /// use std::fmt;
105    /// use std::convert::Infallible;
106    /// use std::marker::PhantomData;
107    ///
108    /// use musli::Context;
109    /// use musli::de::{self, Decoder, Decode};
110    ///
111    /// struct MyDecoder<C, M> {
112    ///     cx: C,
113    ///     _marker: PhantomData<M>,
114    /// }
115    ///
116    /// #[musli::decoder]
117    /// impl<'de, C, M> Decoder<'de> for MyDecoder<C, M>
118    /// where
119    ///     C: Context,
120    ///     M: 'static,
121    /// {
122    ///     type Cx = C;
123    ///
124    ///     #[inline]
125    ///     fn cx(&self) -> Self::Cx {
126    ///         self.cx
127    ///     }
128    ///
129    ///     #[inline]
130    ///     fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131    ///         write!(f, "32-bit unsigned integers")
132    ///     }
133    ///
134    ///     #[inline]
135    ///     fn decode_u32(self) -> Result<u32, Self::Error> {
136    ///         Ok(42)
137    ///     }
138    /// }
139    /// ```
140    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
141
142    /// Try to quickly decode the specified value.
143    ///
144    /// The default implementation simply returns the current decoder as
145    /// `Err(Self)`.
146    ///
147    /// This is intended to be a fast path when decoding a value when an
148    /// encoding permits it.
149    #[inline]
150    fn try_fast_decode<T>(self) -> Result<TryFastDecode<T, Self>, Self::Error>
151    where
152        T: Decode<'de, Self::Mode, Self::Allocator>,
153    {
154        Ok(TryFastDecode::Unsupported(self))
155    }
156
157    /// Decode the current decoder into the value `T`.
158    ///
159    /// This calls the appropriate [`Decode`] implementation for the given type.
160    #[inline]
161    fn decode<T>(self) -> Result<T, Self::Error>
162    where
163        T: Decode<'de, Self::Mode, Self::Allocator>,
164    {
165        match self.try_fast_decode::<T>()? {
166            TryFastDecode::Ok(value) => Ok(value),
167            TryFastDecode::Unsupported(decoder) => T::decode(decoder),
168        }
169    }
170
171    /// Decode an unsized value by reference through the specified closure.
172    #[inline]
173    fn decode_unsized<T, F, O>(self, f: F) -> Result<O, Self::Error>
174    where
175        T: ?Sized + DecodeUnsized<'de, Self::Mode>,
176        F: FnOnce(&T) -> Result<O, Self::Error>,
177    {
178        T::decode_unsized(self, f)
179    }
180
181    /// Decode an unsized value as bytes by reference through the specified
182    /// closure.
183    #[inline]
184    fn decode_unsized_bytes<T, F, O>(self, f: F) -> Result<O, Self::Error>
185    where
186        T: ?Sized + DecodeUnsizedBytes<'de, Self::Mode>,
187        F: FnOnce(&T) -> Result<O, Self::Error>,
188    {
189        T::decode_unsized_bytes(self, f)
190    }
191
192    /// Skip over the current next value.
193    #[inline]
194    fn skip(self) -> Result<(), Self::Error> {
195        Err(self.cx().message(format_args!(
196            "Skipping is not supported, expected {}",
197            ExpectingWrapper::new(&self).format()
198        )))
199    }
200
201    /// This is a variant of [`Decoder::skip`], but instead of erroring in case
202    /// skipping is not supported it must return [`Skip::Unsupported`].
203    #[inline(always)]
204    fn try_skip(self) -> Result<Skip, Self::Error> {
205        Ok(Skip::Unsupported)
206    }
207
208    /// Buffer the current decoder into a buffer that can be used multiple times.
209    ///
210    /// Buffering a decoder is necessary when additional introspection is needed
211    /// to decode a type, but it also means that:
212    ///
213    /// * The entire contents of the decoder needs to be dynamically buffered in
214    ///   memory.
215    /// * The in-memory representation might be lossy in some trivial ways. Such
216    ///   as arbitrary precision numbers being punted into a 64-bit float.
217    ///
218    /// # Examples
219    ///
220    /// ```
221    /// use musli::{Allocator, Context, Decode, Decoder};
222    /// use musli::de::{AsDecoder, MapDecoder, EntryDecoder};
223    /// use musli::mode::Binary;
224    ///
225    /// #[derive(Decode)]
226    /// struct Person {
227    ///     name: String,
228    ///     age: u32,
229    /// }
230    ///
231    /// enum Enum {
232    ///     Empty,
233    ///     Person(Person),
234    /// }
235    ///
236    /// impl<'de, A> Decode<'de, Binary, A> for Enum
237    /// where
238    ///     A: Allocator,
239    /// {
240    ///     #[inline]
241    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
242    ///     where
243    ///         D: Decoder<'de, Mode = Binary>,
244    ///     {
245    ///         let cx = decoder.cx();
246    ///
247    ///         let mut buffer = decoder.decode_buffer()?;
248    ///
249    ///         let discriminant = buffer.as_decoder()?.decode_map(|st| {
250    ///             loop {
251    ///                 let Some(mut e) = st.decode_entry()? else {
252    ///                     return Err(cx.message("Missing variant tag"));
253    ///                 };
254    ///
255    ///                 let found = e.decode_key()?.decode_unsized(|string: &str| {
256    ///                     Ok(string == "type")
257    ///                 })?;
258    ///
259    ///                 if found {
260    ///                     break Ok(e.decode_value()?.decode()?);
261    ///                 }
262    ///             }
263    ///         })?;
264    ///
265    ///         match discriminant {
266    ///             0 => Ok(Enum::Empty),
267    ///             1 => Ok(Enum::Person(buffer.as_decoder()?.decode()?)),
268    ///             other => Err(cx.message(format_args!("Invalid variant tag {other:?}"))),
269    ///         }
270    ///     }
271    /// }
272    /// ```
273    #[inline]
274    fn decode_buffer(self) -> Result<Self::DecodeBuffer, Self::Error> {
275        Err(self.cx().message(format_args!(
276            "Decode buffering not supported, expected {}",
277            ExpectingWrapper::new(&self).format()
278        )))
279    }
280
281    /// Decode a unit.
282    ///
283    /// # Examples
284    ///
285    /// Deriving an implementation:
286    ///
287    /// ```
288    /// use musli::Decode;
289    ///
290    /// #[derive(Decode)]
291    /// struct UnitStruct;
292    /// ```
293    ///
294    /// Implementing manually:
295    ///
296    /// ```
297    /// use musli::{Allocator, Decode, Decoder};
298    /// # struct UnitType;
299    ///
300    /// impl<'de, M, A> Decode<'de, M, A> for UnitType
301    /// where
302    ///     A: Allocator,
303    /// {
304    ///     #[inline]
305    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
306    ///     where
307    ///         D: Decoder<'de>,
308    ///     {
309    ///         decoder.decode_empty()?;
310    ///         Ok(UnitType)
311    ///     }
312    /// }
313    /// ```
314    #[inline]
315    fn decode_empty(self) -> Result<(), Self::Error> {
316        Err(self.cx().message(expecting::unsupported_type(
317            &expecting::Empty,
318            ExpectingWrapper::new(&self),
319        )))
320    }
321
322    /// Decode a boolean.
323    ///
324    /// # Examples
325    ///
326    /// Deriving an implementation:
327    ///
328    /// ```
329    /// use musli::Decode;
330    ///
331    /// #[derive(Decode)]
332    /// #[musli(packed)]
333    /// struct BooleanField {
334    ///     field: bool,
335    /// }
336    /// ```
337    ///
338    /// Implementing manually:
339    ///
340    /// ```
341    /// use musli::{Allocator, Decode, Decoder};
342    /// # struct BooleanField { field: bool }
343    ///
344    /// impl<'de, M, A> Decode<'de, M, A> for BooleanField
345    /// where
346    ///     A: Allocator,
347    /// {
348    ///     #[inline]
349    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
350    ///     where
351    ///         D: Decoder<'de>,
352    ///     {
353    ///         Ok(Self {
354    ///             field: decoder.decode_bool()?,
355    ///         })
356    ///     }
357    /// }
358    /// ```
359    #[inline]
360    fn decode_bool(self) -> Result<bool, Self::Error> {
361        Err(self.cx().message(expecting::unsupported_type(
362            &expecting::Bool,
363            ExpectingWrapper::new(&self),
364        )))
365    }
366
367    /// Decode a character.
368    ///
369    /// # Examples
370    ///
371    /// Deriving an implementation:
372    ///
373    /// ```
374    /// use musli::Decode;
375    ///
376    /// #[derive(Decode)]
377    /// #[musli(packed)]
378    /// struct MyType {
379    ///     data: char,
380    /// }
381    /// ```
382    ///
383    /// Implementing manually:
384    ///
385    /// ```
386    /// use musli::{Allocator, Decode, Decoder};
387    /// # struct MyType { data: char }
388    ///
389    /// impl<'de, M, A> Decode<'de, M, A> for MyType
390    /// where
391    ///     A: Allocator,
392    /// {
393    ///     #[inline]
394    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
395    ///     where
396    ///         D: Decoder<'de>,
397    ///     {
398    ///         Ok(Self {
399    ///             data: decoder.decode_char()?,
400    ///         })
401    ///     }
402    /// }
403    /// ```
404    #[inline]
405    fn decode_char(self) -> Result<char, Self::Error> {
406        Err(self.cx().message(expecting::unsupported_type(
407            &expecting::Char,
408            ExpectingWrapper::new(&self),
409        )))
410    }
411
412    /// Decode a 8-bit unsigned integer (a.k.a. a byte).
413    ///
414    /// # Examples
415    ///
416    /// Deriving an implementation:
417    ///
418    /// ```
419    /// use musli::Decode;
420    ///
421    /// #[derive(Decode)]
422    /// #[musli(packed)]
423    /// struct MyType {
424    ///     data: u8,
425    /// }
426    /// ```
427    ///
428    /// Implementing manually:
429    ///
430    /// ```
431    /// use musli::{Allocator, Decode, Decoder};
432    /// # struct MyType { data: u8 }
433    ///
434    /// impl<'de, M, A> Decode<'de, M, A> for MyType
435    /// where
436    ///     A: Allocator,
437    /// {
438    ///     #[inline]
439    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
440    ///     where
441    ///         D: Decoder<'de>,
442    ///     {
443    ///         Ok(Self {
444    ///             data: decoder.decode_u8()?,
445    ///         })
446    ///     }
447    /// }
448    /// ```
449    #[inline]
450    fn decode_u8(self) -> Result<u8, Self::Error> {
451        Err(self.cx().message(expecting::unsupported_type(
452            &expecting::Unsigned8,
453            ExpectingWrapper::new(&self),
454        )))
455    }
456
457    /// Decode a 16-bit unsigned integer.
458    ///
459    /// # Examples
460    ///
461    /// Deriving an implementation:
462    ///
463    /// ```
464    /// use musli::Decode;
465    ///
466    /// #[derive(Decode)]
467    /// #[musli(packed)]
468    /// struct MyType {
469    ///     data: u16,
470    /// }
471    /// ```
472    ///
473    /// Implementing manually:
474    ///
475    /// ```
476    /// use musli::{Allocator, Decode, Decoder};
477    /// # struct MyType { data: u16 }
478    ///
479    /// impl<'de, M, A> Decode<'de, M, A> for MyType
480    /// where
481    ///     A: Allocator,
482    /// {
483    ///     #[inline]
484    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
485    ///     where
486    ///         D: Decoder<'de>,
487    ///     {
488    ///         Ok(Self {
489    ///             data: decoder.decode_u16()?,
490    ///         })
491    ///     }
492    /// }
493    /// ```
494    #[inline]
495    fn decode_u16(self) -> Result<u16, Self::Error> {
496        Err(self.cx().message(expecting::unsupported_type(
497            &expecting::Unsigned16,
498            ExpectingWrapper::new(&self),
499        )))
500    }
501
502    /// Decode a 32-bit unsigned integer.
503    ///
504    /// # Examples
505    ///
506    /// Deriving an implementation:
507    ///
508    /// ```
509    /// use musli::Decode;
510    ///
511    /// #[derive(Decode)]
512    /// #[musli(packed)]
513    /// struct MyType {
514    ///     data: u32,
515    /// }
516    /// ```
517    ///
518    /// Implementing manually:
519    ///
520    /// ```
521    /// use musli::{Allocator, Decode, Decoder};
522    /// # struct MyType { data: u32 }
523    ///
524    /// impl<'de, M, A> Decode<'de, M, A> for MyType
525    /// where
526    ///     A: Allocator,
527    /// {
528    ///     #[inline]
529    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
530    ///     where
531    ///         D: Decoder<'de>,
532    ///     {
533    ///         Ok(Self {
534    ///             data: decoder.decode_u32()?,
535    ///         })
536    ///     }
537    /// }
538    /// ```
539    #[inline]
540    fn decode_u32(self) -> Result<u32, Self::Error> {
541        Err(self.cx().message(expecting::unsupported_type(
542            &expecting::Unsigned32,
543            ExpectingWrapper::new(&self),
544        )))
545    }
546
547    /// Decode a 64-bit unsigned integer.
548    ///
549    /// # Examples
550    ///
551    /// Deriving an implementation:
552    ///
553    /// ```
554    /// use musli::Decode;
555    ///
556    /// #[derive(Decode)]
557    /// #[musli(packed)]
558    /// struct MyType {
559    ///     data: u64,
560    /// }
561    /// ```
562    ///
563    /// Implementing manually:
564    ///
565    /// ```
566    /// use musli::{Allocator, Decode, Decoder};
567    /// # struct MyType { data: u64 }
568    ///
569    /// impl<'de, M, A> Decode<'de, M, A> for MyType
570    /// where
571    ///     A: Allocator,
572    /// {
573    ///     #[inline]
574    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
575    ///     where
576    ///         D: Decoder<'de>,
577    ///     {
578    ///         Ok(Self {
579    ///             data: decoder.decode_u64()?,
580    ///         })
581    ///     }
582    /// }
583    /// ```
584    #[inline]
585    fn decode_u64(self) -> Result<u64, Self::Error> {
586        Err(self.cx().message(expecting::unsupported_type(
587            &expecting::Unsigned64,
588            ExpectingWrapper::new(&self),
589        )))
590    }
591
592    /// Decode a 128-bit unsigned integer.
593    ///
594    /// # Examples
595    ///
596    /// Deriving an implementation:
597    ///
598    /// ```
599    /// use musli::Decode;
600    ///
601    /// #[derive(Decode)]
602    /// #[musli(packed)]
603    /// struct MyType {
604    ///     data: u128,
605    /// }
606    /// ```
607    ///
608    /// Implementing manually:
609    ///
610    /// ```
611    /// use musli::{Allocator, Decode, Decoder};
612    /// # struct MyType { data: u128 }
613    ///
614    /// impl<'de, M, A> Decode<'de, M, A> for MyType
615    /// where
616    ///     A: Allocator,
617    /// {
618    ///     #[inline]
619    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
620    ///     where
621    ///         D: Decoder<'de>,
622    ///     {
623    ///         Ok(Self {
624    ///             data: decoder.decode_u128()?,
625    ///         })
626    ///     }
627    /// }
628    /// ```
629    #[inline]
630    fn decode_u128(self) -> Result<u128, Self::Error> {
631        Err(self.cx().message(expecting::unsupported_type(
632            &expecting::Unsigned128,
633            ExpectingWrapper::new(&self),
634        )))
635    }
636
637    /// Decode a 8-bit signed integer.
638    ///
639    /// # Examples
640    ///
641    /// Deriving an implementation:
642    ///
643    /// ```
644    /// use musli::Decode;
645    ///
646    /// #[derive(Decode)]
647    /// #[musli(packed)]
648    /// struct MyType {
649    ///     data: i8,
650    /// }
651    /// ```
652    ///
653    /// Implementing manually:
654    ///
655    /// ```
656    /// use musli::{Allocator, Decode, Decoder};
657    /// # struct MyType { data: i8 }
658    ///
659    /// impl<'de, M, A> Decode<'de, M, A> for MyType
660    /// where
661    ///     A: Allocator,
662    /// {
663    ///     #[inline]
664    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
665    ///     where
666    ///         D: Decoder<'de>,
667    ///     {
668    ///         Ok(Self {
669    ///             data: decoder.decode_i8()?,
670    ///         })
671    ///     }
672    /// }
673    /// ```
674    #[inline]
675    fn decode_i8(self) -> Result<i8, Self::Error> {
676        Err(self.cx().message(expecting::unsupported_type(
677            &expecting::Signed8,
678            ExpectingWrapper::new(&self),
679        )))
680    }
681
682    /// Decode a 16-bit signed integer.
683    ///
684    /// # Examples
685    ///
686    /// Deriving an implementation:
687    ///
688    /// ```
689    /// use musli::Decode;
690    ///
691    /// #[derive(Decode)]
692    /// #[musli(packed)]
693    /// struct MyType {
694    ///     data: i16,
695    /// }
696    /// ```
697    ///
698    /// Implementing manually:
699    ///
700    /// ```
701    /// use musli::{Allocator, Decode, Decoder};
702    /// # struct MyType { data: i16 }
703    ///
704    /// impl<'de, M, A> Decode<'de, M, A> for MyType
705    /// where
706    ///     A: Allocator,
707    /// {
708    ///     #[inline]
709    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
710    ///     where
711    ///         D: Decoder<'de>,
712    ///     {
713    ///         Ok(Self {
714    ///             data: decoder.decode_i16()?,
715    ///         })
716    ///     }
717    /// }
718    /// ```
719    #[inline]
720    fn decode_i16(self) -> Result<i16, Self::Error> {
721        Err(self.cx().message(expecting::unsupported_type(
722            &expecting::Signed16,
723            ExpectingWrapper::new(&self),
724        )))
725    }
726
727    /// Decode a 32-bit signed integer.
728    ///
729    /// # Examples
730    ///
731    /// Deriving an implementation:
732    ///
733    /// ```
734    /// use musli::Decode;
735    ///
736    /// #[derive(Decode)]
737    /// #[musli(packed)]
738    /// struct MyType {
739    ///     data: i32,
740    /// }
741    /// ```
742    ///
743    /// Implementing manually:
744    ///
745    /// ```
746    /// use musli::{Allocator, Decode, Decoder};
747    /// # struct MyType { data: i32 }
748    ///
749    /// impl<'de, M, A> Decode<'de, M, A> for MyType
750    /// where
751    ///     A: Allocator,
752    /// {
753    ///     #[inline]
754    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
755    ///     where
756    ///         D: Decoder<'de>,
757    ///     {
758    ///         Ok(Self {
759    ///             data: decoder.decode_i32()?,
760    ///         })
761    ///     }
762    /// }
763    /// ```
764    #[inline]
765    fn decode_i32(self) -> Result<i32, Self::Error> {
766        Err(self.cx().message(expecting::unsupported_type(
767            &expecting::Signed32,
768            ExpectingWrapper::new(&self),
769        )))
770    }
771
772    /// Decode a 64-bit signed integer.
773    ///
774    /// # Examples
775    ///
776    /// Deriving an implementation:
777    ///
778    /// ```
779    /// use musli::Decode;
780    ///
781    /// #[derive(Decode)]
782    /// #[musli(packed)]
783    /// struct MyType {
784    ///     data: i64,
785    /// }
786    /// ```
787    ///
788    /// Implementing manually:
789    ///
790    /// ```
791    /// use musli::{Allocator, Decode, Decoder};
792    /// # struct MyType { data: i64 }
793    ///
794    /// impl<'de, M, A> Decode<'de, M, A> for MyType
795    /// where
796    ///     A: Allocator,
797    /// {
798    ///     #[inline]
799    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
800    ///     where
801    ///         D: Decoder<'de>,
802    ///     {
803    ///         Ok(Self {
804    ///             data: decoder.decode_i64()?,
805    ///         })
806    ///     }
807    /// }
808    /// ```
809    #[inline]
810    fn decode_i64(self) -> Result<i64, Self::Error> {
811        Err(self.cx().message(expecting::unsupported_type(
812            &expecting::Signed64,
813            ExpectingWrapper::new(&self),
814        )))
815    }
816
817    /// Decode a 128-bit signed integer.
818    ///
819    /// # Examples
820    ///
821    /// Deriving an implementation:
822    ///
823    /// ```
824    /// use musli::Decode;
825    ///
826    /// #[derive(Decode)]
827    /// #[musli(packed)]
828    /// struct MyType {
829    ///     data: i128,
830    /// }
831    /// ```
832    ///
833    /// Implementing manually:
834    ///
835    /// ```
836    /// use musli::{Allocator, Decode, Decoder};
837    /// # struct MyType { data: i128 }
838    ///
839    /// impl<'de, M, A> Decode<'de, M, A> for MyType
840    /// where
841    ///     A: Allocator,
842    /// {
843    ///     #[inline]
844    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
845    ///     where
846    ///         D: Decoder<'de>,
847    ///     {
848    ///         Ok(Self {
849    ///             data: decoder.decode_i128()?,
850    ///         })
851    ///     }
852    /// }
853    /// ```
854    #[inline]
855    fn decode_i128(self) -> Result<i128, Self::Error> {
856        Err(self.cx().message(expecting::unsupported_type(
857            &expecting::Signed128,
858            ExpectingWrapper::new(&self),
859        )))
860    }
861
862    /// Decode a [`usize`].
863    ///
864    /// # Examples
865    ///
866    /// Deriving an implementation:
867    ///
868    /// ```
869    /// use musli::Decode;
870    ///
871    /// #[derive(Decode)]
872    /// #[musli(packed)]
873    /// struct MyType {
874    ///     data: usize,
875    /// }
876    /// ```
877    ///
878    /// Implementing manually:
879    ///
880    /// ```
881    /// use musli::{Allocator, Decode, Decoder};
882    /// # struct MyType { data: usize }
883    ///
884    /// impl<'de, M, A> Decode<'de, M, A> for MyType
885    /// where
886    ///     A: Allocator,
887    /// {
888    ///     #[inline]
889    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
890    ///     where
891    ///         D: Decoder<'de>,
892    ///     {
893    ///         Ok(Self {
894    ///             data: decoder.decode_usize()?,
895    ///         })
896    ///     }
897    /// }
898    /// ```
899    #[inline]
900    fn decode_usize(self) -> Result<usize, Self::Error> {
901        Err(self.cx().message(expecting::unsupported_type(
902            &expecting::Usize,
903            ExpectingWrapper::new(&self),
904        )))
905    }
906
907    /// Decode a [`isize`].
908    ///
909    /// # Examples
910    ///
911    /// Deriving an implementation:
912    ///
913    /// ```
914    /// use musli::Decode;
915    ///
916    /// #[derive(Decode)]
917    /// #[musli(packed)]
918    /// struct MyType {
919    ///     data: isize,
920    /// }
921    /// ```
922    ///
923    /// Implementing manually:
924    ///
925    /// ```
926    /// use musli::{Allocator, Decode, Decoder};
927    /// # struct MyType { data: isize }
928    ///
929    /// impl<'de, M, A> Decode<'de, M, A> for MyType
930    /// where
931    ///     A: Allocator,
932    /// {
933    ///     #[inline]
934    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
935    ///     where
936    ///         D: Decoder<'de>,
937    ///     {
938    ///         Ok(Self {
939    ///             data: decoder.decode_isize()?,
940    ///         })
941    ///     }
942    /// }
943    /// ```
944    #[inline]
945    fn decode_isize(self) -> Result<isize, Self::Error> {
946        Err(self.cx().message(expecting::unsupported_type(
947            &expecting::Isize,
948            ExpectingWrapper::new(&self),
949        )))
950    }
951
952    /// Decode a 32-bit floating point value.
953    ///
954    /// # Examples
955    ///
956    /// Deriving an implementation:
957    ///
958    /// ```
959    /// use musli::Decode;
960    ///
961    /// #[derive(Decode)]
962    /// #[musli(packed)]
963    /// struct MyType {
964    ///     data: f32,
965    /// }
966    /// ```
967    ///
968    /// Implementing manually:
969    ///
970    /// ```
971    /// use musli::{Allocator, Decode, Decoder};
972    /// # struct MyType { data: f32 }
973    ///
974    /// impl<'de, M, A> Decode<'de, M, A> for MyType
975    /// where
976    ///     A: Allocator,
977    /// {
978    ///     #[inline]
979    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
980    ///     where
981    ///         D: Decoder<'de>,
982    ///     {
983    ///         Ok(Self {
984    ///             data: decoder.decode_f32()?,
985    ///         })
986    ///     }
987    /// }
988    /// ```
989    #[inline]
990    fn decode_f32(self) -> Result<f32, Self::Error> {
991        Err(self.cx().message(expecting::unsupported_type(
992            &expecting::Float32,
993            ExpectingWrapper::new(&self),
994        )))
995    }
996
997    /// Decode a 64-bit floating point value.
998    ///
999    /// # Examples
1000    ///
1001    /// Deriving an implementation:
1002    ///
1003    /// ```
1004    /// use musli::Decode;
1005    ///
1006    /// #[derive(Decode)]
1007    /// #[musli(packed)]
1008    /// struct MyType {
1009    ///     data: f64,
1010    /// }
1011    /// ```
1012    ///
1013    /// Implementing manually:
1014    ///
1015    /// ```
1016    /// use musli::{Allocator, Decode, Decoder};
1017    /// # struct MyType { data: f64 }
1018    ///
1019    /// impl<'de, M, A> Decode<'de, M, A> for MyType
1020    /// where
1021    ///     A: Allocator,
1022    /// {
1023    ///     #[inline]
1024    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
1025    ///     where
1026    ///         D: Decoder<'de>,
1027    ///     {
1028    ///         Ok(Self {
1029    ///             data: decoder.decode_f64()?,
1030    ///         })
1031    ///     }
1032    /// }
1033    /// ```
1034    #[inline]
1035    fn decode_f64(self) -> Result<f64, Self::Error> {
1036        Err(self.cx().message(expecting::unsupported_type(
1037            &expecting::Float64,
1038            ExpectingWrapper::new(&self),
1039        )))
1040    }
1041
1042    /// Decode a fixed-length array.
1043    ///
1044    /// # Examples
1045    ///
1046    /// Deriving an implementation:
1047    ///
1048    /// ```
1049    /// use musli::Decode;
1050    ///
1051    /// #[derive(Decode)]
1052    /// #[musli(packed)]
1053    /// struct MyType {
1054    ///     data: [u8; 128],
1055    /// }
1056    /// ```
1057    ///
1058    /// Implementing manually:
1059    ///
1060    /// ```
1061    /// use musli::{Allocator, Decode, Decoder};
1062    /// # struct MyType { data: [u8; 128] }
1063    ///
1064    /// impl<'de, M, A> Decode<'de, M, A> for MyType
1065    /// where
1066    ///     A: Allocator,
1067    /// {
1068    ///     #[inline]
1069    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
1070    ///     where
1071    ///         D: Decoder<'de>,
1072    ///     {
1073    ///         Ok(Self {
1074    ///             data: decoder.decode_array()?,
1075    ///         })
1076    ///     }
1077    /// }
1078    /// ```
1079    #[inline]
1080    fn decode_array<const N: usize>(self) -> Result<[u8; N], Self::Error> {
1081        Err(self.cx().message(expecting::unsupported_type(
1082            &expecting::Array,
1083            ExpectingWrapper::new(&self),
1084        )))
1085    }
1086
1087    /// Decode a sequence of bytes whos length is encoded in the payload.
1088    ///
1089    /// # Examples
1090    ///
1091    /// Deriving an implementation:
1092    ///
1093    /// ```
1094    /// use musli::Decode;
1095    ///
1096    /// #[derive(Decode)]
1097    /// #[musli(packed)]
1098    /// struct BytesReference<'de> {
1099    ///     data: &'de [u8],
1100    /// }
1101    /// ```
1102    ///
1103    /// Implementing manually:
1104    ///
1105    /// ```
1106    /// use std::fmt;
1107    ///
1108    /// use musli::{Allocator, Context, Decode, Decoder};
1109    /// use musli::de::UnsizedVisitor;
1110    /// # struct BytesReference<'de> { data: &'de [u8] }
1111    ///
1112    /// impl<'de, M, A> Decode<'de, M, A> for BytesReference<'de>
1113    /// where
1114    ///     A: Allocator,
1115    /// {
1116    ///     #[inline]
1117    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
1118    ///     where
1119    ///         D: Decoder<'de>,
1120    ///     {
1121    ///         struct Visitor;
1122    ///
1123    ///         #[musli::de::unsized_visitor]
1124    ///         impl<'de, C> UnsizedVisitor<'de, C, [u8]> for Visitor
1125    ///         where
1126    ///             C: Context
1127    ///         {
1128    ///             type Ok = &'de [u8];
1129    ///
1130    ///             #[inline]
1131    ///             fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1132    ///                 write!(f, "a literal byte reference")
1133    ///             }
1134    ///
1135    ///             #[inline]
1136    ///             fn visit_borrowed(self, cx: C, bytes: &'de [u8]) -> Result<Self::Ok, C::Error> {
1137    ///                 Ok(bytes)
1138    ///             }
1139    ///         }
1140    ///
1141    ///         Ok(Self {
1142    ///             data: decoder.decode_bytes(Visitor)?,
1143    ///         })
1144    ///     }
1145    /// }
1146    /// ```
1147    #[inline]
1148    fn decode_bytes<V>(self, visitor: V) -> Result<V::Ok, V::Error>
1149    where
1150        V: UnsizedVisitor<'de, Self::Cx, [u8], Error = Self::Error, Allocator = Self::Allocator>,
1151    {
1152        Err(self.cx().message(expecting::unsupported_type(
1153            &expecting::Bytes,
1154            ExpectingWrapper::new(&self),
1155        )))
1156    }
1157
1158    /// Decode a string slice from the current decoder.
1159    ///
1160    /// # Examples
1161    ///
1162    /// Deriving an implementation:
1163    ///
1164    /// ```
1165    /// use musli::Decode;
1166    ///
1167    /// #[derive(Decode)]
1168    /// #[musli(packed)]
1169    /// struct StringReference<'de> {
1170    ///     data: &'de str,
1171    /// }
1172    /// ```
1173    ///
1174    /// Implementing manually:
1175    ///
1176    /// ```
1177    /// use std::fmt;
1178    ///
1179    /// use musli::{Allocator, Context, Decode, Decoder};
1180    /// use musli::de::UnsizedVisitor;
1181    /// # struct StringReference<'de> { data: &'de str }
1182    ///
1183    /// impl<'de, M, A> Decode<'de, M, A> for StringReference<'de>
1184    /// where
1185    ///     A: Allocator,
1186    /// {
1187    ///     #[inline]
1188    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
1189    ///     where
1190    ///         D: Decoder<'de>,
1191    ///     {
1192    ///         struct Visitor;
1193    ///
1194    ///         #[musli::de::unsized_visitor]
1195    ///         impl<'de, C> UnsizedVisitor<'de, C, str> for Visitor
1196    ///         where
1197    ///             C: Context,
1198    ///         {
1199    ///             type Ok = &'de str;
1200    ///
1201    ///             #[inline]
1202    ///             fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1203    ///                 write!(f, "exact bytes reference")
1204    ///             }
1205    ///
1206    ///             #[inline]
1207    ///             fn visit_borrowed(self, _: C, bytes: &'de str) -> Result<Self::Ok, C::Error> {
1208    ///                 Ok(bytes)
1209    ///             }
1210    ///         }
1211    ///
1212    ///         Ok(Self {
1213    ///             data: decoder.decode_string(Visitor)?,
1214    ///         })
1215    ///     }
1216    /// }
1217    /// ```
1218    #[inline]
1219    fn decode_string<V>(self, visitor: V) -> Result<V::Ok, V::Error>
1220    where
1221        V: UnsizedVisitor<'de, Self::Cx, str, Error = Self::Error, Allocator = Self::Allocator>,
1222    {
1223        Err(self.cx().message(expecting::unsupported_type(
1224            &expecting::String,
1225            ExpectingWrapper::new(&self),
1226        )))
1227    }
1228
1229    /// Decode an optional value.
1230    ///
1231    /// # Examples
1232    ///
1233    /// Deriving an implementation:
1234    ///
1235    /// ```
1236    /// use musli::{Context, Decode};
1237    ///
1238    /// #[derive(Decode)]
1239    /// struct OptionalField {
1240    ///     data: Option<String>,
1241    /// }
1242    /// ```
1243    ///
1244    /// Implementing manually:
1245    ///
1246    /// ```
1247    /// use musli::{Allocator, Context, Decode, Decoder};
1248    /// # struct OptionalField { data: Option<String>}
1249    ///
1250    /// impl<'de, M, A> Decode<'de, M, A> for OptionalField
1251    /// where
1252    ///     A: Allocator,
1253    /// {
1254    ///     #[inline]
1255    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
1256    ///     where
1257    ///         D: Decoder<'de>,
1258    ///     {
1259    ///         let data = if let Some(decoder) = decoder.decode_option()? {
1260    ///             Some(decoder.decode()?)
1261    ///         } else {
1262    ///             None
1263    ///         };
1264    ///
1265    ///         Ok(Self { data })
1266    ///     }
1267    /// }
1268    /// ```
1269    #[inline]
1270    #[must_use = "Decoders must be consumed"]
1271    fn decode_option(self) -> Result<Option<Self::DecodeSome>, Self::Error> {
1272        Err(self.cx().message(expecting::unsupported_type(
1273            &expecting::Option,
1274            ExpectingWrapper::new(&self),
1275        )))
1276    }
1277
1278    /// Construct an unpack that can decode more than one element at a time.
1279    ///
1280    /// This hints to the format that it should attempt to decode all of the
1281    /// elements in the packed sequence from an as compact format as possible
1282    /// compatible with what's being returned by
1283    /// [Encoder::pack][crate::Encoder::encode_pack].
1284    ///
1285    /// # Examples
1286    ///
1287    /// Deriving an implementation:
1288    ///
1289    /// ```
1290    /// use musli::Decode;
1291    ///
1292    /// #[derive(Decode)]
1293    /// #[musli(packed)]
1294    /// struct PackedStruct {
1295    ///     field: u32,
1296    ///     data: [u8; 128],
1297    /// }
1298    /// ```
1299    ///
1300    /// Implementing manually:
1301    ///
1302    /// ```
1303    /// use musli::{Allocator, Context, Decode, Decoder};
1304    /// use musli::de::SequenceDecoder;
1305    /// # struct PackedStruct { field: u32, data: [u8; 128] }
1306    ///
1307    /// impl<'de, M, A> Decode<'de, M, A> for PackedStruct
1308    /// where
1309    ///     A: Allocator,
1310    /// {
1311    ///     #[inline]
1312    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
1313    ///     where
1314    ///         D: Decoder<'de>,
1315    ///     {
1316    ///         decoder.decode_pack(|pack| Ok(Self {
1317    ///             field: pack.next()?,
1318    ///             data: pack.next()?,
1319    ///         }))
1320    ///     }
1321    /// }
1322    /// ```
1323    #[inline]
1324    fn decode_pack<F, O>(self, f: F) -> Result<O, Self::Error>
1325    where
1326        F: FnOnce(&mut Self::DecodePack) -> Result<O, Self::Error>,
1327    {
1328        Err(self.cx().message(expecting::unsupported_type(
1329            &expecting::Pack,
1330            ExpectingWrapper::new(&self),
1331        )))
1332    }
1333
1334    /// Decode a sequence of values.
1335    #[inline]
1336    fn decode_slice<V, T>(self) -> Result<V, Self::Error>
1337    where
1338        V: DecodeSliceBuilder<T, Self::Allocator>,
1339        T: Decode<'de, Self::Mode, Self::Allocator>,
1340    {
1341        utils::default_decode_slice(self)
1342    }
1343
1344    /// Decode a sequence.
1345    ///
1346    /// # Examples
1347    ///
1348    /// Deriving an implementation:
1349    ///
1350    /// ```
1351    /// use musli::Decode;
1352    ///
1353    /// #[derive(Decode)]
1354    /// struct VectorField {
1355    ///     data: Vec<String>,
1356    /// }
1357    /// ```
1358    ///
1359    /// Implementing manually:
1360    ///
1361    /// ```
1362    /// use musli::{Allocator, Context, Decode, Decoder};
1363    /// use musli::de::SequenceDecoder;
1364    ///
1365    /// struct VectorField {
1366    ///     data: Vec<String>,
1367    /// }
1368    ///
1369    /// impl<'de, M, A> Decode<'de, M, A> for VectorField
1370    /// where
1371    ///     A: Allocator,
1372    /// {
1373    ///     #[inline]
1374    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
1375    ///     where
1376    ///         D: Decoder<'de>,
1377    ///     {
1378    ///         decoder.decode_sequence(|seq| {
1379    ///             let mut data = Vec::new();
1380    ///
1381    ///             while let Some(decoder) = seq.try_decode_next()? {
1382    ///                 data.push(decoder.decode()?);
1383    ///             }
1384    ///
1385    ///             Ok(Self { data })
1386    ///         })
1387    ///     }
1388    /// }
1389    /// ```
1390    ///
1391    /// Deriving an implementation for a tuple:
1392    ///
1393    /// ```
1394    /// use musli::Decode;
1395    ///
1396    /// #[derive(Decode)]
1397    /// struct TupleStruct(String, u32);
1398    /// ```
1399    ///
1400    /// Implementing manually:
1401    ///
1402    /// ```
1403    /// use musli::{Allocator, Context, Decode, Decoder};
1404    /// use musli::de::SequenceDecoder;
1405    /// # struct TupleStruct(String, u32);
1406    ///
1407    /// impl<'de, M, A> Decode<'de, M, A> for TupleStruct
1408    /// where
1409    ///     A: Allocator,
1410    /// {
1411    ///     #[inline]
1412    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
1413    ///     where
1414    ///         D: Decoder<'de>,
1415    ///     {
1416    ///         decoder.decode_sequence_hint(2, |tuple| {
1417    ///             Ok(Self(tuple.next()?, tuple.next()?))
1418    ///         })
1419    ///     }
1420    /// }
1421    /// ```
1422    #[inline]
1423    fn decode_sequence<F, O>(self, f: F) -> Result<O, Self::Error>
1424    where
1425        F: FnOnce(&mut Self::DecodeSequence) -> Result<O, Self::Error>,
1426    {
1427        Err(self.cx().message(expecting::unsupported_type(
1428            &expecting::UnsizedSequence,
1429            ExpectingWrapper::new(&self),
1430        )))
1431    }
1432
1433    /// Decode a sequence with a `hint` indicating its expected characteristics.
1434    ///
1435    /// # Examples
1436    ///
1437    /// ```
1438    /// use musli::{Allocator, Context, Decode, Decoder};
1439    /// use musli::de::SequenceDecoder;
1440    /// # struct TupleStruct(String, u32);
1441    ///
1442    /// impl<'de, M, A> Decode<'de, M, A> for TupleStruct
1443    /// where
1444    ///     A: Allocator,
1445    /// {
1446    ///     #[inline]
1447    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
1448    ///     where
1449    ///         D: Decoder<'de>,
1450    ///     {
1451    ///         decoder.decode_sequence_hint(2, |tuple| {
1452    ///             Ok(Self(tuple.next()?, tuple.next()?))
1453    ///         })
1454    ///     }
1455    /// }
1456    /// ```
1457    #[inline]
1458    fn decode_sequence_hint<F, O>(self, hint: impl SequenceHint, f: F) -> Result<O, Self::Error>
1459    where
1460        F: FnOnce(&mut Self::DecodeSequence) -> Result<O, Self::Error>,
1461    {
1462        self.decode_sequence(f)
1463    }
1464
1465    /// Decode a map who's size is not known at compile time.
1466    ///
1467    /// # Examples
1468    ///
1469    /// ```
1470    /// use std::collections::HashMap;
1471    ///
1472    /// use musli::{Allocator, Decode, Decoder};
1473    /// use musli::de::{MapDecoder, EntryDecoder};
1474    /// # struct MapStruct { data: HashMap<String, u32> }
1475    ///
1476    /// impl<'de, M, A> Decode<'de, M, A> for MapStruct
1477    /// where
1478    ///     A: Allocator,
1479    /// {
1480    ///     #[inline]
1481    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
1482    ///     where
1483    ///         D: Decoder<'de>,
1484    ///     {
1485    ///         decoder.decode_map(|map| {
1486    ///             let mut data = HashMap::with_capacity(map.size_hint().or_default());
1487    ///
1488    ///             while let Some((key, value)) = map.entry()? {
1489    ///                 data.insert(key, value);
1490    ///             }
1491    ///
1492    ///             Ok(Self { data })
1493    ///         })
1494    ///     }
1495    /// }
1496    /// ```
1497    fn decode_map<F, O>(self, f: F) -> Result<O, Self::Error>
1498    where
1499        F: FnOnce(&mut Self::DecodeMap) -> Result<O, Self::Error>,
1500    {
1501        Err(self.cx().message(expecting::unsupported_type(
1502            &expecting::UnsizedMap,
1503            ExpectingWrapper::new(&self),
1504        )))
1505    }
1506
1507    /// Decode a map using a simplified function.
1508    ///
1509    /// The length of the map must somehow be determined from the underlying
1510    /// format.
1511    ///
1512    /// # Examples
1513    ///
1514    /// Deriving an implementation from a struct:
1515    ///
1516    /// ```
1517    /// use std::collections::HashMap;
1518    ///
1519    /// use musli::Decode;
1520    ///
1521    /// #[derive(Decode)]
1522    /// struct Struct {
1523    ///     string: String,
1524    ///     integer: u32,
1525    /// }
1526    /// ```
1527    ///
1528    /// Implementing manually:
1529    ///
1530    /// ```
1531    /// use musli::{Allocator, Context, Decode, Decoder};
1532    /// use musli::de::{MapDecoder, EntryDecoder};
1533    ///
1534    /// struct Struct {
1535    ///     string: String,
1536    ///     integer: u32,
1537    /// }
1538    ///
1539    /// impl<'de, M, A> Decode<'de, M, A> for Struct
1540    /// where
1541    ///     A: Allocator,
1542    /// {
1543    ///     #[inline]
1544    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
1545    ///     where
1546    ///         D: Decoder<'de>,
1547    ///     {
1548    ///         static HINT: usize = 2;
1549    ///
1550    ///         let cx = decoder.cx();
1551    ///
1552    ///         decoder.decode_map_hint(HINT, |st| {
1553    ///             let mut string = None;
1554    ///             let mut integer = None;
1555    ///
1556    ///             while let Some(mut field) = st.decode_entry()? {
1557    ///                 // Note: to avoid allocating `decode_string` needs to be used with a visitor.
1558    ///                 let tag = field.decode_key()?.decode::<String>()?;
1559    ///
1560    ///                 match tag.as_str() {
1561    ///                     "string" => {
1562    ///                         string = Some(field.decode_value()?.decode()?);
1563    ///                     }
1564    ///                     "integer" => {
1565    ///                         integer = Some(field.decode_value()?.decode()?);
1566    ///                     }
1567    ///                     tag => {
1568    ///                         return Err(cx.message(format_args!("Invalid field tag {tag:?}")));
1569    ///                     }
1570    ///                 }
1571    ///             }
1572    ///
1573    ///             Ok(Self {
1574    ///                 string: string.ok_or_else(|| cx.message("Expected tag `string`"))?,
1575    ///                 integer: integer.ok_or_else(|| cx.message("Expected tag `integer`"))?,
1576    ///             })
1577    ///         })
1578    ///     }
1579    /// }
1580    /// ```
1581    #[inline]
1582    fn decode_map_hint<F, O>(self, _: impl MapHint, f: F) -> Result<O, Self::Error>
1583    where
1584        F: FnOnce(&mut Self::DecodeMap) -> Result<O, Self::Error>,
1585    {
1586        self.decode_map(f)
1587    }
1588
1589    /// Simplified decoding a map of unknown length.
1590    ///
1591    /// The length of the map must somehow be determined from the underlying
1592    /// format.
1593    #[inline]
1594    fn decode_map_entries<F, O>(self, f: F) -> Result<O, Self::Error>
1595    where
1596        F: FnOnce(&mut Self::DecodeMapEntries) -> Result<O, Self::Error>,
1597    {
1598        Err(self.cx().message(expecting::unsupported_type(
1599            &expecting::MapEntries,
1600            ExpectingWrapper::new(&self),
1601        )))
1602    }
1603
1604    /// Decode a variant using a closure.
1605    ///
1606    /// # Examples
1607    ///
1608    /// ```
1609    /// use musli::{Allocator, Context, Decode};
1610    /// use musli::de::{Decoder, VariantDecoder};
1611    ///
1612    /// enum Enum {
1613    ///     Number(u32),
1614    ///     String(String),
1615    /// }
1616    ///
1617    /// impl<'de, M, A> Decode<'de, M, A> for Enum
1618    /// where
1619    ///     A: Allocator,
1620    /// {
1621    ///     #[inline]
1622    ///     fn decode<D>(decoder: D) -> Result<Self, D::Error>
1623    ///     where
1624    ///         D: Decoder<'de>,
1625    ///     {
1626    ///         let cx = decoder.cx();
1627    ///
1628    ///         decoder.decode_variant(|variant| {
1629    ///             let tag = variant.decode_tag()?.decode()?;
1630    ///             let value = variant.decode_value()?;
1631    ///
1632    ///             match tag {
1633    ///                 0 => Ok(Self::Number(value.decode()?)),
1634    ///                 1 => Ok(Self::String(value.decode()?)),
1635    ///                 tag => Err(cx.message(format_args!("Invalid variant tag {tag:?}"))),
1636    ///             }
1637    ///         })
1638    ///     }
1639    /// }
1640    /// ```
1641    #[inline]
1642    fn decode_variant<F, O>(self, f: F) -> Result<O, Self::Error>
1643    where
1644        F: FnOnce(&mut Self::DecodeVariant) -> Result<O, Self::Error>,
1645    {
1646        Err(self.cx().message(expecting::unsupported_type(
1647            &expecting::Variant,
1648            ExpectingWrapper::new(&self),
1649        )))
1650    }
1651
1652    /// Decode an unknown number using a visitor.
1653    #[inline]
1654    fn decode_number<V>(self, visitor: V) -> Result<V::Ok, Self::Error>
1655    where
1656        V: Visitor<'de, Self::Cx, Error = Self::Error, Allocator = Self::Allocator>,
1657    {
1658        Err(self.cx().message(expecting::unsupported_type(
1659            &expecting::Number,
1660            ExpectingWrapper::new(&self),
1661        )))
1662    }
1663
1664    /// Decode dynamically through a [`Visitor`].
1665    #[inline]
1666    fn decode_any<V>(self, visitor: V) -> Result<V::Ok, V::Error>
1667    where
1668        V: Visitor<'de, Self::Cx, Error = Self::Error, Allocator = Self::Allocator>,
1669    {
1670        Err(self.cx().message(format_args!(
1671            "Any type not supported, expected {}",
1672            ExpectingWrapper::new(&self).format()
1673        )))
1674    }
1675}
1676
1677#[repr(transparent)]
1678struct ExpectingWrapper<T> {
1679    inner: T,
1680}
1681
1682impl<T> ExpectingWrapper<T> {
1683    fn new(inner: &T) -> &Self {
1684        // Safety: `ExpectingWrapper` is repr(transparent) over `T`.
1685        unsafe { &*(inner as *const T as *const Self) }
1686    }
1687}
1688
1689impl<'de, T> Expecting for ExpectingWrapper<T>
1690where
1691    T: Decoder<'de>,
1692{
1693    #[inline]
1694    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1695        self.inner.expecting(f)
1696    }
1697}