musli_core/de/entries_decoder.rs
1use crate::{Allocator, 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: Context<Error = Self::Error, Allocator = Self::Allocator>;
16 /// Error associated with decoding.
17 type Error;
18 /// The allocator associated with the decoder.
19 type Allocator: Allocator;
20 /// The mode of the decoder.
21 type Mode: 'static;
22 /// The decoder to use for a tuple field index.
23 type DecodeEntryKey<'this>: Decoder<
24 'de,
25 Cx = Self::Cx,
26 Error = Self::Error,
27 Allocator = Self::Allocator,
28 Mode = Self::Mode,
29 >
30 where
31 Self: 'this;
32 /// The decoder to use for a tuple field value.
33 type DecodeEntryValue<'this>: Decoder<
34 'de,
35 Cx = Self::Cx,
36 Error = Self::Error,
37 Allocator = Self::Allocator,
38 Mode = Self::Mode,
39 >
40 where
41 Self: 'this;
42
43 /// Access the context associated with the decoder.
44 fn cx(&self) -> Self::Cx;
45
46 /// Get a size hint for the size of the map being decoded.
47 #[inline]
48 fn size_hint(&self) -> SizeHint {
49 SizeHint::any()
50 }
51
52 /// Try to return the decoder for the first value in the pair.
53 ///
54 /// If this is a map the first value would be the key of the map, if this is
55 /// a struct the first value would be the field of the struct.
56 #[must_use = "Decoders must be consumed"]
57 fn decode_entry_key(&mut self) -> Result<Option<Self::DecodeEntryKey<'_>>, Self::Error>;
58
59 /// Decode the value in the map.
60 #[must_use = "Decoders must be consumed"]
61 fn decode_entry_value(&mut self) -> Result<Self::DecodeEntryValue<'_>, Self::Error>;
62
63 /// End entries decoding.
64 fn end_entries(self) -> Result<(), Self::Error>;
65}