1use crate::Context;
23use super::{Decode, Decoder, EntriesDecoder, EntryDecoder, SizeHint};
45/// Trait governing how to decode a sequence of pairs.
6pub trait MapDecoder<'de> {
7/// Context associated with the decoder.
8type Cx: ?Sized + Context;
9/// The decoder to use for a key.
10type DecodeEntry<'this>: EntryDecoder<'de, Cx = Self::Cx>
11where
12Self: 'this;
13/// Decoder returned by [`MapDecoder::decode_remaining_entries`].
14type DecodeRemainingEntries<'this>: EntriesDecoder<'de, Cx = Self::Cx>
15where
16Self: 'this;
1718/// Get a size hint of known remaining elements.
19#[inline]
20fn size_hint(&self) -> SizeHint {
21 SizeHint::any()
22 }
2324/// Decode the next key. This returns `Ok(None)` where there are no more
25 /// elements to decode.
26#[must_use = "Decoders must be consumed"]
27fn decode_entry(
28&mut self,
29 ) -> Result<Option<Self::DecodeEntry<'_>>, <Self::Cx as Context>::Error>;
3031/// Return simplified decoder for remaining entries.
32fn decode_remaining_entries(
33&mut self,
34 ) -> Result<Self::DecodeRemainingEntries<'_>, <Self::Cx as Context>::Error>;
3536/// Decode the next map entry as a tuple.
37fn entry<K, V>(&mut self) -> Result<Option<(K, V)>, <Self::Cx as Context>::Error>
38where
39K: Decode<'de, <Self::Cx as Context>::Mode>,
40 V: Decode<'de, <Self::Cx as Context>::Mode>,
41 {
42let Some(mut entry) = self.decode_entry()? else {
43return Ok(None);
44 };
4546let key = entry.decode_key()?.decode()?;
47let value = entry.decode_value()?.decode()?;
48Ok(Some((key, value)))
49 }
50}