deser-core 0.10.1

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
//! Support for the sinks of derived structs.
//!
//! The logic that does not depend on the types of the fields lives here so
//! that it exists once instead of once per derived struct.  This is not part
//! of the public API, it's used by the derive through `deser::__derive`.
use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::format;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use core::marker::PhantomData;
use core::ptr::NonNull;

use crate::State;
use crate::Text;
use crate::adapters::Same;
use crate::de::CollectedErrors;
use crate::de::atoms::{atom_into_handle, borrowed_atom_into_handle};
use crate::de::duplicates::{duplicate_field, is_seen, mark_seen};
use crate::de::sinkbox::ArenaStruct;
use crate::de::unknown::{unknown_field, wants_unknown_fields};
use crate::de::{Deserialize, Sink, SinkHandle, default_atom};
use crate::error::{Error, ErrorKind, discarded_error};
use crate::event::Atom;

/// The index of a key that is not a field.
const UNKNOWN: usize = usize::MAX;

/// A function that returns the index of the field for a key.
pub(crate) type FieldLookup = fn(&str) -> Option<usize>;

/// A function that returns `true` if the field with the index collects the
/// values of a repeated key (see
/// [`Deserialize::__private_collects`]).
pub(crate) type FieldCollects = fn(usize) -> bool;

/// How the value of a field is deserialized.
///
/// In a multimap (see
/// [`ContainerShape::set_multimap`](crate::ContainerShape::set_multimap))
/// fields that are collections collect the values of all occurrences of
/// their key.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Collect {
    /// The value is the value of the field.
    No,
    /// The value is the first one that the field collects.
    First,
    /// The value is added to the values the field collected.
    Next,
}

/// The sink for the keys of a derived struct.
///
/// Keys are resolved to the index of their field with a function generated
/// by the derive.  The names of keys that are not fields are only retained
/// if they are needed: always if the sink was created with `retain` (for
/// structs with flattened fields which look them up on the flattened fields
/// and structs that reject unknown keys), otherwise only if the
/// [`UnknownFields`](crate::de::UnknownFields) policy wants them.
pub struct FieldKeySink {
    index: usize,
    other: Option<String>,
    // the position of the key in the input if it's retained in `other`
    offset: Option<usize>,
    lookup: FieldLookup,
    collects: FieldCollects,
    // `true` if the field is a collection whose key was given before
    repeated: bool,
    retain: bool,
}

/// What a derived struct with flattened fields does with the next value.
pub enum NextField {
    /// The value is ignored (a duplicate that is ignored).
    Ignore,
    /// The value is for the field with the index.
    Field(usize),
    /// The value is for a key that is not a field.
    Other(String),
}

impl FieldKeySink {
    /// Creates the key sink.
    #[inline]
    pub fn new(lookup: FieldLookup, collects: FieldCollects, retain: bool) -> FieldKeySink {
        FieldKeySink {
            index: UNKNOWN,
            other: None,
            offset: None,
            lookup,
            collects,
            repeated: false,
            retain,
        }
    }

    /// Returns how the value of the field with the index is deserialized.
    ///
    /// This is only needed for updates: collections whose key is given
    /// the first time are replaced, later values are added to them.  It
    /// must be called once per value.
    #[inline]
    pub fn collect(&mut self, index: usize, state: &State) -> Collect {
        match (state.is_multimap() && (self.collects)(index), self.repeated) {
            (false, _) => Collect::No,
            (true, false) => Collect::First,
            (true, true) => {
                self.repeated = false;
                Collect::Next
            }
        }
    }

    /// Decides if the value of a field that was given before is used.
    ///
    /// Collections in multimaps take all values, for other fields the
    /// duplicate key policy decides.
    #[cold]
    #[inline(never)]
    fn duplicate(&mut self, index: usize, fields: &[&str], state: &State) -> Result<bool, Error> {
        if state.is_multimap() && (self.collects)(index) {
            self.repeated = true;
            return Ok(true);
        }
        duplicate_field(fields[index], state)
    }

    /// Resets the key before the next key is deserialized.
    #[inline]
    pub fn reset(&mut self) {
        self.index = UNKNOWN;
        self.other = None;
    }

    /// Sets the field of the next value.
    #[inline]
    pub fn set_index(&mut self, index: usize) {
        self.reset();
        self.index = index;
    }

    /// Returns the position of the last key that is not a field.
    #[inline]
    pub fn offset(&self) -> Option<usize> {
        self.offset
    }

    /// Deserializes a key from an atom.
    ///
    /// `lookup` must be the function the sink was created with, it's passed
    /// again so that it can be called directly (this is always inlined).
    #[inline(always)]
    pub fn key_atom(
        &mut self,
        atom: Atom,
        lookup: FieldLookup,
        state: &mut State,
    ) -> Result<(), Error> {
        match atom {
            // the string is moved out so that only it needs to be dropped
            Atom::Str(key) | Atom::Lexical(key) => {
                match lookup(&key) {
                    Some(index) => self.index = index,
                    None => self.other_key(key, state),
                }
                Ok(())
            }
            other => self.key_atom_slow(other, state),
        }
    }

    #[inline(never)]
    fn key_atom_slow(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.reset();
        self.atom(atom, state)
    }

    /// Records a key that is not a field.
    #[inline(never)]
    fn other_key(&mut self, key: Text<'_>, state: &State) {
        self.index = UNKNOWN;
        self.other = if self.retain || wants_unknown_fields(state) {
            self.offset = state.input_range().map(|range| range.start);
            Some(key.into_owned())
        } else {
            None
        };
    }

    /// Takes the field index of the next value of a struct without
    /// flattened fields.
    ///
    /// Fields that were already seen are resolved with the duplicate key
    /// policy, keys that are not fields with the unknown fields policy (or
    /// rejected if `deny` is set).  Returns `None` if the value is ignored.
    #[inline]
    pub fn next_index(
        &mut self,
        seen: &mut [u64],
        fields: &[&str],
        deny: bool,
        state: &mut State,
    ) -> Result<Option<usize>, Error> {
        let index = core::mem::replace(&mut self.index, UNKNOWN);
        if index != UNKNOWN {
            Ok(
                if !mark_seen(seen, index) || self.duplicate(index, fields, state)? {
                    Some(index)
                } else {
                    None
                },
            )
        } else {
            if self.other.is_some() {
                self.unknown_key(fields, deny, state)?;
            }
            Ok(None)
        }
    }

    #[inline(never)]
    fn unknown_key(&mut self, fields: &[&str], deny: bool, state: &mut State) -> Result<(), Error> {
        match self.other.take() {
            Some(key) => unknown_field(&key, self.offset, fields, deny, state),
            None => Ok(()),
        }
    }

    /// Takes the key of the next value of a struct with flattened fields.
    ///
    /// Fields that were already seen are resolved with the duplicate key
    /// policy.  Keys that are not fields are returned, they are unknown if
    /// no flattened field takes them.
    #[inline(never)]
    pub fn next_field(
        &mut self,
        seen: &mut [u64],
        fields: &[&str],
        state: &State,
    ) -> Result<NextField, Error> {
        let index = core::mem::replace(&mut self.index, UNKNOWN);
        Ok(if index != UNKNOWN {
            if !mark_seen(seen, index) || self.duplicate(index, fields, state)? {
                NextField::Field(index)
            } else {
                NextField::Ignore
            }
        } else {
            match self.other.take() {
                Some(key) => NextField::Other(key),
                None => NextField::Ignore,
            }
        })
    }
}

impl<'de> Sink<'de> for FieldKeySink {
    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        match atom {
            Atom::Str(key) | Atom::Lexical(key) => {
                match (self.lookup)(&key) {
                    Some(index) => self.index = index,
                    None => self.other_key(key, state),
                }
                Ok(())
            }
            other => default_atom(self, other, state),
        }
    }

    fn expecting(&self) -> Cow<'_, str> {
        Cow::Borrowed("string")
    }
}

/// What a derived struct (without flattened fields) is, independent of the
/// types of its fields.
pub struct StructInfo {
    /// The name of the struct for errors.
    pub name: &'static str,
    /// The names of the fields by index.
    pub fields: &'static [&'static str],
    /// Returns the index of the field for a key.
    pub lookup: FieldLookup,
    /// Rejects keys that are not fields.
    pub deny: bool,
    /// The fields can borrow from the data (the struct has generics), see
    /// [`FieldSlot::borrowed_atom`].
    pub borrows: bool,
}

/// The fields of a derived struct without flattened fields.
///
/// The derive implements this for a struct that holds the slot of the
/// struct and a [`FieldValue`] for every field.  [`StructSink`] does
/// everything that does not depend on the types of the fields, it exists
/// once for all structs, and what depends on the type of a field is done
/// by the [`FieldSlot`] of the field which exists once per type of field.
/// The derive only returns the slot of a field by its index and builds the
/// struct.
pub trait StructFields<'de>: Send {
    /// Returns the slot of the field with the index.
    fn field(&mut self, index: usize) -> &mut dyn FieldSlot<'de>;

    /// Returns the fields that want raw values as bits by index (see
    /// [`Deserialize::__private_raw`]).
    ///
    /// The last bit is set if a field from the 64th on wants a raw value,
    /// these are asked for it (see [`FieldSlot::raw`]).
    #[inline(always)]
    fn raw_fields() -> u64
    where
        Self: Sized,
    {
        0
    }

    /// Returns the fields that collect the values of a repeated key as
    /// bits by index (see [`Deserialize::__private_collects`]).
    ///
    /// The last bit is set if a field from the 64th on collects.  Only the
    /// fields whose bit is set are asked (see [`FieldSlot::collects`]), by
    /// default all of them.
    #[inline(always)]
    fn collect_fields() -> u64
    where
        Self: Sized,
    {
        u64::MAX
    }

    /// Builds the struct from the fields and places it in the slot.
    ///
    /// If [`StructFinish::ok`] returns `false` or required fields are
    /// missing, this fails with [`StructFinish::missing`].  The fields are
    /// not dropped after it returns (also if it fails), so all values have
    /// to be taken out.  If it panics, the fields are dropped.
    fn finish(&mut self, finish: &mut StructFinish<'_>, state: &mut State) -> Result<(), Error>;
}

/// The value of a field of a derived struct while it's deserialized.
///
/// This is implemented by [`FieldValue`] for all types of fields (and
/// adapters), so the code exists once per type of field instead of once per
/// struct (see [`StructFields`]).
pub trait FieldSlot<'de>: Send {
    /// Returns `true` if the field collects the values of a repeated key
    /// (see [`Deserialize::__private_collects`]).
    fn collects(&self) -> bool;

    /// Returns the format of the raw value the field wants (see
    /// [`Deserialize::__private_raw`]).
    fn raw(&self) -> Option<&'static crate::ext::RawFormatInfo>;

    /// Returns the sink of the field, optionally collecting its value.
    fn sink(&mut self, collect: Collect, state: &mut State) -> SinkHandle<'_, 'de>;

    /// Deserializes an atom into the field.
    fn atom(&mut self, collect: Collect, atom: Atom, state: &mut State) -> Result<(), Error>;

    /// Deserializes a borrowed atom into the field.
    ///
    /// This is only invoked if [`StructInfo::borrows`] is set, otherwise
    /// borrowed atoms are passed to [`atom`](Self::atom).
    fn borrowed_atom(
        &mut self,
        collect: Collect,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error>;
}

/// The value of a field of type `T` that is deserialized with the adapter
/// `A` (see [`FieldSlot`]).
///
/// This is unrelated to the public [`Slot`](crate::de::Slot), the slot of a
/// value that is deserialized from an atom.
pub struct FieldValue<T, A = Same> {
    value: Option<T>,
    _adapter: PhantomData<fn() -> A>,
}

impl<T, A> FieldValue<T, A> {
    /// Creates the slot with an initial value.
    #[inline(always)]
    pub fn new(value: Option<T>) -> FieldValue<T, A> {
        FieldValue {
            value,
            _adapter: PhantomData,
        }
    }

    /// Returns `true` if the field has no value.
    #[inline(always)]
    pub fn is_none(&self) -> bool {
        self.value.is_none()
    }

    /// Sets the value of the field.
    #[inline(always)]
    pub fn set(&mut self, value: Option<T>) {
        self.value = value;
    }

    /// Takes the value of the field.
    #[inline(always)]
    pub fn take(&mut self) -> Option<T> {
        self.value.take()
    }
}

/// The slot of a struct without fields, it's never used.
struct NoField;

impl<'de> FieldSlot<'de> for NoField {
    fn collects(&self) -> bool {
        false
    }

    fn raw(&self) -> Option<&'static crate::ext::RawFormatInfo> {
        None
    }

    fn sink(&mut self, _collect: Collect, _state: &mut State) -> SinkHandle<'_, 'de> {
        SinkHandle::null()
    }

    fn atom(&mut self, _collect: Collect, _atom: Atom, _state: &mut State) -> Result<(), Error> {
        Ok(())
    }

    fn borrowed_atom(
        &mut self,
        _collect: Collect,
        _atom: Atom<'de>,
        _state: &mut State,
    ) -> Result<(), Error> {
        Ok(())
    }
}

/// Returns the slot of a field of a struct without fields.
///
/// The fields of a struct are only looked up by the index of a field, so
/// this is never used.
pub fn no_field_slot<'x, 'de>() -> &'x mut dyn FieldSlot<'de> {
    // boxes of zero sized types do not allocate
    Box::leak(Box::new(NoField))
}

impl<'de, T: Send, A: Deserialize<'de, T>> FieldSlot<'de> for FieldValue<T, A> {
    fn collects(&self) -> bool {
        A::__private_collects()
    }

    fn raw(&self) -> Option<&'static crate::ext::RawFormatInfo> {
        A::__private_raw()
    }

    fn sink(&mut self, collect: Collect, state: &mut State) -> SinkHandle<'_, 'de> {
        // `collect` is only set for fields that collect, the check of the
        // type removes the branch for the others
        if A::__private_collects() && collect != Collect::No {
            A::__private_collect_into(&mut self.value, state)
        } else {
            A::deserialize_into(&mut self.value, state)
        }
    }

    fn atom(&mut self, collect: Collect, atom: Atom, state: &mut State) -> Result<(), Error> {
        if A::__private_collects() && collect != Collect::No {
            atom_into_handle(
                A::__private_collect_into(&mut self.value, state),
                atom,
                state,
            )
        } else {
            A::__private_atom_into(&mut self.value, atom, state)
        }
    }

    fn borrowed_atom(
        &mut self,
        collect: Collect,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        if A::__private_collects() && collect != Collect::No {
            borrowed_atom_into_handle(
                A::__private_collect_into(&mut self.value, state),
                atom,
                state,
            )
        } else {
            A::__private_borrowed_atom_into(&mut self.value, atom, state)
        }
    }
}

/// Passed to [`StructFields::finish`].
pub struct StructFinish<'a> {
    seen: &'a Seen,
    errors: &'a mut CollectedErrors,
    fields: &'static [&'static str],
}

impl StructFinish<'_> {
    /// Returns `true` if no errors were collected, the struct is only built
    /// then.
    #[inline(always)]
    pub fn ok(&self) -> bool {
        self.errors.is_empty()
    }

    /// Returns the error if the struct is not built.
    ///
    /// `missing` tells for every field (by index) if it's required and has
    /// no value.  The error holds the errors that were collected and the
    /// fields that are missing.  Fields that were seen but have no value
    /// failed, they are only missing if no errors were collected.
    #[cold]
    #[inline(never)]
    pub fn missing(&mut self, missing: &[bool], state: &State) -> Error {
        if self.errors.is_empty() {
            return missing_field(missing, self.fields, state);
        }
        for (index, (missing, name)) in missing.iter().zip(self.fields).enumerate() {
            if *missing && !self.seen.contains(index) {
                self.errors
                    .push(new_missing_field_error(name, state), state);
            }
        }
        match self.errors.take() {
            Some(err) => err,
            None => unreachable!(),
        }
    }
}

/// The fields of a struct that were seen.
///
/// The first 64 fields are tracked inline, the others (of large structs)
/// in words that are allocated once they are needed.
struct Seen {
    small: u64,
    large: Option<Box<[u64]>>,
}

impl Seen {
    /// Returns `true` if the field with the index was seen.
    fn contains(&self, index: usize) -> bool {
        if index < 64 {
            self.small & (1 << index) != 0
        } else {
            match self.large {
                Some(ref large) => is_seen(large, index),
                None => false,
            }
        }
    }
}

/// The sink of a derived struct without flattened fields.
///
/// It exists once for all structs, the fields are behind
/// [`StructFields`].
pub struct StructSink<'a, 'de> {
    // the fields are in the same block as the sink (see `ArenaStruct`) which
    // drops them
    fields: NonNull<dyn StructFields<'de> + 'a>,
    key: FieldKeySink,
    seen: Seen,
    errors: CollectedErrors,
    info: &'static StructInfo,
    // the fields that want raw values as bits by index (see
    // `StructFields::raw_fields`)
    raw: u64,
    // the fields that collect as bits by index (see
    // `StructFields::collect_fields`)
    collects: u64,
    // `true` while the fields are empty after `finish`, they are not
    // dropped then (see `ArenaStruct`).  It's reset when a field gets a
    // value again (a root sink can receive another value after it
    // finished), otherwise that value would never be dropped.
    finished: bool,
}

// SAFETY: the sink owns the fields which are `Send`
unsafe impl Send for StructSink<'_, '_> {}

impl<'a, 'de> StructSink<'a, 'de> {
    /// Creates the sink of a struct.
    #[inline]
    pub fn handle<F: StructFields<'de> + 'a>(
        fields: F,
        info: &'static StructInfo,
        state: &mut State,
    ) -> SinkHandle<'a, 'de> {
        SinkHandle::from_arena_struct(ArenaStruct::new(
            fields,
            info,
            F::raw_fields(),
            F::collect_fields(),
            &mut state.arena,
        ))
    }

    /// Creates the sink for fields, see [`ArenaStruct`].
    #[inline(always)]
    pub(crate) fn new(
        fields: NonNull<dyn StructFields<'de> + 'a>,
        info: &'static StructInfo,
        raw: u64,
        collects: u64,
    ) -> StructSink<'a, 'de> {
        StructSink {
            fields,
            key: FieldKeySink::new(info.lookup, |_| false, info.deny),
            seen: Seen {
                small: 0,
                large: None,
            },
            errors: CollectedErrors::new(),
            info,
            raw,
            collects,
            finished: false,
        }
    }

    /// Returns the pointer to the fields and if they need to be dropped.
    #[inline(always)]
    pub(crate) fn fields_ptr(&self) -> (NonNull<dyn StructFields<'de> + 'a>, bool) {
        (self.fields, !self.finished)
    }

    #[inline(always)]
    fn fields(&mut self) -> &mut (dyn StructFields<'de> + 'a) {
        // SAFETY: the fields are valid while the sink exists and only
        // borrowed through it
        unsafe { self.fields.as_mut() }
    }

    /// Requests the value of the key as raw value if its field wants one.
    #[inline(never)]
    fn request_raw(&mut self, state: &mut State) -> Result<(), Error> {
        let index = self.key.index;
        // the last bit stands for all fields from the 64th on
        if self.raw & (1 << index.min(63)) != 0
            && index < self.info.fields.len()
            && let Some(format) = self.fields().field(index).raw()
        {
            return state.__private_request_raw(format);
        }
        Ok(())
    }

    /// Returns how this occurrence of a field is deserialized.
    fn collect(&mut self, index: usize, state: &State) -> Collect {
        // the last bit stands for all fields from the 64th on
        let collects = state.is_multimap()
            && self.collects & (1 << index.min(63)) != 0
            && self.fields().field(index).collects();
        match (collects, core::mem::replace(&mut self.key.repeated, false)) {
            (false, _) => Collect::No,
            (true, false) => Collect::First,
            (true, true) => Collect::Next,
        }
    }

    /// Takes the field index of the next value.
    ///
    /// Returns `None` if the value is ignored, see
    /// [`FieldKeySink::next_index`].
    #[inline(always)]
    fn next_index(&mut self, state: &mut State) -> Result<Option<usize>, Error> {
        // every value of a field passes here, the fields can hold values
        self.finished = false;
        let index = core::mem::replace(&mut self.key.index, UNKNOWN);
        if index < 64 && self.seen.small & (1 << index) == 0 {
            self.seen.small |= 1 << index;
            return Ok(Some(index));
        }
        self.next_index_slow(index, state)
    }

    /// Handles keys that are not fields, duplicate fields and the fields
    /// of large structs.
    #[inline(never)]
    fn next_index_slow(&mut self, index: usize, state: &mut State) -> Result<Option<usize>, Error> {
        if index == UNKNOWN {
            if self.key.other.is_some() {
                self.key
                    .unknown_key(self.info.fields, self.info.deny, state)?;
            }
            return Ok(None);
        }
        let seen_before = if index < 64 {
            true
        } else {
            let words = self.info.fields.len().div_ceil(64);
            let large = self
                .seen
                .large
                .get_or_insert_with(|| vec![0; words].into_boxed_slice());
            mark_seen(large, index)
        };
        if !seen_before || (state.is_multimap() && self.fields().field(index).collects()) {
            if seen_before {
                self.key.repeated = true;
            }
            Ok(Some(index))
        } else if duplicate_field(self.info.fields[index], state)? {
            Ok(Some(index))
        } else {
            Ok(None)
        }
    }
}

impl<'a, 'de> Sink<'de> for StructSink<'a, 'de> {
    fn expecting(&self) -> Cow<'_, str> {
        Cow::Borrowed(self.info.name)
    }

    fn map(&mut self, _state: &mut State) -> Result<(), Error> {
        Ok(())
    }

    fn next_key(&mut self, _state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        self.key.reset();
        Ok(SinkHandle::to(&mut self.key))
    }

    fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        Ok(match self.next_index(state)? {
            Some(index) => {
                let collect = self.collect(index, state);
                self.fields().field(index).sink(collect, state)
            }
            None => SinkHandle::null(),
        })
    }

    fn __private_key_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.key.key_atom(atom, self.key.lookup, state)?;
        if self.raw != 0 {
            return self.request_raw(state);
        }
        Ok(())
    }

    fn __private_value_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        match self.next_index(state)? {
            Some(index) => {
                let collect = self.collect(index, state);
                self.fields().field(index).atom(collect, atom, state)
            }
            None => Ok(()),
        }
    }

    fn __private_borrowed_key_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        // keys are only matched, they do not need to be borrowed
        self.key.key_atom(atom, self.key.lookup, state)?;
        if self.raw != 0 {
            return self.request_raw(state);
        }
        Ok(())
    }

    fn __private_borrowed_value_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        match self.next_index(state)? {
            Some(index) if self.info.borrows => {
                let collect = self.collect(index, state);
                self.fields()
                    .field(index)
                    .borrowed_atom(collect, atom, state)
            }
            Some(index) => {
                let collect = self.collect(index, state);
                self.fields().field(index).atom(collect, atom, state)
            }
            None => Ok(()),
        }
    }

    fn value_for_key(
        &mut self,
        key: &str,
        state: &mut State,
    ) -> Result<Option<SinkHandle<'_, 'de>>, Error> {
        // the value is deserialized like the value of a key (so that the
        // struct can be flattened)
        match (self.info.lookup)(key) {
            Some(index) => {
                self.key.set_index(index);
                self.next_value(state).map(Some)
            }
            None => Ok(None),
        }
    }

    fn recover(&mut self, err: Error, state: &mut State) -> Result<(), Error> {
        self.errors.collect(err, state)
    }

    fn finish(&mut self, state: &mut State) -> Result<(), Error> {
        let mut finish = StructFinish {
            seen: &self.seen,
            errors: &mut self.errors,
            fields: self.info.fields,
        };
        // SAFETY: see `fields`
        let rv = unsafe { self.fields.as_mut() }.finish(&mut finish, state);
        // the fields took their values (also if they failed).  This is set
        // after the call: if it panics, the values that are left in the
        // fields are dropped with them.
        self.finished = true;
        rv
    }
}

/// The fields of a derived struct that is updated (see
/// [`Deserialize::deserialize_update`]).
///
/// The derive implements this for structs, [`StructUpdateSink`] does
/// everything else, it exists once for all structs.
pub trait UpdateFields<'de>: Send {
    /// Returns the sink that updates the field with the index.
    ///
    /// `collect` says if the value is collected (see [`Collect`]).
    fn update_field(
        &mut self,
        index: usize,
        collect: Collect,
        state: &mut State,
    ) -> SinkHandle<'_, 'de>;

    /// Returns whether the field collects repeated values.
    fn collects(&self, index: usize) -> bool;
}

/// The sink that updates a derived struct.
///
/// The fields that are given are updated, the others are kept.
pub struct StructUpdateSink<'a, 'de> {
    value: &'a mut (dyn UpdateFields<'de> + 'a),
    key: FieldKeySink,
    seen: Vec<u64>,
    info: &'static StructInfo,
}

impl<'a, 'de> StructUpdateSink<'a, 'de> {
    /// Creates the sink that updates a struct.
    pub fn handle(
        value: &'a mut (dyn UpdateFields<'de> + 'a),
        info: &'static StructInfo,
        state: &mut State,
    ) -> SinkHandle<'a, 'de> {
        SinkHandle::arena(
            StructUpdateSink {
                value,
                key: FieldKeySink::new(info.lookup, |_| false, info.deny),
                seen: vec![0; info.fields.len().div_ceil(64)],
                info,
            },
            state,
        )
    }
}

impl<'a, 'de> Sink<'de> for StructUpdateSink<'a, 'de> {
    fn expecting(&self) -> Cow<'_, str> {
        Cow::Borrowed(self.info.name)
    }

    fn map(&mut self, _state: &mut State) -> Result<(), Error> {
        Ok(())
    }

    fn next_key(&mut self, _state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        self.key.reset();
        Ok(SinkHandle::to(&mut self.key))
    }

    fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        let index = core::mem::replace(&mut self.key.index, UNKNOWN);
        if index == UNKNOWN {
            if self.key.other.is_some() {
                self.key
                    .unknown_key(self.info.fields, self.info.deny, state)?;
            }
            return Ok(SinkHandle::null());
        }
        let repeated = mark_seen(&mut self.seen, index);
        let collects = state.is_multimap() && self.value.collects(index);
        if repeated && !collects && !duplicate_field(self.info.fields[index], state)? {
            return Ok(SinkHandle::null());
        }
        let collect = if !collects {
            Collect::No
        } else if repeated {
            Collect::Next
        } else {
            Collect::First
        };
        Ok(self.value.update_field(index, collect, state))
    }

    fn value_for_key(
        &mut self,
        key: &str,
        state: &mut State,
    ) -> Result<Option<SinkHandle<'_, 'de>>, Error> {
        // the value is deserialized like the value of a key (so that the
        // struct can be flattened)
        match (self.key.lookup)(key) {
            Some(index) => {
                self.key.set_index(index);
                self.next_value(state).map(Some)
            }
            None => Ok(None),
        }
    }
}

/// Returns the errors a struct collected together with the fields that
/// are missing.
///
/// `missing` tells for the required fields (with the indexes and names
/// given) if they have no value.  Fields that were seen but have no
/// value failed, they are not missing.
#[cold]
#[inline(never)]
pub fn collected_errors(
    errors: &mut CollectedErrors,
    seen: &[u64],
    missing: &[bool],
    indexes: &[usize],
    names: &[&str],
    state: &State,
) -> Error {
    for ((missing, index), name) in missing.iter().zip(indexes).zip(names) {
        if *missing && !is_seen(seen, *index) {
            errors.push(new_missing_field_error(name, state), state);
        }
    }
    match errors.take() {
        Some(err) => err,
        None => unreachable!(),
    }
}

/// Creates the error for the first missing field.
///
/// If errors are collected, the error holds all missing fields.
#[cold]
pub fn missing_field(missing: &[bool], names: &[&str], state: &State) -> Error {
    if state.collects_errors() {
        let errors = missing
            .iter()
            .zip(names)
            .filter(|(missing, _)| **missing)
            .map(|(_, name)| state.error_in_context(new_missing_field_error(name, state)));
        if let Some(err) = Error::from_errors(errors) {
            return err;
        }
    }
    let index = missing.iter().position(|x| *x).unwrap_or_default();
    new_missing_field_error(names[index], state)
}

/// Creates the error for a missing field.
#[cold]
pub fn new_missing_field_error(name: &str, state: &State) -> Error {
    if state.discards_errors {
        return discarded_error(ErrorKind::MissingField);
    }
    Error::new(ErrorKind::MissingField, format!("missing field `{}`", name))
}