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
//! Support for updating existing values (see
//! [`Deserialize::deserialize_update`]).
use alloc::borrow::Cow;
use core::marker::PhantomData;
use core::ptr::NonNull;

use crate::State;
use crate::arena::ArenaBox;
use crate::de::{Deserialize, OwnedSink, Sink, SinkHandle, is_null_atom};
use crate::error::Error;
use crate::event::Atom;

/// The part of replacing a value that depends on its type.
///
/// [`ReplaceSink`] is the same for all types (it exists once), it only
/// calls into this.
trait Replace<'de>: Send {
    /// Returns the sink of the new value.
    fn sink(&mut self) -> &mut (dyn Sink<'de> + '_);

    /// Returns the sink of the new value.
    fn sink_ref(&self) -> &(dyn Sink<'de> + '_);

    /// Replaces the value with the new value (if there is one).
    fn replace(&mut self);
}

/// Replaces a value of a type.
struct Replacer<'a, 'de, T> {
    out: &'a mut T,
    sink: OwnedSink<'de, T>,
}

impl<'a, 'de, T: Send> Replace<'de> for Replacer<'a, 'de, T> {
    fn sink(&mut self) -> &mut (dyn Sink<'de> + '_) {
        self.sink.get_mut()
    }

    fn sink_ref(&self) -> &(dyn Sink<'de> + '_) {
        self.sink.get()
    }

    fn replace(&mut self) {
        if let Some(value) = self.sink.take() {
            *self.out = value;
        }
    }
}

/// A sink that replaces a value.
///
/// The new value is deserialized into an owned sink and replaces the value
/// once it's complete.
struct ReplaceSink<'a, 'de> {
    // in the arena, the sink exists once for all types
    inner: ArenaBox<dyn Replace<'de> + 'a>,
}

/// Creates a sink handle that replaces a value with a value that is
/// deserialized with a sink the function creates.
///
/// This is the default implementation of
/// [`Deserialize::deserialize_update`].
pub(crate) fn replace_handle_with<'a, 'de, T: Send + 'a>(
    out: &'a mut T,
    make: for<'x> fn(&'x mut Option<T>, &mut State) -> SinkHandle<'x, 'de>,
    state: &mut State,
) -> SinkHandle<'a, 'de> {
    replace_with(out, OwnedSink::with(make, state), state)
}

/// Creates a sink handle that updates a value and checks it once the update
/// is complete.
///
/// `update` creates the sink that updates the value (for instance
/// [`Deserialize::deserialize_update`] of the type or an adapter).  Once
/// the update is complete, `check` is invoked with the updated value.
/// Errors it returns point at the start of the value in the input.  The
/// value is updated in place, if the check fails it's updated anyway (like
/// when an update fails, see [`Deserialize::deserialize_update`]).  This is
/// for adapters that check values to support updates:
///
/// ```
/// use deser::de::{DeserializeDriver, checked_update};
/// use deser::{Deserialize, Error, ErrorKind, Event};
///
/// #[derive(Deserialize)]
/// struct Range {
///     min: u32,
///     max: u32,
/// }
///
/// fn check(range: &Range) -> Result<(), Error> {
///     if range.min > range.max {
///         return Err(Error::new(
///             ErrorKind::InvalidValue,
///             "min is larger than max",
///         ));
///     }
///     Ok(())
/// }
///
/// let mut range = Range { min: 1, max: 5 };
/// let mut driver = DeserializeDriver::from_fn(|state| {
///     checked_update(&mut range, Range::deserialize_update, check, state)
/// });
/// driver.emit(Event::map_start()).unwrap();
/// driver.emit("min").unwrap();
/// driver.emit(10u64).unwrap();
/// let err = driver.emit(Event::MapEnd).unwrap_err();
/// assert_eq!(err.message(), "min is larger than max");
/// ```
pub fn checked_update<'a, 'de, T: Send + 'a>(
    value: &'a mut T,
    update: for<'x> fn(&'x mut T, &mut State) -> SinkHandle<'x, 'de>,
    check: fn(&T) -> Result<(), Error>,
    state: &mut State,
) -> SinkHandle<'a, 'de> {
    let ptr = NonNull::from(value);
    // SAFETY: the sink borrows the value for 'a, the pointer is only used
    // again once the sink was dropped (in `finish`).
    let sink = update(unsafe { &mut *ptr.as_ptr() }, state);
    SinkHandle::arena(
        CheckedUpdateSink {
            value: ptr,
            sink: Some(sink),
            check,
            start: None,
            _marker: PhantomData,
        },
        state,
    )
}

/// The sink of [`checked_update`].
struct CheckedUpdateSink<'a, 'de, T> {
    value: NonNull<T>,
    // borrows the value, `None` once the update is complete
    sink: Option<SinkHandle<'a, 'de>>,
    check: fn(&T) -> Result<(), Error>,
    // the start of the value in the input
    start: Option<usize>,
    _marker: PhantomData<&'a mut T>,
}

// SAFETY: the sink holds a mutable reference to the value (as pointer).
unsafe impl<T: Send> Send for CheckedUpdateSink<'_, '_, T> {}

impl<'a, 'de, T> CheckedUpdateSink<'a, 'de, T> {
    fn sink(&mut self) -> &mut SinkHandle<'a, 'de> {
        self.sink.as_mut().expect("update is complete")
    }

    fn begin(&mut self, state: &State) -> &mut SinkHandle<'a, 'de> {
        if self.start.is_none() {
            self.start = state.input_range().map(|x| x.start);
        }
        self.sink()
    }
}

impl<'a, 'de, T: Send> Sink<'de> for CheckedUpdateSink<'a, 'de, T> {
    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.begin(state).atom(atom, state)
    }

    fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
        self.begin(state).borrowed_atom(atom, state)
    }

    fn map(&mut self, state: &mut State) -> Result<(), Error> {
        self.begin(state).map(state)
    }

    fn seq(&mut self, state: &mut State) -> Result<(), Error> {
        self.begin(state).seq(state)
    }

    fn next_key(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        self.sink().next_key(state)
    }

    fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        self.sink().next_value(state)
    }

    fn __private_key_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.sink().__private_key_atom(atom, state)
    }

    fn __private_value_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.sink().__private_value_atom(atom, state)
    }

    fn __private_borrowed_key_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        self.sink().__private_borrowed_key_atom(atom, state)
    }

    fn __private_borrowed_value_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        self.sink().__private_borrowed_value_atom(atom, state)
    }

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

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

    fn finish(&mut self, state: &mut State) -> Result<(), Error> {
        let rv = self.sink().finish(state);
        // the sink borrows the value, it's dropped before the value is used
        self.sink = None;
        rv?;
        // SAFETY: nothing borrows the value anymore
        let value = unsafe { self.value.as_ref() };
        (self.check)(value).map_err(|mut err| {
            if let (None, Some(start)) = (err.offset(), self.start) {
                err.set_offset(start);
            }
            err
        })
    }

    fn expecting(&self) -> Cow<'_, str> {
        match self.sink {
            Some(ref sink) => sink.expecting(),
            None => Cow::Borrowed("compatible type"),
        }
    }
}

/// Creates a sink handle that replaces a value with the value of an owned
/// sink.
pub(crate) fn replace_with<'a, 'de, T: Send + 'a>(
    out: &'a mut T,
    sink: OwnedSink<'de, T>,
    state: &mut State,
) -> SinkHandle<'a, 'de> {
    let inner = ArenaBox::into_raw(ArenaBox::new(Replacer { out, sink }, &mut state.arena));
    // SAFETY: the pointer comes from the box
    let inner = unsafe { ArenaBox::from_raw(inner.as_ptr() as *mut (dyn Replace<'de> + 'a)) };
    SinkHandle::arena(ReplaceSink { inner }, state)
}

impl<'a, 'de> Sink<'de> for ReplaceSink<'a, 'de> {
    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.inner.get_mut().sink().atom(atom, state)
    }

    fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
        self.inner.get_mut().sink().borrowed_atom(atom, state)
    }

    fn map(&mut self, state: &mut State) -> Result<(), Error> {
        self.inner.get_mut().sink().map(state)
    }

    fn seq(&mut self, state: &mut State) -> Result<(), Error> {
        self.inner.get_mut().sink().seq(state)
    }

    fn next_key(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        self.inner.get_mut().sink().next_key(state)
    }

    fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        self.inner.get_mut().sink().next_value(state)
    }

    fn __private_key_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.inner.get_mut().sink().__private_key_atom(atom, state)
    }

    fn __private_value_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.inner
            .get_mut()
            .sink()
            .__private_value_atom(atom, state)
    }

    fn __private_borrowed_key_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        self.inner
            .get_mut()
            .sink()
            .__private_borrowed_key_atom(atom, state)
    }

    fn __private_borrowed_value_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        self.inner
            .get_mut()
            .sink()
            .__private_borrowed_value_atom(atom, state)
    }

    fn value_for_key(
        &mut self,
        key: &str,
        state: &mut State,
    ) -> Result<Option<SinkHandle<'_, 'de>>, Error> {
        self.inner.get_mut().sink().value_for_key(key, state)
    }

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

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

    fn finish(&mut self, state: &mut State) -> Result<(), Error> {
        self.inner.get_mut().sink().finish(state)?;
        self.inner.get_mut().replace();
        Ok(())
    }
}

/// A sink that updates the value of an `Option` which is set.
///
/// The value is moved into an owned sink which updates it.  Null clears the
/// option, everything else updates the value.  The value is moved back
/// when the update finished, or failed.
struct OptionUpdateSink<'a, 'de, T> {
    out: &'a mut Option<T>,
    sink: OwnedSink<'de, T>,
}

/// Creates a sink handle that updates an `Option`.
///
/// If the option is set, the value in it is updated, otherwise a new value
/// is deserialized.  Null clears the option.
/// The value is updated with the adapter `A`.
pub(crate) fn update_option<'a, 'de, T: Send + 'a, A: Deserialize<'de, T>>(
    out: &'a mut Option<T>,
    state: &mut State,
) -> SinkHandle<'a, 'de> {
    match out.take() {
        Some(value) => SinkHandle::arena(
            OptionUpdateSink {
                out,
                sink: OwnedSink::update(value, A::deserialize_update, state),
            },
            state,
        ),
        None => replace_handle_with(
            out,
            <Option<A> as Deserialize<'de, Option<T>>>::deserialize_into,
            state,
        ),
    }
}

impl<'a, 'de, T: Send> Sink<'de> for OptionUpdateSink<'a, 'de, T> {
    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        if is_null_atom(&atom) {
            // the value is dropped, the option remains empty
            drop(self.sink.take());
            return Ok(());
        }
        self.sink.get_mut().atom(atom, state)
    }

    fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
        if is_null_atom(&atom) {
            drop(self.sink.take());
            return Ok(());
        }
        self.sink.get_mut().borrowed_atom(atom, state)
    }

    fn map(&mut self, state: &mut State) -> Result<(), Error> {
        self.sink.get_mut().map(state)
    }

    fn seq(&mut self, state: &mut State) -> Result<(), Error> {
        self.sink.get_mut().seq(state)
    }

    forward_to_owned!(sink);

    fn finish(&mut self, state: &mut State) -> Result<(), Error> {
        self.sink.get_mut().finish(state)?;
        *self.out = self.sink.take();
        Ok(())
    }
}

impl<'a, 'de, T> Drop for OptionUpdateSink<'a, 'de, T> {
    fn drop(&mut self) {
        // if the update failed, the (partially updated) value is put back
        if let Some(value) = self.sink.take() {
            *self.out = Some(value);
        }
    }
}

/// A struct that is updated field by field.
///
/// The update sinks of derived structs with flattened fields keep the sinks
/// of the flattened fields (which borrow them) for the whole update while
/// they update the other fields, and look at the whole struct once they are
/// done (to validate it).  A mutable reference to the struct cannot be used
/// for this, so the fields are borrowed through a pointer.  The derive
/// borrows every field at most once at a time and only borrows the struct
/// as a whole once the borrows of all fields ended.
#[cfg(feature = "derive")]
#[doc(hidden)]
pub struct UpdateTarget<'a, T> {
    ptr: core::ptr::NonNull<T>,
    _marker: core::marker::PhantomData<&'a mut T>,
}

// SAFETY: this is a mutable reference to `T`.
#[cfg(feature = "derive")]
unsafe impl<T: Send> Send for UpdateTarget<'_, T> {}

#[cfg(feature = "derive")]
impl<'a, T> UpdateTarget<'a, T> {
    /// Creates the target for a value.
    #[inline]
    pub fn new(value: &'a mut T) -> UpdateTarget<'a, T> {
        UpdateTarget {
            ptr: core::ptr::NonNull::from(value),
            _marker: core::marker::PhantomData,
        }
    }

    /// Returns the pointer to the value.
    ///
    /// Fields are borrowed with `&mut (*target.as_ptr()).field`, which does
    /// not borrow the other fields.
    #[inline]
    pub fn as_ptr(&self) -> *mut T {
        self.ptr.as_ptr()
    }
}

/// Collections that collect the values of a repeated key one at a time.
///
/// In a multimap (see
/// [`ContainerShape::set_multimap`](crate::ContainerShape::set_multimap))
/// the fields of structs and the values of maps with these types receive
/// every value of their key (see [`Deserialize::__private_collects`]).
pub(crate) trait Collection<T>: Sized + Send {
    /// Returns an empty collection.
    fn empty() -> Self;

    /// Adds a value to the collection.
    fn add(&mut self, value: T) -> Result<(), Error>;
}

/// Where a collected value goes.
enum CollectTarget<'a, C> {
    /// The slot of a value that is deserialized, the collection is created
    /// if it's empty.
    Slot(&'a mut Option<C>),
    /// A collection that is updated.
    Value(&'a mut C),
}

/// The sink of one value of a repeated key that is added to a collection
/// once it's complete.
///
/// A value that is a sequence which the element rejects is the values of
/// the key: they are all added to the collection (like `multi[]=a` in a
/// query string for a `Vec<String>`).
struct CollectSink<'a, 'de, C, T, A> {
    target: CollectTarget<'a, C>,
    element: OwnedSink<'de, T>,
    // `true` if the value is a sequence whose items are added
    extend: bool,
    _marker: PhantomData<fn() -> A>,
}

impl<'a, 'de, C: Collection<T>, T: Send> CollectSink<'a, 'de, C, T, ()> {
    /// Adds the value of the element (if there is one).
    fn add(
        target: &mut CollectTarget<'a, C>,
        element: &mut OwnedSink<'de, T>,
    ) -> Result<(), Error> {
        // a null (for an optional element) leaves no value
        if let Some(value) = element.take() {
            match *target {
                CollectTarget::Slot(ref mut slot) => {
                    slot.get_or_insert_with(C::empty).add(value)?
                }
                CollectTarget::Value(ref mut collection) => collection.add(value)?,
            }
        }
        Ok(())
    }
}

impl<'a, 'de, C, T, A> Sink<'de> for CollectSink<'a, 'de, C, T, A>
where
    C: Collection<T>,
    T: Send,
    A: Deserialize<'de, T>,
{
    fn atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.element.get_mut().atom(atom, state)
    }

    fn borrowed_atom(&mut self, atom: Atom<'de>, state: &mut State) -> Result<(), Error> {
        self.element.get_mut().borrowed_atom(atom, state)
    }

    fn map(&mut self, state: &mut State) -> Result<(), Error> {
        self.element.get_mut().map(state)
    }

    fn seq(&mut self, state: &mut State) -> Result<(), Error> {
        match self.element.get_mut().seq(state) {
            Err(err) if err.kind().is_rejection() => {
                // the element that rejected the sequence is not a value
                // (the slots of optionals are set when they are created)
                self.element = OwnedSink::null(state);
                self.extend = true;
                Ok(())
            }
            rv => rv,
        }
    }

    fn next_key(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        self.element.get_mut().next_key(state)
    }

    fn next_value(&mut self, state: &mut State) -> Result<SinkHandle<'_, 'de>, Error> {
        if self.extend {
            // the previous item was finished by the driver
            CollectSink::<C, T, ()>::add(&mut self.target, &mut self.element)?;
            self.element = OwnedSink::deserialize_as::<A>(state);
            return Ok(SinkHandle::to(self.element.get_mut()));
        }
        self.element.get_mut().next_value(state)
    }

    fn __private_key_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        self.element.get_mut().__private_key_atom(atom, state)
    }

    fn __private_value_atom(&mut self, atom: Atom, state: &mut State) -> Result<(), Error> {
        if self.extend {
            return crate::de::atom_into_handle(self.next_value(state)?, atom, state);
        }
        self.element.get_mut().__private_value_atom(atom, state)
    }

    fn __private_borrowed_key_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        self.element
            .get_mut()
            .__private_borrowed_key_atom(atom, state)
    }

    fn __private_borrowed_value_atom(
        &mut self,
        atom: Atom<'de>,
        state: &mut State,
    ) -> Result<(), Error> {
        if self.extend {
            return crate::de::borrowed_atom_into_handle(self.next_value(state)?, atom, state);
        }
        self.element
            .get_mut()
            .__private_borrowed_value_atom(atom, state)
    }

    fn value_for_key(
        &mut self,
        key: &str,
        state: &mut State,
    ) -> Result<Option<SinkHandle<'_, 'de>>, Error> {
        if self.extend {
            return Ok(None);
        }
        self.element.get_mut().value_for_key(key, state)
    }

    fn recover(&mut self, err: Error, state: &mut State) -> Result<(), Error> {
        if self.extend {
            return Err(err);
        }
        self.element.get_mut().recover(err, state)
    }

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

    fn finish(&mut self, state: &mut State) -> Result<(), Error> {
        if !self.extend {
            self.element.get_mut().finish(state)?;
        }
        CollectSink::<C, T, ()>::add(&mut self.target, &mut self.element)
    }
}

/// Creates the sink of a value that is added to the collection in a slot.
///
/// This implements [`Deserialize::__private_collect_into`] for collections.
pub(crate) fn collect_into<'a, 'de, C, T, A>(
    out: &'a mut Option<C>,
    state: &mut State,
) -> SinkHandle<'a, 'de>
where
    C: Collection<T> + 'a,
    T: Send + 'a,
    A: Deserialize<'de, T>,
{
    let element = OwnedSink::deserialize_as::<A>(state);
    // SAFETY: `A` is an adapter, the sink only holds a marker of it
    unsafe {
        SinkHandle::arena_unbounded(
            CollectSink {
                target: CollectTarget::Slot(out),
                element,
                extend: false,
                _marker: PhantomData::<fn() -> A>,
            },
            state,
        )
    }
}

/// Creates the sink of a value that is added to a collection that is
/// updated.
///
/// This implements [`Deserialize::__private_collect_update`] for
/// collections.  The first value of a key replaces the collection.
pub(crate) fn collect_update<'a, 'de, C, T, A>(
    value: &'a mut C,
    first: bool,
    state: &mut State,
) -> SinkHandle<'a, 'de>
where
    C: Collection<T> + 'a,
    T: Send + 'a,
    A: Deserialize<'de, T>,
{
    if first {
        *value = C::empty();
    }
    let element = OwnedSink::deserialize_as::<A>(state);
    // SAFETY: `A` is an adapter, the sink only holds a marker of it
    unsafe {
        SinkHandle::arena_unbounded(
            CollectSink {
                target: CollectTarget::Value(value),
                element,
                extend: false,
                _marker: PhantomData::<fn() -> A>,
            },
            state,
        )
    }
}