musli_core/de/
map_decoder.rs

1use crate::{Allocator, Context};
2
3use super::{Decode, Decoder, EntriesDecoder, EntryDecoder, SizeHint};
4
5/// Trait governing how to decode a sequence of pairs.
6pub trait MapDecoder<'de> {
7    /// Context associated with the decoder.
8    type Cx: Context<Error = Self::Error, Allocator = Self::Allocator>;
9    /// Error associated with decoding.
10    type Error;
11    /// The allocator associated with the decoder.
12    type Allocator: Allocator;
13    /// The mode of the decoder.
14    type Mode: 'static;
15    /// The decoder to use for a key.
16    type DecodeEntry<'this>: EntryDecoder<
17            'de,
18            Cx = Self::Cx,
19            Error = Self::Error,
20            Allocator = Self::Allocator,
21            Mode = Self::Mode,
22        >
23    where
24        Self: 'this;
25    /// Decoder returned by [`MapDecoder::decode_remaining_entries`].
26    type DecodeRemainingEntries<'this>: EntriesDecoder<
27            'de,
28            Cx = Self::Cx,
29            Error = Self::Error,
30            Allocator = Self::Allocator,
31            Mode = Self::Mode,
32        >
33    where
34        Self: 'this;
35
36    /// Access the context associated with the decoder.
37    fn cx(&self) -> Self::Cx;
38
39    /// Get a size hint of known remaining elements.
40    #[inline]
41    fn size_hint(&self) -> SizeHint {
42        SizeHint::any()
43    }
44
45    /// Decode the next key. This returns `Ok(None)` where there are no more
46    /// elements to decode.
47    #[must_use = "Decoders must be consumed"]
48    fn decode_entry(&mut self) -> Result<Option<Self::DecodeEntry<'_>>, Self::Error>;
49
50    /// Return simplified decoder for remaining entries.
51    fn decode_remaining_entries(&mut self)
52    -> Result<Self::DecodeRemainingEntries<'_>, Self::Error>;
53
54    /// Decode the next map entry as a tuple.
55    fn entry<K, V>(&mut self) -> Result<Option<(K, V)>, Self::Error>
56    where
57        K: Decode<'de, Self::Mode, Self::Allocator>,
58        V: Decode<'de, Self::Mode, Self::Allocator>,
59    {
60        let Some(mut entry) = self.decode_entry()? else {
61            return Ok(None);
62        };
63
64        let key = entry.decode_key()?.decode()?;
65        let value = entry.decode_value()?.decode()?;
66        Ok(Some((key, value)))
67    }
68}