musli_core/de/
map_decoder.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::Context;

use super::{Decode, Decoder, EntriesDecoder, EntryDecoder, SizeHint};

/// Trait governing how to decode a sequence of pairs.
pub trait MapDecoder<'de> {
    /// Context associated with the decoder.
    type Cx: ?Sized + Context;
    /// The decoder to use for a key.
    type DecodeEntry<'this>: EntryDecoder<'de, Cx = Self::Cx>
    where
        Self: 'this;
    /// Decoder returned by [`MapDecoder::decode_remaining_entries`].
    type DecodeRemainingEntries<'this>: EntriesDecoder<'de, Cx = Self::Cx>
    where
        Self: 'this;

    /// Get a size hint of known remaining elements.
    #[inline]
    fn size_hint(&self) -> SizeHint {
        SizeHint::any()
    }

    /// Decode the next key. This returns `Ok(None)` where there are no more
    /// elements to decode.
    #[must_use = "Decoders must be consumed"]
    fn decode_entry(
        &mut self,
    ) -> Result<Option<Self::DecodeEntry<'_>>, <Self::Cx as Context>::Error>;

    /// Return simplified decoder for remaining entries.
    fn decode_remaining_entries(
        &mut self,
    ) -> Result<Self::DecodeRemainingEntries<'_>, <Self::Cx as Context>::Error>;

    /// Decode the next map entry as a tuple.
    fn entry<K, V>(&mut self) -> Result<Option<(K, V)>, <Self::Cx as Context>::Error>
    where
        K: Decode<'de, <Self::Cx as Context>::Mode>,
        V: Decode<'de, <Self::Cx as Context>::Mode>,
    {
        let Some(mut entry) = self.decode_entry()? else {
            return Ok(None);
        };

        let key = entry.decode_key()?.decode()?;
        let value = entry.decode_value()?.decode()?;
        Ok(Some((key, value)))
    }
}