musli_value/
en.rs

1use core::fmt;
2
3#[cfg(feature = "alloc")]
4use alloc::boxed::Box;
5#[cfg(feature = "alloc")]
6use alloc::vec::Vec;
7
8use musli::en::{Encode, Encoder};
9#[cfg(feature = "alloc")]
10use musli::en::{EntriesEncoder, EntryEncoder, MapEncoder, SequenceEncoder, VariantEncoder};
11#[cfg(feature = "alloc")]
12use musli::hint::{MapHint, SequenceHint};
13#[cfg(feature = "alloc")]
14use musli::Buf;
15use musli::Context;
16#[cfg(feature = "alloc")]
17use musli_storage::en::StorageEncoder;
18#[cfg(feature = "alloc")]
19use musli_utils::writer::BufWriter;
20use musli_utils::Options;
21
22use crate::value::{Number, Value};
23
24/// Insert a value into the given receiver.
25trait ValueOutput {
26    fn write(self, value: Value);
27}
28
29impl ValueOutput for &mut Value {
30    #[inline]
31    fn write(self, value: Value) {
32        *self = value;
33    }
34}
35
36#[cfg(feature = "alloc")]
37impl ValueOutput for &mut Vec<Value> {
38    #[inline]
39    fn write(self, value: Value) {
40        self.push(value);
41    }
42}
43
44/// Writer which writes an optional value that is present.
45#[cfg(feature = "alloc")]
46pub struct SomeValueWriter<O> {
47    output: O,
48}
49
50#[cfg(feature = "alloc")]
51impl<O> ValueOutput for SomeValueWriter<O>
52where
53    O: ValueOutput,
54{
55    fn write(self, value: Value) {
56        self.output.write(Value::Option(Some(Box::new(value))));
57    }
58}
59
60/// Encoder for a single value.
61pub struct ValueEncoder<'a, const OPT: Options, O, C: ?Sized> {
62    cx: &'a C,
63    output: O,
64}
65
66impl<'a, const OPT: Options, O, C: ?Sized> ValueEncoder<'a, OPT, O, C> {
67    #[inline]
68    pub(crate) fn new(cx: &'a C, output: O) -> Self {
69        Self { cx, output }
70    }
71}
72
73#[musli::encoder]
74impl<'a, const OPT: Options, O, C> Encoder for ValueEncoder<'a, OPT, O, C>
75where
76    O: ValueOutput,
77    C: ?Sized + Context,
78{
79    type Cx = C;
80    type Error = C::Error;
81    type Ok = ();
82    type Mode = C::Mode;
83    type WithContext<'this, U> = ValueEncoder<'this, OPT, O, U> where U: 'this + Context;
84    #[cfg(feature = "alloc")]
85    type EncodeSome = ValueEncoder<'a, OPT, SomeValueWriter<O>, C>;
86    #[cfg(feature = "alloc")]
87    type EncodePack = PackValueEncoder<'a, OPT, O, C>;
88    #[cfg(feature = "alloc")]
89    type EncodeSequence = SequenceValueEncoder<'a, OPT, O, C>;
90    #[cfg(feature = "alloc")]
91    type EncodeMap = MapValueEncoder<'a, OPT, O, C>;
92    #[cfg(feature = "alloc")]
93    type EncodeMapEntries = MapValueEncoder<'a, OPT, O, C>;
94    #[cfg(feature = "alloc")]
95    type EncodeVariant = VariantValueEncoder<'a, OPT, O, C>;
96    #[cfg(feature = "alloc")]
97    type EncodeSequenceVariant = VariantSequenceEncoder<'a, OPT, O, C>;
98    #[cfg(feature = "alloc")]
99    type EncodeMapVariant = VariantStructEncoder<'a, OPT, O, C>;
100
101    #[inline]
102    fn cx(&self) -> &C {
103        self.cx
104    }
105
106    #[inline]
107    fn with_context<U>(self, cx: &U) -> Result<Self::WithContext<'_, U>, C::Error>
108    where
109        U: Context,
110    {
111        Ok(ValueEncoder::new(cx, self.output))
112    }
113
114    #[inline]
115    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
116        write!(f, "value that can be encoded")
117    }
118
119    #[inline]
120    fn encode<T>(self, value: T) -> Result<Self::Ok, C::Error>
121    where
122        T: Encode<Self::Mode>,
123    {
124        value.encode(self.cx, self)
125    }
126
127    #[inline]
128    fn encode_unit(self) -> Result<Self::Ok, C::Error> {
129        Ok(())
130    }
131
132    #[inline]
133    fn encode_bool(self, b: bool) -> Result<Self::Ok, C::Error> {
134        self.output.write(Value::Bool(b));
135        Ok(())
136    }
137
138    #[inline]
139    fn encode_char(self, c: char) -> Result<Self::Ok, C::Error> {
140        self.output.write(Value::Char(c));
141        Ok(())
142    }
143
144    #[inline]
145    fn encode_u8(self, n: u8) -> Result<Self::Ok, C::Error> {
146        self.output.write(Value::Number(Number::U8(n)));
147        Ok(())
148    }
149
150    #[inline]
151    fn encode_u16(self, n: u16) -> Result<Self::Ok, C::Error> {
152        self.output.write(Value::Number(Number::U16(n)));
153        Ok(())
154    }
155
156    #[inline]
157    fn encode_u32(self, n: u32) -> Result<Self::Ok, C::Error> {
158        self.output.write(Value::Number(Number::U32(n)));
159        Ok(())
160    }
161
162    #[inline]
163    fn encode_u64(self, n: u64) -> Result<Self::Ok, C::Error> {
164        self.output.write(Value::Number(Number::U64(n)));
165        Ok(())
166    }
167
168    #[inline]
169    fn encode_u128(self, n: u128) -> Result<Self::Ok, C::Error> {
170        self.output.write(Value::Number(Number::U128(n)));
171        Ok(())
172    }
173
174    #[inline]
175    fn encode_i8(self, n: i8) -> Result<Self::Ok, C::Error> {
176        self.output.write(Value::Number(Number::I8(n)));
177        Ok(())
178    }
179
180    #[inline]
181    fn encode_i16(self, n: i16) -> Result<Self::Ok, C::Error> {
182        self.output.write(Value::Number(Number::I16(n)));
183        Ok(())
184    }
185
186    #[inline]
187    fn encode_i32(self, n: i32) -> Result<Self::Ok, C::Error> {
188        self.output.write(Value::Number(Number::I32(n)));
189        Ok(())
190    }
191
192    #[inline]
193    fn encode_i64(self, n: i64) -> Result<Self::Ok, C::Error> {
194        self.output.write(Value::Number(Number::I64(n)));
195        Ok(())
196    }
197
198    #[inline]
199    fn encode_i128(self, n: i128) -> Result<Self::Ok, C::Error> {
200        self.output.write(Value::Number(Number::I128(n)));
201        Ok(())
202    }
203
204    #[inline]
205    fn encode_usize(self, n: usize) -> Result<Self::Ok, C::Error> {
206        self.output.write(Value::Number(Number::Usize(n)));
207        Ok(())
208    }
209
210    #[inline]
211    fn encode_isize(self, n: isize) -> Result<Self::Ok, C::Error> {
212        self.output.write(Value::Number(Number::Isize(n)));
213        Ok(())
214    }
215
216    #[inline]
217    fn encode_f32(self, n: f32) -> Result<Self::Ok, C::Error> {
218        self.output.write(Value::Number(Number::F32(n)));
219        Ok(())
220    }
221
222    #[inline]
223    fn encode_f64(self, n: f64) -> Result<Self::Ok, C::Error> {
224        self.output.write(Value::Number(Number::F64(n)));
225        Ok(())
226    }
227
228    #[cfg(feature = "alloc")]
229    #[inline]
230    fn encode_array<const N: usize>(self, array: &[u8; N]) -> Result<Self::Ok, C::Error> {
231        self.output.write(Value::Bytes(array.into()));
232        Ok(())
233    }
234
235    #[cfg(feature = "alloc")]
236    #[inline]
237    fn encode_bytes(self, bytes: &[u8]) -> Result<Self::Ok, C::Error> {
238        self.output.write(Value::Bytes(bytes.to_vec()));
239        Ok(())
240    }
241
242    #[cfg(feature = "alloc")]
243    #[inline]
244    fn encode_bytes_vectored<I>(self, len: usize, vectors: I) -> Result<Self::Ok, C::Error>
245    where
246        I: IntoIterator,
247        I::Item: AsRef<[u8]>,
248    {
249        let mut bytes = Vec::with_capacity(len);
250
251        for b in vectors {
252            bytes.extend_from_slice(b.as_ref());
253        }
254
255        self.output.write(Value::Bytes(bytes));
256        Ok(())
257    }
258
259    #[cfg(feature = "alloc")]
260    #[inline]
261    fn encode_string(self, string: &str) -> Result<Self::Ok, C::Error> {
262        self.output.write(Value::String(string.into()));
263        Ok(())
264    }
265
266    #[inline]
267    fn collect_string<T>(self, value: &T) -> Result<Self::Ok, <Self::Cx as Context>::Error>
268    where
269        T: ?Sized + fmt::Display,
270    {
271        let buf = self.cx.collect_string(value)?;
272        self.encode_string(buf.as_ref())
273    }
274
275    #[cfg(feature = "alloc")]
276    #[inline]
277    fn encode_some(self) -> Result<Self::EncodeSome, C::Error> {
278        Ok(ValueEncoder::new(
279            self.cx,
280            SomeValueWriter {
281                output: self.output,
282            },
283        ))
284    }
285
286    #[cfg(feature = "alloc")]
287    #[inline]
288    fn encode_none(self) -> Result<Self::Ok, C::Error> {
289        self.output.write(Value::Option(None));
290        Ok(())
291    }
292
293    #[cfg(feature = "alloc")]
294    #[inline]
295    fn encode_pack(self) -> Result<Self::EncodePack, C::Error> {
296        PackValueEncoder::new(self.cx, self.output)
297    }
298
299    #[cfg(feature = "alloc")]
300    #[inline]
301    fn encode_sequence(self, _: &SequenceHint) -> Result<Self::EncodeSequence, C::Error> {
302        Ok(SequenceValueEncoder::new(self.cx, self.output))
303    }
304
305    #[cfg(feature = "alloc")]
306    #[inline]
307    fn encode_map(self, _: &MapHint) -> Result<Self::EncodeMap, C::Error> {
308        Ok(MapValueEncoder::new(self.cx, self.output))
309    }
310
311    #[cfg(feature = "alloc")]
312    #[inline]
313    fn encode_map_entries(self, _: &MapHint) -> Result<Self::EncodeMapEntries, C::Error> {
314        Ok(MapValueEncoder::new(self.cx, self.output))
315    }
316
317    #[cfg(feature = "alloc")]
318    #[inline]
319    fn encode_variant(self) -> Result<Self::EncodeVariant, C::Error> {
320        Ok(VariantValueEncoder::new(self.cx, self.output))
321    }
322
323    #[cfg(feature = "alloc")]
324    #[inline]
325    fn encode_unit_variant<T>(self, tag: &T) -> Result<(), C::Error>
326    where
327        T: ?Sized + Encode<C::Mode>,
328    {
329        let mut variant = self.encode_variant()?;
330        variant.encode_tag()?.encode(tag)?;
331        variant.encode_data()?.encode_unit()?;
332        variant.finish_variant()?;
333        Ok(())
334    }
335
336    #[inline]
337    #[cfg(feature = "alloc")]
338    fn encode_sequence_variant<T>(
339        self,
340        tag: &T,
341        hint: &SequenceHint,
342    ) -> Result<Self::EncodeSequenceVariant, C::Error>
343    where
344        T: ?Sized + Encode<C::Mode>,
345    {
346        let mut variant = Value::Unit;
347        ValueEncoder::<OPT, _, _>::new(self.cx, &mut variant).encode(tag)?;
348
349        Ok(VariantSequenceEncoder::new(
350            self.cx,
351            self.output,
352            variant,
353            hint.size,
354        ))
355    }
356
357    #[cfg(feature = "alloc")]
358    #[inline]
359    fn encode_map_variant<T>(
360        self,
361        tag: &T,
362        hint: &MapHint,
363    ) -> Result<Self::EncodeMapVariant, C::Error>
364    where
365        T: ?Sized + Encode<C::Mode>,
366    {
367        let mut variant = Value::Unit;
368        ValueEncoder::<OPT, _, _>::new(self.cx, &mut variant).encode(tag)?;
369
370        Ok(VariantStructEncoder::new(
371            self.cx,
372            self.output,
373            variant,
374            hint.size,
375        ))
376    }
377}
378
379/// A sequence encoder.
380#[cfg(feature = "alloc")]
381pub struct SequenceValueEncoder<'a, const OPT: Options, O, C: ?Sized> {
382    cx: &'a C,
383    output: O,
384    values: Vec<Value>,
385}
386
387#[cfg(feature = "alloc")]
388impl<'a, const OPT: Options, O, C: ?Sized> SequenceValueEncoder<'a, OPT, O, C> {
389    #[inline]
390    fn new(cx: &'a C, output: O) -> Self {
391        Self {
392            cx,
393            output,
394            values: Vec::new(),
395        }
396    }
397}
398
399#[cfg(feature = "alloc")]
400impl<'a, const OPT: Options, O, C> SequenceEncoder for SequenceValueEncoder<'a, OPT, O, C>
401where
402    O: ValueOutput,
403    C: ?Sized + Context,
404{
405    type Cx = C;
406    type Ok = ();
407
408    type EncodeNext<'this> = ValueEncoder<'a, OPT, &'this mut Vec<Value>, C>
409    where
410        Self: 'this;
411
412    #[inline]
413    fn encode_next(&mut self) -> Result<Self::EncodeNext<'_>, C::Error> {
414        Ok(ValueEncoder::new(self.cx, &mut self.values))
415    }
416
417    #[inline]
418    fn finish_sequence(self) -> Result<Self::Ok, C::Error> {
419        self.output.write(Value::Sequence(self.values));
420        Ok(())
421    }
422}
423
424/// A pack encoder.
425#[cfg(feature = "alloc")]
426pub struct PackValueEncoder<'a, const OPT: Options, O, C>
427where
428    C: ?Sized + Context,
429{
430    cx: &'a C,
431    output: O,
432    writer: BufWriter<C::Buf<'a>>,
433}
434
435#[cfg(feature = "alloc")]
436impl<'a, const OPT: Options, O, C> PackValueEncoder<'a, OPT, O, C>
437where
438    C: ?Sized + Context,
439{
440    #[inline]
441    fn new(cx: &'a C, output: O) -> Result<Self, C::Error> {
442        let Some(buf) = cx.alloc() else {
443            return Err(cx.message("Failed to allocate buffer"));
444        };
445
446        Ok(Self {
447            cx,
448            output,
449            writer: BufWriter::new(buf),
450        })
451    }
452}
453
454#[cfg(feature = "alloc")]
455impl<'a, const OPT: Options, O, C> SequenceEncoder for PackValueEncoder<'a, OPT, O, C>
456where
457    O: ValueOutput,
458    C: ?Sized + Context,
459{
460    type Cx = C;
461    type Ok = ();
462
463    type EncodeNext<'this> = StorageEncoder<'a, &'this mut BufWriter<C::Buf<'a>>, OPT, C>
464    where
465        Self: 'this;
466
467    #[inline]
468    fn encode_next(&mut self) -> Result<Self::EncodeNext<'_>, C::Error> {
469        Ok(StorageEncoder::new(self.cx, &mut self.writer))
470    }
471
472    #[inline]
473    fn finish_sequence(self) -> Result<Self::Ok, C::Error> {
474        let buf = self.writer.into_inner();
475        self.output.write(Value::Bytes(buf.as_slice().into()));
476        Ok(())
477    }
478}
479
480/// A pairs encoder.
481#[cfg(feature = "alloc")]
482pub struct MapValueEncoder<'a, const OPT: Options, O, C: ?Sized> {
483    cx: &'a C,
484    output: O,
485    values: Vec<(Value, Value)>,
486}
487
488#[cfg(feature = "alloc")]
489impl<'a, const OPT: Options, O, C: ?Sized> MapValueEncoder<'a, OPT, O, C> {
490    #[inline]
491    fn new(cx: &'a C, output: O) -> Self {
492        Self {
493            cx,
494            output,
495            values: Vec::new(),
496        }
497    }
498}
499
500#[cfg(feature = "alloc")]
501impl<'a, const OPT: Options, O, C> MapEncoder for MapValueEncoder<'a, OPT, O, C>
502where
503    O: ValueOutput,
504    C: ?Sized + Context,
505{
506    type Cx = C;
507    type Ok = ();
508    type EncodeEntry<'this> = PairValueEncoder<'this, OPT, C>
509    where
510        Self: 'this;
511
512    #[inline]
513    fn encode_entry(&mut self) -> Result<Self::EncodeEntry<'_>, C::Error> {
514        Ok(PairValueEncoder::new(self.cx, &mut self.values))
515    }
516
517    #[inline]
518    fn finish_map(self) -> Result<Self::Ok, C::Error> {
519        self.output.write(Value::Map(self.values));
520        Ok(())
521    }
522}
523
524#[cfg(feature = "alloc")]
525impl<'a, const OPT: Options, O, C> EntriesEncoder for MapValueEncoder<'a, OPT, O, C>
526where
527    O: ValueOutput,
528    C: ?Sized + Context,
529{
530    type Cx = C;
531    type Ok = ();
532    type EncodeEntryKey<'this> = ValueEncoder<'a, OPT, &'this mut Value, C>
533    where
534        Self: 'this;
535    type EncodeEntryValue<'this> = ValueEncoder<'a, OPT, &'this mut Value, C>
536    where
537        Self: 'this;
538
539    #[inline]
540    fn encode_entry_key(&mut self) -> Result<Self::EncodeEntryKey<'_>, C::Error> {
541        self.values.push((Value::Unit, Value::Unit));
542
543        let Some((key, _)) = self.values.last_mut() else {
544            return Err(self.cx.message("Pair has not been encoded"));
545        };
546
547        Ok(ValueEncoder::new(self.cx, key))
548    }
549
550    #[inline]
551    fn encode_entry_value(&mut self) -> Result<Self::EncodeEntryValue<'_>, C::Error> {
552        let Some((_, value)) = self.values.last_mut() else {
553            return Err(self.cx.message("Pair has not been encoded"));
554        };
555
556        Ok(ValueEncoder::new(self.cx, value))
557    }
558
559    #[inline]
560    fn finish_entries(self) -> Result<Self::Ok, C::Error> {
561        self.output.write(Value::Map(self.values));
562        Ok(())
563    }
564}
565
566/// A pairs encoder.
567#[cfg(feature = "alloc")]
568pub struct PairValueEncoder<'a, const OPT: Options, C: ?Sized> {
569    cx: &'a C,
570    output: &'a mut Vec<(Value, Value)>,
571    pair: (Value, Value),
572}
573
574#[cfg(feature = "alloc")]
575impl<'a, const OPT: Options, C: ?Sized> PairValueEncoder<'a, OPT, C> {
576    #[inline]
577    fn new(cx: &'a C, output: &'a mut Vec<(Value, Value)>) -> Self {
578        Self {
579            cx,
580            output,
581            pair: (Value::Unit, Value::Unit),
582        }
583    }
584}
585
586#[cfg(feature = "alloc")]
587impl<'a, const OPT: Options, C> EntryEncoder for PairValueEncoder<'a, OPT, C>
588where
589    C: ?Sized + Context,
590{
591    type Cx = C;
592    type Ok = ();
593    type EncodeKey<'this> = ValueEncoder<'a, OPT, &'this mut Value, C>
594    where
595        Self: 'this;
596    type EncodeValue<'this> = ValueEncoder<'a, OPT, &'this mut Value, C> where Self: 'this;
597
598    #[inline]
599    fn encode_key(&mut self) -> Result<Self::EncodeKey<'_>, C::Error> {
600        Ok(ValueEncoder::new(self.cx, &mut self.pair.0))
601    }
602
603    #[inline]
604    fn encode_value(&mut self) -> Result<Self::EncodeValue<'_>, C::Error> {
605        Ok(ValueEncoder::new(self.cx, &mut self.pair.1))
606    }
607
608    #[inline]
609    fn finish_entry(self) -> Result<Self::Ok, C::Error> {
610        self.output.push(self.pair);
611        Ok(())
612    }
613}
614
615/// A pairs encoder.
616#[cfg(feature = "alloc")]
617pub struct VariantValueEncoder<'a, const OPT: Options, O, C: ?Sized> {
618    cx: &'a C,
619    output: O,
620    pair: (Value, Value),
621}
622
623#[cfg(feature = "alloc")]
624impl<'a, const OPT: Options, O, C: ?Sized> VariantValueEncoder<'a, OPT, O, C> {
625    #[inline]
626    fn new(cx: &'a C, output: O) -> Self {
627        Self {
628            cx,
629            output,
630            pair: (Value::Unit, Value::Unit),
631        }
632    }
633}
634
635#[cfg(feature = "alloc")]
636impl<'a, const OPT: Options, O, C> VariantEncoder for VariantValueEncoder<'a, OPT, O, C>
637where
638    O: ValueOutput,
639    C: ?Sized + Context,
640{
641    type Cx = C;
642    type Ok = ();
643    type EncodeTag<'this> = ValueEncoder<'a, OPT, &'this mut Value, C>
644    where
645        Self: 'this;
646    type EncodeData<'this> = ValueEncoder<'a, OPT, &'this mut Value, C>
647    where
648        Self: 'this;
649
650    #[inline]
651    fn encode_tag(&mut self) -> Result<Self::EncodeTag<'_>, C::Error> {
652        Ok(ValueEncoder::new(self.cx, &mut self.pair.0))
653    }
654
655    #[inline]
656    fn encode_data(&mut self) -> Result<Self::EncodeData<'_>, C::Error> {
657        Ok(ValueEncoder::new(self.cx, &mut self.pair.1))
658    }
659
660    #[inline]
661    fn finish_variant(self) -> Result<Self::Ok, C::Error> {
662        self.output.write(Value::Variant(Box::new(self.pair)));
663        Ok(())
664    }
665}
666
667/// A variant sequence encoder.
668#[cfg(feature = "alloc")]
669pub struct VariantSequenceEncoder<'a, const OPT: Options, O, C: ?Sized> {
670    cx: &'a C,
671    output: O,
672    variant: Value,
673    values: Vec<Value>,
674}
675
676#[cfg(feature = "alloc")]
677impl<'a, const OPT: Options, O, C: ?Sized> VariantSequenceEncoder<'a, OPT, O, C> {
678    #[inline]
679    fn new(cx: &'a C, output: O, variant: Value, len: usize) -> Self {
680        Self {
681            cx,
682            output,
683            variant,
684            values: Vec::with_capacity(len),
685        }
686    }
687}
688
689#[cfg(feature = "alloc")]
690impl<'a, const OPT: Options, O, C> SequenceEncoder for VariantSequenceEncoder<'a, OPT, O, C>
691where
692    O: ValueOutput,
693    C: ?Sized + Context,
694{
695    type Cx = C;
696    type Ok = ();
697
698    type EncodeNext<'this> = ValueEncoder<'a, OPT, &'this mut Vec<Value>, C>
699    where
700        Self: 'this;
701
702    #[inline]
703    fn encode_next(&mut self) -> Result<Self::EncodeNext<'_>, C::Error> {
704        Ok(ValueEncoder::new(self.cx, &mut self.values))
705    }
706
707    #[inline]
708    fn finish_sequence(self) -> Result<Self::Ok, C::Error> {
709        self.output.write(Value::Variant(Box::new((
710            self.variant,
711            Value::Sequence(self.values),
712        ))));
713        Ok(())
714    }
715}
716
717/// A variant struct encoder.
718#[cfg(feature = "alloc")]
719pub struct VariantStructEncoder<'a, const OPT: Options, O, C: ?Sized> {
720    cx: &'a C,
721    output: O,
722    variant: Value,
723    fields: Vec<(Value, Value)>,
724}
725
726#[cfg(feature = "alloc")]
727impl<'a, const OPT: Options, O, C: ?Sized> VariantStructEncoder<'a, OPT, O, C> {
728    #[inline]
729    fn new(cx: &'a C, output: O, variant: Value, len: usize) -> Self {
730        Self {
731            cx,
732            output,
733            variant,
734            fields: Vec::with_capacity(len),
735        }
736    }
737}
738
739#[cfg(feature = "alloc")]
740impl<'a, const OPT: Options, O, C> MapEncoder for VariantStructEncoder<'a, OPT, O, C>
741where
742    O: ValueOutput,
743    C: ?Sized + Context,
744{
745    type Cx = C;
746    type Ok = ();
747
748    type EncodeEntry<'this> = PairValueEncoder<'this, OPT, C>
749    where
750        Self: 'this;
751
752    #[inline]
753    fn encode_entry(&mut self) -> Result<Self::EncodeEntry<'_>, C::Error> {
754        Ok(PairValueEncoder::new(self.cx, &mut self.fields))
755    }
756
757    #[inline]
758    fn finish_map(self) -> Result<Self::Ok, C::Error> {
759        self.output.write(Value::Variant(Box::new((
760            self.variant,
761            Value::Map(self.fields),
762        ))));
763        Ok(())
764    }
765}