musli_core/de/
entries_decoder.rs

1use crate::Context;
2
3use super::{Decoder, SizeHint};
4
5/// Trait governing how to decode a sequence of map pairs.
6///
7/// This trait exists so that decoders can implement a mode that is compatible
8/// with serde deserialization.
9///
10/// If you do not intend to implement this, then serde compatibility for your
11/// format might be degraded.
12#[must_use = "Must call end_entries to complete decoding"]
13pub trait EntriesDecoder<'de> {
14    /// Context associated with the decoder.
15    type Cx: ?Sized + Context;
16    /// The decoder to use for a tuple field index.
17    type DecodeEntryKey<'this>: Decoder<
18        'de,
19        Cx = Self::Cx,
20        Error = <Self::Cx as Context>::Error,
21        Mode = <Self::Cx as Context>::Mode,
22    >
23    where
24        Self: 'this;
25    /// The decoder to use for a tuple field value.
26    type DecodeEntryValue<'this>: Decoder<
27        'de,
28        Cx = Self::Cx,
29        Error = <Self::Cx as Context>::Error,
30        Mode = <Self::Cx as Context>::Mode,
31    >
32    where
33        Self: 'this;
34
35    /// Get a size hint for the size of the map being decoded.
36    #[inline]
37    fn size_hint(&self) -> SizeHint {
38        SizeHint::any()
39    }
40
41    /// Try to return the decoder for the first value in the pair.
42    ///
43    /// If this is a map the first value would be the key of the map, if this is
44    /// a struct the first value would be the field of the struct.
45    #[must_use = "Decoders must be consumed"]
46    fn decode_entry_key(
47        &mut self,
48    ) -> Result<Option<Self::DecodeEntryKey<'_>>, <Self::Cx as Context>::Error>;
49
50    /// Decode the value in the map.
51    #[must_use = "Decoders must be consumed"]
52    fn decode_entry_value(
53        &mut self,
54    ) -> Result<Self::DecodeEntryValue<'_>, <Self::Cx as Context>::Error>;
55
56    /// End entries decoding.
57    fn end_entries(self) -> Result<(), <Self::Cx as Context>::Error>;
58}