musli_core/en/
entry_encoder.rs

1use crate::Context;
2
3use super::{Encode, Encoder};
4
5/// Trait governing how to encode a map entry.
6pub trait EntryEncoder: Sized {
7    /// Context associated with the encoder.
8    type Cx: Context<Error = Self::Error>;
9    /// Error associated with encoding.
10    type Error;
11    /// The mode of the encoder.
12    type Mode: 'static;
13    /// The encoder returned when advancing the map encoder to encode the key.
14    type EncodeKey<'this>: Encoder<Cx = Self::Cx, Error = Self::Error, Mode = Self::Mode>
15    where
16        Self: 'this;
17    /// The encoder returned when advancing the map encoder to encode the value.
18    type EncodeValue<'this>: Encoder<Cx = Self::Cx, Error = Self::Error, Mode = Self::Mode>
19    where
20        Self: 'this;
21
22    /// Access the context associated with the encoder.
23    fn cx(&self) -> Self::Cx;
24
25    /// Return the encoder for the key in the entry.
26    #[must_use = "Encoders must be consumed"]
27    fn encode_key(&mut self) -> Result<Self::EncodeKey<'_>, Self::Error>;
28
29    /// Return encoder for value in the entry.
30    #[must_use = "Encoders must be consumed"]
31    fn encode_value(&mut self) -> Result<Self::EncodeValue<'_>, Self::Error>;
32
33    /// Stop encoding this pair.
34    fn finish_entry(self) -> Result<(), Self::Error>;
35
36    /// Insert the pair immediately.
37    #[inline]
38    fn insert_entry<K, V>(mut self, key: K, value: V) -> Result<(), Self::Error>
39    where
40        K: Encode<Self::Mode>,
41        V: Encode<Self::Mode>,
42    {
43        self.encode_key()?.encode(key)?;
44        self.encode_value()?.encode(value)?;
45        self.finish_entry()
46    }
47}