musli_core/en/
entries_encoder.rs

1use crate::Context;
2
3use super::{Encode, Encoder};
4
5/// 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.
14    type Cx: ?Sized + Context;
15    /// Result type of the encoder.
16    type Ok;
17    /// The encoder returned when advancing the map encoder to encode the key.
18    type EncodeEntryKey<'this>: Encoder<
19        Cx = Self::Cx,
20        Ok = Self::Ok,
21        Error = <Self::Cx as Context>::Error,
22        Mode = <Self::Cx as Context>::Mode,
23    >
24    where
25        Self: 'this;
26    /// The encoder returned when advancing the map encoder to encode the value.
27    type EncodeEntryValue<'this>: Encoder<
28        Cx = Self::Cx,
29        Ok = Self::Ok,
30        Error = <Self::Cx as Context>::Error,
31        Mode = <Self::Cx as Context>::Mode,
32    >
33    where
34        Self: 'this;
35
36    /// Return the encoder for the key in the entry.
37    #[must_use = "Encoders must be consumed"]
38    fn encode_entry_key(
39        &mut self,
40    ) -> Result<Self::EncodeEntryKey<'_>, <Self::Cx as Context>::Error>;
41
42    /// Return encoder for value in the entry.
43    #[must_use = "Encoders must be consumed"]
44    fn encode_entry_value(
45        &mut self,
46    ) -> Result<Self::EncodeEntryValue<'_>, <Self::Cx as Context>::Error>;
47
48    /// Complete encoding map entries.
49    fn finish_entries(self) -> Result<Self::Ok, <Self::Cx as Context>::Error>;
50
51    /// Insert the pair immediately.
52    #[inline]
53    fn insert_entry<K, V>(&mut self, key: K, value: V) -> Result<(), <Self::Cx as Context>::Error>
54    where
55        K: Encode<<Self::Cx as Context>::Mode>,
56        V: Encode<<Self::Cx as Context>::Mode>,
57    {
58        self.encode_entry_key()?.encode(key)?;
59        self.encode_entry_value()?.encode(value)?;
60        Ok(())
61    }
62}