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