1use crate::Context;
23use super::{Encode, Encoder};
45/// Trait governing how to encode a map entry.
6///
7/// This trait exists so that decoders can implement a mode that is compatible
8/// with serde serialization.
9///
10/// If you do not intend to implement this, then serde compatibility for your
11/// format might be degraded.
12pub trait EntriesEncoder {
13/// Context associated with the encoder.
14type Cx: ?Sized + Context;
15/// Result type of the encoder.
16type Ok;
17/// The encoder returned when advancing the map encoder to encode the key.
18type EncodeEntryKey<'this>: Encoder<
19 Cx = Self::Cx,
20Ok = Self::Ok,
21 Error = <Self::Cx as Context>::Error,
22 Mode = <Self::Cx as Context>::Mode,
23 >
24where
25Self: 'this;
26/// The encoder returned when advancing the map encoder to encode the value.
27type EncodeEntryValue<'this>: Encoder<
28 Cx = Self::Cx,
29Ok = Self::Ok,
30 Error = <Self::Cx as Context>::Error,
31 Mode = <Self::Cx as Context>::Mode,
32 >
33where
34Self: 'this;
3536/// Return the encoder for the key in the entry.
37#[must_use = "Encoders must be consumed"]
38fn encode_entry_key(
39&mut self,
40 ) -> Result<Self::EncodeEntryKey<'_>, <Self::Cx as Context>::Error>;
4142/// Return encoder for value in the entry.
43#[must_use = "Encoders must be consumed"]
44fn encode_entry_value(
45&mut self,
46 ) -> Result<Self::EncodeEntryValue<'_>, <Self::Cx as Context>::Error>;
4748/// Complete encoding map entries.
49fn finish_entries(self) -> Result<Self::Ok, <Self::Cx as Context>::Error>;
5051/// Insert the pair immediately.
52#[inline]
53fn insert_entry<K, V>(&mut self, key: K, value: V) -> Result<(), <Self::Cx as Context>::Error>
54where
55K: Encode<<Self::Cx as Context>::Mode>,
56 V: Encode<<Self::Cx as Context>::Mode>,
57 {
58self.encode_entry_key()?.encode(key)?;
59self.encode_entry_value()?.encode(value)?;
60Ok(())
61 }
62}