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 {
7    /// Context associated with the encoder.
8    type Cx: ?Sized + Context;
9    /// Result type of the encoder.
10    type Ok;
11    /// The encoder returned when advancing the map encoder to encode the key.
12    type EncodeKey<'this>: Encoder<
13        Cx = Self::Cx,
14        Ok = Self::Ok,
15        Error = <Self::Cx as Context>::Error,
16        Mode = <Self::Cx as Context>::Mode,
17    >
18    where
19        Self: 'this;
20    /// The encoder returned when advancing the map encoder to encode the value.
21    type EncodeValue<'this>: Encoder<
22        Cx = Self::Cx,
23        Ok = Self::Ok,
24        Error = <Self::Cx as Context>::Error,
25        Mode = <Self::Cx as Context>::Mode,
26    >
27    where
28        Self: 'this;
29
30    /// Return the encoder for the key in the entry.
31    #[must_use = "Encoders must be consumed"]
32    fn encode_key(&mut self) -> Result<Self::EncodeKey<'_>, <Self::Cx as Context>::Error>;
33
34    /// Return encoder for value in the entry.
35    #[must_use = "Encoders must be consumed"]
36    fn encode_value(&mut self) -> Result<Self::EncodeValue<'_>, <Self::Cx as Context>::Error>;
37
38    /// Stop encoding this pair.
39    fn finish_entry(self) -> Result<Self::Ok, <Self::Cx as Context>::Error>;
40
41    /// Insert the pair immediately.
42    #[inline]
43    fn insert_entry<K, V>(
44        mut self,
45        key: K,
46        value: V,
47    ) -> Result<Self::Ok, <Self::Cx as Context>::Error>
48    where
49        Self: Sized,
50        K: Encode<<Self::Cx as Context>::Mode>,
51        V: Encode<<Self::Cx as Context>::Mode>,
52    {
53        self.encode_key()?.encode(key)?;
54        self.encode_value()?.encode(value)?;
55        self.finish_entry()
56    }
57}