musli_core/en/
map_encoder.rs

1use core::fmt;
2
3use crate::Context;
4use crate::hint::MapHint;
5
6use super::{Encode, Encoder, EntryEncoder};
7
8/// Encoder for a map.
9pub trait MapEncoder {
10    /// Context associated with the encoder.
11    type Cx: Context<Error = Self::Error>;
12    /// Error associated with encoding.
13    type Error;
14    /// The mode of the encoder.
15    type Mode: 'static;
16    /// Encode the next pair.
17    type EncodeEntry<'this>: EntryEncoder<Cx = Self::Cx, Error = Self::Error, Mode = Self::Mode>
18    where
19        Self: 'this;
20
21    /// Access the context associated with the encoder.
22    fn cx(&self) -> Self::Cx;
23
24    /// Encode the next map entry.
25    #[must_use = "Encoders must be consumed"]
26    fn encode_entry(&mut self) -> Result<Self::EncodeEntry<'_>, Self::Error>;
27
28    /// Simplified encoder for a map entry, which ensures that encoding is
29    /// always finished.
30    #[inline]
31    fn encode_entry_fn<F>(&mut self, f: F) -> Result<(), Self::Error>
32    where
33        F: FnOnce(&mut Self::EncodeEntry<'_>) -> Result<(), Self::Error>,
34    {
35        let mut encoder = self.encode_entry()?;
36        f(&mut encoder)?;
37        encoder.finish_entry()
38    }
39
40    /// Insert a map entry.
41    #[inline]
42    fn insert_entry<F, S>(&mut self, key: F, value: S) -> Result<(), Self::Error>
43    where
44        F: Encode<Self::Mode>,
45        S: Encode<Self::Mode>,
46    {
47        self.encode_entry()?.insert_entry(key, value)?;
48        Ok(())
49    }
50
51    /// Encode a map entry.
52    #[inline]
53    fn as_encoder(&mut self) -> AsEncoder<'_, Self>
54    where
55        Self: Sized,
56    {
57        AsEncoder { encoder: self }
58    }
59
60    /// Finish encoding a map.
61    fn finish_map(self) -> Result<(), Self::Error>;
62}
63
64/// Encoder for a map.
65pub struct AsEncoder<'a, E>
66where
67    E: ?Sized,
68{
69    encoder: &'a mut E,
70}
71
72#[crate::trait_defaults(crate)]
73impl<'a, E> Encoder for AsEncoder<'a, E>
74where
75    E: MapEncoder,
76{
77    type Cx = E::Cx;
78    type Error = E::Error;
79    type Mode = E::Mode;
80    type EncodeMap = AsEncoderEncodeMap<'a, E>;
81
82    #[inline]
83    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84        write!(f, "a map")
85    }
86
87    #[inline]
88    fn cx(&self) -> Self::Cx {
89        self.encoder.cx()
90    }
91
92    #[inline]
93    fn encode_map(self, _: impl MapHint) -> Result<Self::EncodeMap, Self::Error> {
94        Ok(AsEncoderEncodeMap {
95            encoder: self.encoder,
96        })
97    }
98}
99
100pub struct AsEncoderEncodeMap<'a, E>
101where
102    E: ?Sized,
103{
104    encoder: &'a mut E,
105}
106
107impl<E> MapEncoder for AsEncoderEncodeMap<'_, E>
108where
109    E: ?Sized + MapEncoder,
110{
111    type Cx = E::Cx;
112    type Error = E::Error;
113    type Mode = E::Mode;
114    type EncodeEntry<'this>
115        = E::EncodeEntry<'this>
116    where
117        Self: 'this;
118
119    #[inline]
120    fn cx(&self) -> Self::Cx {
121        self.encoder.cx()
122    }
123
124    #[inline]
125    fn encode_entry(&mut self) -> Result<Self::EncodeEntry<'_>, Self::Error> {
126        self.encoder.encode_entry()
127    }
128
129    #[inline]
130    fn finish_map(self) -> Result<(), Self::Error> {
131        Ok(())
132    }
133}