deser-core 0.10.2

Core traits and types of deser, use the deser crate instead
Documentation
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
//! Adapters that accept or write values in another form: [`OneOrMany`],
//! [`PickFirst`], [`MapAsPairs`] and [`PairsAsMap`].
use alloc::borrow::Cow;
use alloc::collections::{BTreeMap, BTreeSet, VecDeque};
use alloc::string::String;
use alloc::vec::Vec;
#[cfg(feature = "std")]
use core::hash::{BuildHasher, Hash};
use core::marker::PhantomData;
use core::mem::take;
#[cfg(feature = "std")]
use std::collections::{HashMap, HashSet};

use crate::State;
use crate::Text;
use crate::adapted::{self, Adapted};
use crate::adapters::Same;
use crate::de::impls::{MapOut, MapTarget, map_sink};
use crate::de::recording::{Capture, RecordBuf};
use crate::de::update::Collection;
use crate::de::{CollectedErrors, Deserialize, DuplicateKeys, Sink, SinkHandle};
use crate::error::{Error, ErrorKind};
use crate::event::{Atom, ContainerShape, Order};
use crate::extensions::EventData;
use crate::ser::impls::{MapIterEmitter, handle_as};
use crate::ser::{Emit, IndexedSeq, IndexedSeqEmitter, SeqEmitter, Serialize, SerializeHandle};

/// A collection that also accepts a single element.
///
/// When deserializing, a sequence is the collection and every other value
/// (an atom or a map) is a single element, which becomes a collection of
/// one.  The elements are deserialized with the adapter `A` (by default
/// [`Same`]).  This is for data that writes a list with one element as the
/// element itself (`"tags": "a"` and `"tags": ["a", "b"]`).  As a sequence
/// is always the collection, elements cannot be sequences themselves.
///
/// When serializing, `F` decides the form: [`PreferOne`] (the default)
/// writes a collection with exactly one element as that element and
/// [`PreferMany`] always writes a sequence.
///
/// Supported are `Vec<T>`, `VecDeque<T>`, `BTreeSet<T>` and `HashSet<T>`.
///
/// ```
/// use deser::adapters::{OneOrMany, PreferMany};
/// use deser::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize, Debug, PartialEq)]
/// pub struct Rule {
///     #[deser(as = OneOrMany)]
///     hosts: Vec<String>,
///     #[deser(as = OneOrMany<_, PreferMany>)]
///     ports: Vec<u16>,
/// }
///
/// let rule: Rule = deser_json::from_str(r#"{"hosts": "a", "ports": 80}"#).unwrap();
/// assert_eq!(rule.hosts, ["a"]);
/// assert_eq!(rule.ports, [80]);
/// assert_eq!(
///     deser_json::to_string(&rule).unwrap(),
///     r#"{"hosts":"a","ports":[80]}"#
/// );
/// ```
///
/// In formats where keys can be given more than once (like query strings,
/// see [`ContainerShape::set_multimap`]) the values of all occurrences of
/// the key are collected, as without the adapter.
pub struct OneOrMany<A = Same, F = PreferOne>(PhantomData<fn() -> (A, F)>);

/// Serializes a [`OneOrMany`] collection with one element as the element.
pub struct PreferOne;

/// Serializes a [`OneOrMany`] collection always as sequence.
pub struct PreferMany;

mod sealed {
    pub trait Sealed {}
    impl Sealed for super::PreferOne {}
    impl Sealed for super::PreferMany {}
}

/// The forms of [`OneOrMany`]: [`PreferOne`] and [`PreferMany`].
///
/// This trait is sealed.
pub trait OneOrManyForm: sealed::Sealed + 'static {
    #[doc(hidden)]
    const PREFER_ONE: bool;
}

impl OneOrManyForm for PreferOne {
    const PREFER_ONE: bool = true;
}

impl OneOrManyForm for PreferMany {
    const PREFER_ONE: bool = false;
}

/// The sink of [`OneOrMany`].
///
/// The first event decides if the value is the collection or a single
/// element.
enum OneOrManySink<'a, 'de, C> {
    Pending(&'a mut Option<C>),
    Active(SinkHandle<'a, 'de>),
}

impl<'a, 'de, C: Send> OneOrManySink<'a, 'de, C> {
    /// Returns the sink of the value, creates it if needed.
    ///
    /// For a sequence that is the sink of the collection (with the adapter
    /// `M`), otherwise the sink of an element that is added to the
    /// collection.
    fn active<T, A, M>(&mut self, many: bool, state: &mut State) -> &mut SinkHandle<'a, 'de>
    where
        C: Collection<T>,
        T: Send + 'a,
        A: Deserialize<'de, T>,
        M: Deserialize<'de, C>,
    {
        if let OneOrManySink::Pending(..) = self
            && let OneOrManySink::Pending(out) =
                core::mem::replace(self, OneOrManySink::Active(SinkHandle::null()))
        {
            *self = OneOrManySink::Active(if many {
                M::deserialize_into(out, state)
            } else {
                crate::de::update::collect_into::<C, T, A>(out, state)
            });
        }
        match self {
            OneOrManySink::Active(sink) => sink,
            OneOrManySink::Pending(..) => unreachable!(),
        }
    }
}

/// Returns `true` if the atom is the collection itself.
///
/// This is the case for bytes if the elements are bytes (and strings,
/// which are bytes in formats without native bytes).
fn atom_is_many<'de, T: Send, A: Deserialize<'de, T>>(atom: &Atom) -> bool {
    A::__private_is_bytes() && matches!(atom, Atom::Bytes(_) | Atom::Str(_))
}

impl<'a, 'de, C, T, A, M> Sink<'de> for Adapted<(T, A, M), OneOrManySink<'a, 'de, C>>
where
    C: Collection<T>,
    T: Send + 'a,
    A: Deserialize<'de, T>,
    M: Deserialize<'de, C>,
{
    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        let many = atom_is_many::<T, A>(&atom);
        self.active::<T, A, M>(many, state).atom(atom, state)
    }

    fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
        let many = atom_is_many::<T, A>(&atom);
        self.active::<T, A, M>(many, state)
            .borrowed_atom(atom, state)
    }

    fn map(&mut self, state: &mut State) -> Result<(), Error> {
        self.active::<T, A, M>(false, state).map(state)
    }

    fn seq(&mut self, state: &mut State) -> Result<(), Error> {
        self.active::<T, A, M>(true, state).seq(state)
    }

    fn __private_seq(&mut self, state: &mut State) -> Result<bool, Error> {
        self.active::<T, A, M>(true, state).__private_seq(state)
    }

    fn __private_inline_atom(
        &mut self,
        index: usize,
        atom: Atom,
        state: &mut State,
    ) -> Result<(), Error> {
        self.active::<T, A, M>(true, state)
            .__private_inline_atom(index, atom, state)
    }

    fn __private_inline_event(
        &mut self,
        event: crate::de::InlineEvent,
        state: &mut State,
    ) -> Result<(), Error> {
        self.active::<T, A, M>(true, state)
            .__private_inline_event(event, state)
    }

    fn next_key(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        self.active::<T, A, M>(false, state).next_key(state)
    }

    fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        self.active::<T, A, M>(false, state).next_value(state)
    }

    fn __private_key_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.active::<T, A, M>(false, state)
            .__private_key_atom(atom, state)
    }

    fn __private_value_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.active::<T, A, M>(false, state)
            .__private_value_atom(atom, state)
    }

    fn __private_borrowed_key_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        self.active::<T, A, M>(false, state)
            .__private_borrowed_key_atom(atom, state)
    }

    fn __private_borrowed_value_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        self.active::<T, A, M>(false, state)
            .__private_borrowed_value_atom(atom, state)
    }

    fn value_for_key(
        &mut self,
        key: &str,
        state: &mut State,
    ) -> Result<Option<SinkHandle<'_, 'de>>, Error> {
        self.active::<T, A, M>(false, state)
            .value_for_key(key, state)
    }

    fn recover(&mut self, err: Error, state: &mut State) -> Result<(), Error> {
        self.active::<T, A, M>(false, state).recover(err, state)
    }

    fn finish(&mut self, state: &mut State) -> Result<(), Error> {
        self.active::<T, A, M>(false, state).finish(state)
    }

    fn expecting(&self) -> Cow<'_, str> {
        match &**self {
            OneOrManySink::Active(sink) => sink.expecting(),
            OneOrManySink::Pending(..) => M::expecting(),
        }
    }
}

/// Implements `OneOrMany` for collections.
///
/// The entries are `[bounds] [serialize bounds] Collection<T> =>
/// Collection<A>;` where the last part is the adapter for the collection
/// when it's a sequence.
macro_rules! one_or_many_impls {
    ($([$($bound:tt)*] [$($ser_bound:tt)*] $target:ty => $many:ty;)*) => {
        $(
            impl<'de, $($bound)*, A: Deserialize<'de, T>, F: OneOrManyForm>
                Deserialize<'de, $target> for OneOrMany<A, F>
            {
                fn deserialize_into<'out>(
                    out: &'out mut Option<$target>,
                    state: &mut State,
                ) -> SinkHandle<'out, 'de> {
                    adapted::arena_sink::<(T, A, $many), _>(OneOrManySink::Pending(out), state)
                }

                fn expecting() -> Cow<'static, str> {
                    <$many as Deserialize<'de, $target>>::expecting()
                }

                fn initial_value() -> Option<$target> {
                    <$many as Deserialize<'de, $target>>::initial_value()
                }

                #[inline]
                fn __private_atom_into(
                    out: &mut Option<$target>,
                    atom: Atom,
                    state: &mut State,
                ) -> Result<(), Error> {
                    if atom_is_many::<T, A>(&atom) {
                        return <$many as Deserialize<'de, $target>>::__private_atom_into(
                            out, atom, state,
                        );
                    }
                    let mut element = None;
                    A::__private_atom_into(&mut element, atom, state)?;
                    add_element(out, element)
                }

                #[inline]
                fn __private_borrowed_atom_into(
                    out: &mut Option<$target>,
                    atom: Atom<'de>,
                    state: &mut State,
                ) -> Result<(), Error> {
                    if atom_is_many::<T, A>(&atom) {
                        return <$many as Deserialize<'de, $target>>::__private_borrowed_atom_into(
                            out, atom, state,
                        );
                    }
                    let mut element = None;
                    A::__private_borrowed_atom_into(&mut element, atom, state)?;
                    add_element(out, element)
                }

                // the occurrences of a repeated key are collected like
                // without the adapter

                fn __private_collects() -> bool {
                    <$many as Deserialize<'de, $target>>::__private_collects()
                }

                fn __private_collect_into<'out>(
                    out: &'out mut Option<$target>,
                    state: &mut State,
                ) -> SinkHandle<'out, 'de> {
                    <$many as Deserialize<'de, $target>>::__private_collect_into(out, state)
                }

                fn __private_collect_update<'out>(
                    value: &'out mut $target,
                    first: bool,
                    state: &mut State,
                ) -> SinkHandle<'out, 'de> {
                    <$many as Deserialize<'de, $target>>::__private_collect_update(
                        value, first, state,
                    )
                }

                fn __private_collect_empty() -> Option<$target> {
                    <$many as Deserialize<'de, $target>>::__private_collect_empty()
                }
            }

            impl<$($ser_bound)*, A: Serialize<T>, F: OneOrManyForm> Serialize<$target>
                for OneOrMany<A, F>
            {
                fn serialize<'a>(
                    value: &'a $target,
                    _state: &mut State,
                ) -> Result<Emit<'a>, Error> {
                    let mut iter = value.iter();
                    Ok(Emit::Forward(match (iter.next(), iter.next()) {
                        (Some(element), None) if F::PREFER_ONE => handle_as::<A, T>(element),
                        _ => handle_as::<$many, $target>(value),
                    }))
                }

                fn describe(value: &$target, d: &mut dyn crate::ser::Describe) {
                    <$many as Serialize<$target>>::describe(value, d)
                }
            }
        )*
    };
}

/// Adds a single element to a new collection.
#[inline]
fn add_element<C: Collection<T>, T>(out: &mut Option<C>, element: Option<T>) -> Result<(), Error> {
    if let Some(element) = element {
        let mut collection = C::empty();
        collection.add(element)?;
        *out = Some(collection);
    }
    Ok(())
}

one_or_many_impls! {
    [T: Send] [T: Sync] Vec<T> => Vec<A>;
    [T: Send] [T: Sync] VecDeque<T> => VecDeque<A>;
    [T: Ord + Send] [T: Sync] BTreeSet<T> => BTreeSet<A>;
}

#[cfg(feature = "std")]
one_or_many_impls! {
    [T: Hash + Eq + Send, H: BuildHasher + Default + Send]
    [T: Sync, H: BuildHasher + Sync]
    HashSet<T, H> => HashSet<A>;
}

/// Tries adapters in order until one of them accepts the value.
///
/// The adapters are given as a tuple of two to four adapters, `_` stands
/// for [`Same`].  When deserializing, the value is delivered to the
/// adapters in order and the first one that accepts it wins.  Values other
/// than atoms are recorded and replayed for every adapter (like for
/// untagged enums, see [`Recording`](crate::de::Recording)), which keeps
/// their source locations.  If no adapter accepts the value, the error of
/// the first adapter is reported.  Missing values are handled by the first
/// adapter.  Serialization uses the first adapter.
///
/// The most common use is to accept a value in more than one form, like
/// numbers that are also sent as strings:
///
/// ```
/// use deser::adapters::{DisplayFromStr, PickFirst};
/// use deser::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize, Debug, PartialEq)]
/// pub struct Item {
///     #[deser(as = PickFirst<(_, DisplayFromStr)>)]
///     id: u64,
/// }
///
/// let a: Item = deser_json::from_str(r#"{"id": 42}"#).unwrap();
/// let b: Item = deser_json::from_str(r#"{"id": "42"}"#).unwrap();
/// assert_eq!(a, b);
/// assert_eq!(deser_json::to_string(&b).unwrap(), r#"{"id":42}"#);
/// ```
pub struct PickFirst<A>(PhantomData<fn() -> A>);

/// The value that is delivered to the adapters of [`PickFirst`].
enum PickInput<'t, 'de> {
    Atom(Atom<'t>),
    Borrowed(Atom<'de>),
    Recorded(&'t RecordBuf<'de>),
}

/// Delivers the value to one of the adapters of a [`PickFirst`].
type Attempt<'de, T> =
    for<'t> fn(&mut Option<T>, &PickInput<'t, 'de>, &EventData, &mut State) -> Result<(), Error>;

/// The adapters of a [`PickFirst`] (up to four).
struct Attempts<'de, T> {
    list: [Attempt<'de, T>; 4],
    len: usize,
}

impl<'de, T: Send> Attempts<'de, T> {
    fn new(list: &[Attempt<'de, T>]) -> Attempts<'de, T> {
        let mut rv = Attempts {
            list: [list[0]; 4],
            len: list.len(),
        };
        rv.list[..list.len()].copy_from_slice(list);
        rv
    }

    /// Delivers the value to the adapters until one of them accepts it.
    ///
    /// If none does, the value is delivered to the first adapter once more
    /// to report its error.
    fn pick(
        &self,
        out: &mut Option<T>,
        input: PickInput<'_, 'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        // sinks can take event data (like CBOR tags), every adapter gets
        // the data of the event.  Replaying a recording restores the event
        // data of the recorded events.
        let data = match input {
            PickInput::Recorded(_) => EventData::new(),
            _ => state.extensions().capture_event_data(),
        };
        for attempt in &self.list[..self.len] {
            let mut value = None;
            // only whether the adapter accepts the value matters
            let rv = state.discard_errors(|state| attempt(&mut value, &input, &data, state));
            if rv.is_ok() && value.is_some() {
                *out = value;
                return Ok(());
            }
        }
        (self.list[0])(out, &input, &data, state)
    }
}

/// Delivers the value to the adapter `A`.
fn pick_attempt<'de, T: Send, A: Deserialize<'de, T>>(
    out: &mut Option<T>,
    input: &PickInput<'_, 'de>,
    data: &EventData,
    state: &mut State,
) -> Result<(), Error> {
    if !data.is_empty() {
        state.extensions_mut().restore_event_data(data);
    }
    match *input {
        PickInput::Atom(ref atom) => A::__private_atom_into(out, atom.as_borrowed(), state),
        PickInput::Borrowed(ref atom) => A::__private_borrowed_atom_into(out, atom.clone(), state),
        PickInput::Recorded(buffer) => buffer.replay(A::deserialize_into(out, state), state),
    }
}

/// The sink of [`PickFirst`] passes the value on to this.
struct PickCapture<'a, 'de, T> {
    out: &'a mut Option<T>,
    attempts: Attempts<'de, T>,
    expecting: fn() -> Cow<'static, str>,
}

impl<'a, 'de, T: Send> Capture<'de, RecordBuf<'de>> for PickCapture<'a, 'de, T> {
    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.attempts.pick(self.out, PickInput::Atom(atom), state)
    }

    fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
        self.attempts
            .pick(self.out, PickInput::Borrowed(atom), state)
    }

    fn recorded(&mut self, buffer: RecordBuf<'de>, state: &mut State) -> Result<(), Error> {
        self.attempts
            .pick(self.out, PickInput::Recorded(&buffer), state)
    }

    fn expecting(&self) -> Cow<'_, str> {
        (self.expecting)()
    }
}

macro_rules! pick_first_impls {
    ($(($first:ident $(, $adapter:ident)+);)*) => {
        $(
            impl<'de, T: Send, $first: Deserialize<'de, T>, $($adapter: Deserialize<'de, T>),+>
                Deserialize<'de, T> for PickFirst<($first, $($adapter),+)>
            {
                fn deserialize_into<'out>(
                    out: &'out mut Option<T>,
                    state: &mut State,
                ) -> SinkHandle<'out, 'de> {
                    RecordBuf::capture_with(
                        PickCapture {
                            out,
                            attempts: pick_first_impls!(@attempts $first $(, $adapter)+),
                            expecting: <Self as Deserialize<'de, T>>::expecting,
                        },
                        state,
                    )
                }

                fn expecting() -> Cow<'static, str> {
                    let mut rv = String::from($first::expecting());
                    $(
                        rv.push_str(" or ");
                        rv.push_str(&$adapter::expecting());
                    )+
                    Cow::Owned(rv)
                }

                fn initial_value() -> Option<T> {
                    $first::initial_value()
                }

                #[inline]
                fn __private_atom_into(
                    out: &mut Option<T>,
                    atom: Atom,
                    state: &mut State,
                ) -> Result<(), Error> {
                    pick_first_impls!(@attempts $first $(, $adapter)+)
                        .pick(out, PickInput::Atom(atom), state)
                }

                #[inline]
                fn __private_borrowed_atom_into(
                    out: &mut Option<T>,
                    atom: Atom<'de>,
                    state: &mut State,
                ) -> Result<(), Error> {
                    pick_first_impls!(@attempts $first $(, $adapter)+)
                        .pick(out, PickInput::Borrowed(atom), state)
                }
            }

            impl<T: ?Sized, $first: Serialize<T>, $($adapter),+> Serialize<T>
                for PickFirst<($first, $($adapter),+)>
            {
                fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error> {
                    $first::serialize(value, state)
                }

                fn finish(value: &T, state: &mut State) -> Result<(), Error> {
                    $first::finish(value, state)
                }

                fn is_optional(value: &T) -> bool {
                    $first::is_optional(value)
                }

                fn container_shape(value: &T) -> ContainerShape {
                    $first::container_shape(value)
                }

                fn describe(value: &T, d: &mut dyn crate::ser::Describe) {
                    $first::describe(value, d)
                }

                #[inline]
                fn __private_begin<'a>(
                    value: &'a T,
                    state: &mut State,
                ) -> Result<crate::ser::Begin<'a>, Error> {
                    $first::__private_begin(value, state)
                }

                fn __private_slice_as_bytes(val: &[T]) -> Option<Cow<'_, [u8]>>
                where
                    T: Sized,
                {
                    $first::__private_slice_as_bytes(val)
                }
            }
        )*
    };
    (@attempts $($adapter:ident),+) => {
        Attempts::new(&[$(pick_attempt::<T, $adapter> as Attempt<'de, T>),+])
    };
}

pick_first_impls! {
    (A1, A2);
    (A1, A2, A3);
    (A1, A2, A3, A4);
}

/// A map that is written as a sequence of key and value pairs.
///
/// When serializing, the entries are written as a sequence of pairs
/// (`[["a", 1], ["b", 2]]`).  This is useful for maps whose keys cannot be
/// keys in the format, like tuples or structs in JSON.  When deserializing,
/// a sequence of pairs and a map are accepted.  The keys are deserialized
/// with the adapter `KA` and the values with `VA` (both [`Same`] by
/// default).  Keys that are given more than once are handled like in maps
/// (see [`DuplicateKeys`]).
///
/// Supported are `BTreeMap<K, V>` and `HashMap<K, V>`.
///
/// ```
/// use std::collections::BTreeMap;
/// use deser::adapters::MapAsPairs;
/// use deser::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize)]
/// pub struct Grid {
///     #[deser(as = MapAsPairs)]
///     cells: BTreeMap<(u32, u32), String>,
/// }
///
/// let grid: Grid = deser_json::from_str(r#"{"cells": [[[0, 1], "x"]]}"#).unwrap();
/// assert_eq!(grid.cells[&(0, 1)], "x");
/// assert_eq!(
///     deser_json::to_string(&grid).unwrap(),
///     r#"{"cells":[[[0,1],"x"]]}"#
/// );
/// ```
///
#[cfg_attr(
    not(feature = "std"),
    doc = "[`HashMap`]: https://doc.rust-lang.org/std/collections/struct.HashMap.html"
)]
pub struct MapAsPairs<KA = Same, VA = Same>(PhantomData<fn() -> (KA, VA)>);

/// A sequence of key and value pairs that is written as a map.
///
/// When serializing, the pairs are written as the entries of a map in
/// their order.  When deserializing, a map and a sequence of pairs are
/// accepted.  All entries are kept in the order of the data, also if a key
/// is given more than once.  The keys are deserialized with the adapter
/// `KA` and the values with `VA` (both [`Same`] by default).
///
/// This keeps the order of the entries and keys that are given more than
/// once.  Flattened into a struct, it receives the entries the struct does
/// not take in order.
///
/// Supported is `Vec<(K, V)>`.
///
/// ```
/// use deser::adapters::PairsAsMap;
/// use deser::{Deserialize, Serialize};
///
/// #[derive(Serialize, Deserialize)]
/// pub struct Headers {
///     #[deser(as = PairsAsMap)]
///     headers: Vec<(String, String)>,
/// }
///
/// let input = r#"{"headers":{"b":"1","a":"2"}}"#;
/// let headers: Headers = deser_json::from_str(input).unwrap();
/// assert_eq!(headers.headers[0], ("b".into(), "1".into()));
/// assert_eq!(deser_json::to_string(&headers).unwrap(), input);
/// ```
pub struct PairsAsMap<KA = Same, VA = Same>(PhantomData<fn() -> (KA, VA)>);

/// The sink of [`MapAsPairs`].
enum MapAsPairsSink<'a, 'de, M, K, V> {
    Pending(&'a mut Option<M>),
    // the value is a map
    Map(SinkHandle<'a, 'de>),
    // the value is a sequence of pairs
    Pairs(PairsSink<'a, M, K, V>),
}

/// Collects the pairs of a [`MapAsPairs`] into the map.
struct PairsSink<'a, M, K, V> {
    out: &'a mut Option<M>,
    map: M,
    pair: Option<(K, V)>,
    duplicate_keys: DuplicateKeys,
    errors: CollectedErrors,
}

impl<'a, M: MapTarget<K, V>, K, V> PairsSink<'a, M, K, V> {
    /// Adds the previous pair before the next one starts.
    ///
    /// If it's a duplicate that is rejected, the error is collected here as
    /// it's not the error of the next pair.
    fn flush(&mut self, state: &mut State) -> Result<(), Error> {
        if let Some((key, value)) = self.pair.take() {
            let replace = self.duplicate_keys != DuplicateKeys::First;
            if self.map.insert_entry(key, value, replace)
                && let Err(err) = self
                    .duplicate_keys
                    .resolve(|| "duplicate key in map".into())
            {
                return self.errors.collect(err, state);
            }
        }
        Ok(())
    }
}

impl<'a, 'de, M, K, V> MapAsPairsSink<'a, 'de, M, K, V>
where
    M: MapTarget<K, V>,
    K: Send + 'a,
    V: Send + 'a,
{
    /// Returns the sink of the map, creates it if the value is a map.
    fn map_sink<KA, VA>(&mut self, state: &mut State) -> Result<&mut SinkHandle<'a, 'de>, Error>
    where
        KA: Deserialize<'de, K>,
        VA: Deserialize<'de, V>,
    {
        if let MapAsPairsSink::Pending(..) = self
            && let MapAsPairsSink::Pending(out) =
                core::mem::replace(self, MapAsPairsSink::Map(SinkHandle::null()))
        {
            *self = MapAsPairsSink::Map(map_sink::<M, K, V, KA, VA>(MapOut::Slot(out), state));
        }
        match self {
            MapAsPairsSink::Map(sink) => Ok(sink),
            _ => Err(Error::new(
                ErrorKind::InvalidState,
                "the value is not a map",
            )),
        }
    }
}

impl<'a, 'de, M, K, V, KA, VA> Sink<'de> for Adapted<(KA, VA), MapAsPairsSink<'a, 'de, M, K, V>>
where
    M: MapTarget<K, V>,
    K: Send + 'a,
    V: Send + 'a,
    KA: Deserialize<'de, K>,
    VA: Deserialize<'de, V>,
{
    fn expecting(&self) -> Cow<'_, str> {
        Cow::Borrowed(M::NAME)
    }

    fn map(&mut self, state: &mut State) -> Result<(), Error> {
        self.map_sink::<KA, VA>(state)?.map(state)
    }

    fn seq(&mut self, state: &mut State) -> Result<(), Error> {
        if let MapAsPairsSink::Pending(..) = **self
            && let MapAsPairsSink::Pending(out) =
                core::mem::replace(&mut **self, MapAsPairsSink::Map(SinkHandle::null()))
        {
            let mut map = M::default();
            map.reserve_entries(state.container_shape().cautious_capacity::<(K, V)>());
            **self = MapAsPairsSink::Pairs(PairsSink {
                out,
                map,
                pair: None,
                duplicate_keys: DuplicateKeys::of(state),
                errors: CollectedErrors::new(),
            });
            return Ok(());
        }
        match **self {
            MapAsPairsSink::Map(ref mut sink) => sink.seq(state),
            _ => default_container_error(self, state),
        }
    }

    fn next_key(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        match **self {
            MapAsPairsSink::Map(ref mut sink) => sink.next_key(state),
            _ => Ok(SinkHandle::null()),
        }
    }

    fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        match **self {
            MapAsPairsSink::Map(ref mut sink) => sink.next_value(state),
            MapAsPairsSink::Pairs(ref mut pairs) => {
                pairs.flush(state)?;
                Ok(<(KA, VA) as Deserialize<'de, (K, V)>>::deserialize_into(
                    &mut pairs.pair,
                    state,
                ))
            }
            MapAsPairsSink::Pending(..) => Ok(SinkHandle::null()),
        }
    }

    fn __private_key_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        match **self {
            MapAsPairsSink::Map(ref mut sink) => sink.__private_key_atom(atom, state),
            _ => crate::de::atom_into_handle(self.next_key(state)?, atom, state),
        }
    }

    fn __private_value_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        match **self {
            MapAsPairsSink::Map(ref mut sink) => sink.__private_value_atom(atom, state),
            _ => crate::de::atom_into_handle(self.next_value(state)?, atom, state),
        }
    }

    fn __private_borrowed_key_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        match **self {
            MapAsPairsSink::Map(ref mut sink) => sink.__private_borrowed_key_atom(atom, state),
            _ => crate::de::borrowed_atom_into_handle(self.next_key(state)?, atom, state),
        }
    }

    fn __private_borrowed_value_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        match **self {
            MapAsPairsSink::Map(ref mut sink) => sink.__private_borrowed_value_atom(atom, state),
            _ => crate::de::borrowed_atom_into_handle(self.next_value(state)?, atom, state),
        }
    }

    /// Takes all keys when the map is flattened into a struct, like a map.
    fn value_for_key(
        &mut self,
        key: &str,
        state: &mut State,
    ) -> Result<Option<SinkHandle<'_, 'de>>, Error> {
        if let MapAsPairsSink::Pairs(..) = **self {
            return Ok(None);
        }
        self.map_sink::<KA, VA>(state)?.value_for_key(key, state)
    }

    fn recover(&mut self, err: Error, state: &mut State) -> Result<(), Error> {
        match **self {
            MapAsPairsSink::Map(ref mut sink) => sink.recover(err, state),
            MapAsPairsSink::Pairs(ref mut pairs) => {
                pairs.pair = None;
                pairs.errors.collect(err, state)
            }
            MapAsPairsSink::Pending(..) => Err(err),
        }
    }

    fn finish(&mut self, state: &mut State) -> Result<(), Error> {
        match **self {
            MapAsPairsSink::Map(ref mut sink) => sink.finish(state),
            MapAsPairsSink::Pairs(ref mut pairs) => {
                pairs.flush(state)?;
                pairs.errors.finish()?;
                *pairs.out = Some(take(&mut pairs.map));
                Ok(())
            }
            // a flattened map that received no keys
            MapAsPairsSink::Pending(..) => self.map_sink::<KA, VA>(state)?.finish(state),
        }
    }
}

/// Returns the error for a sequence that is given to a sink that is not
/// pending anymore.
#[cold]
fn default_container_error(sink: &mut dyn Sink<'_>, state: &State) -> Result<(), Error> {
    crate::de::atoms::default_container(sink, "sequence", state)
}

/// Implements `MapAsPairs` for maps.
macro_rules! map_as_pairs_impls {
    ($([$($bound:tt)*] [$($ser_bound:tt)*] $target:ty, $order:ident;)*) => {
        $(
            impl<'de, $($bound)*, KA: Deserialize<'de, K>, VA: Deserialize<'de, V>>
                Deserialize<'de, $target> for MapAsPairs<KA, VA>
            {
                fn deserialize_into<'out>(
                    out: &'out mut Option<$target>,
                    state: &mut State,
                ) -> SinkHandle<'out, 'de> {
                    adapted::arena_sink::<(KA, VA), _>(
                        MapAsPairsSink::<_, K, V>::Pending(out),
                        state,
                    )
                }

                fn expecting() -> Cow<'static, str> {
                    Cow::Borrowed(<$target as MapTarget<K, V>>::NAME)
                }
            }

            impl<$($ser_bound)*, KA: Serialize<K>, VA: Serialize<V>> Serialize<$target>
                for MapAsPairs<KA, VA>
            {
                fn serialize<'a>(
                    value: &'a $target,
                    state: &mut State,
                ) -> Result<Emit<'a>, Error> {
                    Ok(adapted::seq_emitter::<(KA, VA), _>(
                        PairsEmitter {
                            iter: value.iter(),
                            current: None,
                        },
                        state,
                    ))
                }

                fn container_shape(value: &$target) -> ContainerShape {
                    let mut shape = ContainerShape::with_order(Order::$order);
                    shape.set_len(value.len());
                    shape
                }
            }
        )*
    };
}

map_as_pairs_impls! {
    [K: Ord + Send, V: Send] [K: Sync, V: Sync] BTreeMap<K, V>, Sorted;
}

#[cfg(feature = "std")]
map_as_pairs_impls! {
    [K: Hash + Eq + Send, V: Send, H: BuildHasher + Default + Send]
    [K: Sync, V: Sync, H: BuildHasher + Sync]
    HashMap<K, V, H>, Arbitrary;
}

/// Emits the entries of a map as pairs.
struct PairsEmitter<'a, I, K, V> {
    iter: I,
    // the entry that is emitted, the handle of the pair borrows it
    current: Option<(&'a K, &'a V)>,
}

impl<'a, I, K, V, KA, VA> SeqEmitter for Adapted<(KA, VA), PairsEmitter<'a, I, K, V>>
where
    I: Iterator<Item = (&'a K, &'a V)> + Send,
    K: Sync + 'a,
    V: Sync + 'a,
    KA: Serialize<K>,
    VA: Serialize<V>,
{
    fn next(&mut self, _state: &mut State) -> Result<Option<SerializeHandle<'_>>, Error> {
        let this = &mut **self;
        this.current = this.iter.next();
        Ok(this
            .current
            .as_ref()
            .map(handle_as::<Pair<KA, VA>, (&'a K, &'a V)>))
    }
}

/// Serializes a key and a value as a pair.
struct Pair<KA, VA>(PhantomData<fn() -> (KA, VA)>);

impl<'r, K, V, KA, VA> Serialize<(&'r K, &'r V)> for Pair<KA, VA>
where
    K: Sync,
    V: Sync,
    KA: Serialize<K>,
    VA: Serialize<V>,
{
    fn serialize<'a>(value: &'a (&'r K, &'r V), state: &mut State) -> Result<Emit<'a>, Error> {
        let seq = Adapted::<Self, _>::indexed(value);
        Ok(Emit::seq(IndexedSeqEmitter::new(seq), state))
    }

    fn container_shape(_value: &(&'r K, &'r V)) -> ContainerShape {
        ContainerShape::with_len(2)
    }
}

impl<'r, K, V, KA, VA> IndexedSeq for Adapted<Pair<KA, VA>, (&'r K, &'r V)>
where
    K: Sync,
    V: Sync,
    KA: Serialize<K>,
    VA: Serialize<V>,
{
    fn element(
        &self,
        index: usize,
        _state: &mut State,
    ) -> Result<Option<SerializeHandle<'_>>, Error> {
        let (key, value) = *self.get();
        Ok(match index {
            0 => Some(handle_as::<KA, K>(key)),
            1 => Some(handle_as::<VA, V>(value)),
            _ => None,
        })
    }
}

/// The sink of [`PairsAsMap`].
enum PairsAsMapSink<'a, 'de, K, V> {
    Pending(&'a mut Option<Vec<(K, V)>>),
    // the value is a sequence of pairs
    Seq(SinkHandle<'a, 'de>),
    // the value is a map
    Map(EntriesSink<'a, K, V>),
}

/// Collects the entries of a map for a [`PairsAsMap`].
struct EntriesSink<'a, K, V> {
    out: &'a mut Option<Vec<(K, V)>>,
    entries: Vec<(K, V)>,
    key: Option<K>,
    value: Option<V>,
    errors: CollectedErrors,
}

impl<'a, K, V> EntriesSink<'a, K, V> {
    /// Adds the previous entry before the next one starts.
    fn flush(&mut self) {
        if let (Some(key), Some(value)) = (self.key.take(), self.value.take()) {
            self.entries.push((key, value));
        }
    }
}

impl<'a, 'de, K: Send, V: Send> PairsAsMapSink<'a, 'de, K, V> {
    /// Returns the sink of the sequence, creates it if the value is a
    /// sequence.
    fn seq_sink<KA, VA>(&mut self, state: &mut State) -> Option<&mut SinkHandle<'a, 'de>>
    where
        KA: Deserialize<'de, K>,
        VA: Deserialize<'de, V>,
    {
        if let PairsAsMapSink::Pending(..) = self
            && let PairsAsMapSink::Pending(out) =
                core::mem::replace(self, PairsAsMapSink::Seq(SinkHandle::null()))
        {
            *self = PairsAsMapSink::Seq(
                <Vec<(KA, VA)> as Deserialize<'de, Vec<(K, V)>>>::deserialize_into(out, state),
            );
        }
        match self {
            PairsAsMapSink::Seq(sink) => Some(sink),
            _ => None,
        }
    }

    /// Returns the sink of the entries, creates it if the value is a map.
    fn entries(&mut self) -> Option<&mut EntriesSink<'a, K, V>> {
        if let PairsAsMapSink::Pending(..) = self
            && let PairsAsMapSink::Pending(out) =
                core::mem::replace(self, PairsAsMapSink::Seq(SinkHandle::null()))
        {
            *self = PairsAsMapSink::Map(EntriesSink {
                out,
                entries: Vec::new(),
                key: None,
                value: None,
                errors: CollectedErrors::new(),
            });
        }
        match self {
            PairsAsMapSink::Map(entries) => Some(entries),
            _ => None,
        }
    }
}

impl<'a, 'de, K, V, KA, VA> Sink<'de> for Adapted<(KA, VA), PairsAsMapSink<'a, 'de, K, V>>
where
    K: Send,
    V: Send,
    KA: Deserialize<'de, K>,
    VA: Deserialize<'de, V>,
{
    fn expecting(&self) -> Cow<'_, str> {
        Cow::Borrowed("map")
    }

    fn map(&mut self, state: &mut State) -> Result<(), Error> {
        let capacity = state.container_shape().cautious_capacity::<(K, V)>();
        if let Some(entries) = self.entries() {
            entries.entries.reserve(capacity);
            return Ok(());
        }
        crate::de::atoms::default_container(self, "map", state)
    }

    fn seq(&mut self, state: &mut State) -> Result<(), Error> {
        match self.seq_sink::<KA, VA>(state) {
            Some(sink) => sink.seq(state),
            None => default_container_error(self, state),
        }
    }

    fn __private_seq(&mut self, state: &mut State) -> Result<bool, Error> {
        // pairs of atoms are built inline by the sink of the sequence
        match self.seq_sink::<KA, VA>(state) {
            Some(sink) => sink.__private_seq(state),
            None => default_container_error(self, state).map(|()| false),
        }
    }

    fn __private_inline_atom(
        &mut self,
        index: usize,
        atom: Atom,
        state: &mut State,
    ) -> Result<(), Error> {
        match **self {
            PairsAsMapSink::Seq(ref mut sink) => sink.__private_inline_atom(index, atom, state),
            _ => unreachable!(),
        }
    }

    fn __private_inline_event(
        &mut self,
        event: crate::de::InlineEvent,
        state: &mut State,
    ) -> Result<(), Error> {
        match **self {
            PairsAsMapSink::Seq(ref mut sink) => sink.__private_inline_event(event, state),
            _ => unreachable!(),
        }
    }

    fn next_key(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        match **self {
            PairsAsMapSink::Map(ref mut entries) => {
                entries.flush();
                Ok(KA::deserialize_into(&mut entries.key, state))
            }
            PairsAsMapSink::Seq(ref mut sink) => sink.next_key(state),
            PairsAsMapSink::Pending(..) => Ok(SinkHandle::null()),
        }
    }

    fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        match **self {
            PairsAsMapSink::Map(ref mut entries) => {
                Ok(VA::deserialize_into(&mut entries.value, state))
            }
            PairsAsMapSink::Seq(ref mut sink) => sink.next_value(state),
            PairsAsMapSink::Pending(..) => Ok(SinkHandle::null()),
        }
    }

    fn __private_key_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        match **self {
            PairsAsMapSink::Map(ref mut entries) => {
                entries.flush();
                KA::__private_atom_into(&mut entries.key, atom, state)
            }
            _ => crate::de::atom_into_handle(self.next_key(state)?, atom, state),
        }
    }

    fn __private_value_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        match **self {
            PairsAsMapSink::Map(ref mut entries) => {
                VA::__private_atom_into(&mut entries.value, atom, state)
            }
            PairsAsMapSink::Seq(ref mut sink) => sink.__private_value_atom(atom, state),
            PairsAsMapSink::Pending(..) => Ok(()),
        }
    }

    fn __private_borrowed_key_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        match **self {
            PairsAsMapSink::Map(ref mut entries) => {
                entries.flush();
                KA::__private_borrowed_atom_into(&mut entries.key, atom, state)
            }
            _ => crate::de::borrowed_atom_into_handle(self.next_key(state)?, atom, state),
        }
    }

    fn __private_borrowed_value_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        match **self {
            PairsAsMapSink::Map(ref mut entries) => {
                VA::__private_borrowed_atom_into(&mut entries.value, atom, state)
            }
            PairsAsMapSink::Seq(ref mut sink) => sink.__private_borrowed_value_atom(atom, state),
            PairsAsMapSink::Pending(..) => Ok(()),
        }
    }

    /// Takes all keys when flattened into a struct, the key is parsed like
    /// the keys of formats that only have string keys.
    fn value_for_key(
        &mut self,
        key: &str,
        state: &mut State,
    ) -> Result<Option<SinkHandle<'_, 'de>>, Error> {
        let Some(entries) = self.entries() else {
            return Ok(None);
        };
        entries.flush();
        KA::__private_atom_into(&mut entries.key, Atom::Lexical(Text::borrowed(key)), state)?;
        Ok(Some(VA::deserialize_into(&mut entries.value, state)))
    }

    fn recover(&mut self, err: Error, state: &mut State) -> Result<(), Error> {
        match **self {
            PairsAsMapSink::Map(ref mut entries) => {
                entries.key = None;
                entries.value = None;
                entries.errors.collect(err, state)
            }
            PairsAsMapSink::Seq(ref mut sink) => sink.recover(err, state),
            PairsAsMapSink::Pending(..) => Err(err),
        }
    }

    fn finish(&mut self, state: &mut State) -> Result<(), Error> {
        // a flattened value that received no keys has no entries
        let _ = self.entries();
        match **self {
            PairsAsMapSink::Map(ref mut entries) => {
                entries.flush();
                entries.errors.finish()?;
                *entries.out = Some(take(&mut entries.entries));
                Ok(())
            }
            PairsAsMapSink::Seq(ref mut sink) => sink.finish(state),
            PairsAsMapSink::Pending(..) => Ok(()),
        }
    }
}

impl<'de, K, V, KA, VA> Deserialize<'de, Vec<(K, V)>> for PairsAsMap<KA, VA>
where
    K: Send,
    V: Send,
    KA: Deserialize<'de, K>,
    VA: Deserialize<'de, V>,
{
    fn deserialize_into<'out>(
        out: &'out mut Option<Vec<(K, V)>>,
        state: &mut State,
    ) -> SinkHandle<'out, 'de> {
        adapted::arena_sink::<(KA, VA), _>(PairsAsMapSink::Pending(out), state)
    }

    fn expecting() -> Cow<'static, str> {
        Cow::Borrowed("map")
    }
}

impl<K, V, KA, VA> Serialize<Vec<(K, V)>> for PairsAsMap<KA, VA>
where
    K: Sync,
    V: Sync,
    KA: Serialize<K>,
    VA: Serialize<V>,
{
    fn serialize<'a>(value: &'a Vec<(K, V)>, state: &mut State) -> Result<Emit<'a>, Error> {
        Ok(MapIterEmitter::emit::<K, KA, VA>(
            value.iter().map(split_pair as fn(&(K, V)) -> (&K, &V)),
            state,
        ))
    }

    fn container_shape(value: &Vec<(K, V)>) -> ContainerShape {
        let mut shape = ContainerShape::with_order(Order::Significant);
        shape.set_len(value.len());
        shape
    }
}

/// Splits a reference to a pair into references to its parts.
fn split_pair<K, V>(pair: &(K, V)) -> (&K, &V) {
    (&pair.0, &pair.1)
}