musli_core/en/
map_encoder.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::Context;

use super::{Encode, EntryEncoder};

/// Encoder for a map.
pub trait MapEncoder {
    /// Context associated with the encoder.
    type Cx: ?Sized + Context;
    /// Result type of the encoder.
    type Ok;
    /// Encode the next pair.
    type EncodeEntry<'this>: EntryEncoder<Cx = Self::Cx, Ok = Self::Ok>
    where
        Self: 'this;

    /// Encode the next pair.
    #[must_use = "Encoders must be consumed"]
    fn encode_entry(&mut self) -> Result<Self::EncodeEntry<'_>, <Self::Cx as Context>::Error>;

    /// Simplified encoder for a map entry, which ensures that encoding is
    /// always finished.
    #[inline]
    fn encode_entry_fn<F>(&mut self, f: F) -> Result<Self::Ok, <Self::Cx as Context>::Error>
    where
        F: FnOnce(&mut Self::EncodeEntry<'_>) -> Result<(), <Self::Cx as Context>::Error>,
    {
        let mut encoder = self.encode_entry()?;
        f(&mut encoder)?;
        encoder.finish_entry()
    }

    /// Insert a pair immediately.
    #[inline]
    fn insert_entry<F, S>(&mut self, key: F, value: S) -> Result<(), <Self::Cx as Context>::Error>
    where
        Self: Sized,
        F: Encode<<Self::Cx as Context>::Mode>,
        S: Encode<<Self::Cx as Context>::Mode>,
    {
        self.encode_entry()?.insert_entry(key, value)?;
        Ok(())
    }

    /// Finish encoding pairs.
    fn finish_map(self) -> Result<Self::Ok, <Self::Cx as Context>::Error>;
}