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
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
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::marker::PhantomData;
use core::ptr::NonNull;

use crate::arena::{Alloc, Buffer};
use crate::error::Error;
use crate::ser::layer::{EventFn, Layer, Next};
use crate::ser::{
    Begin, BeginKind, Boxed, ContainerShape, Emit, Erased, FIELDS_END, HandleInner, IndexedSeq,
    IndexedStruct, PLAIN_BUDGET, PlainSink, Serialize, SerializeRef, StructField,
};
use crate::unwind::DropInReverse;
use crate::{Atom, Event, State};
use crate::{Context, Text};

use super::{MapEmitter, SeqEmitter, SerializeHandle, StructEmitter};

/// The driver allows serializing a [`Serialize`] iteratively.
///
/// This is the only way to convert from a [`Serialize`] into an event
/// stream.  There are several ways to receive the events:
///
/// * [`drive`](Self::drive) invokes a callback for every event and
///   [`drive_sink`](Self::drive_sink) delivers them to an [`EventSink`].
///   Both serialize the value at once.
/// * [`drive_until`](Self::drive_until) delivers the events to an
///   [`EventSink`] which can pause the driver, for instance to write the
///   output of large values in pieces.
/// * [`next`](Self::next) returns one event at a time.
///
/// Event sinks can also receive the values of the events to
/// [describe](crate::ser::Describe) them (see [`EventSink::DESCRIBED`]).
///
/// When the serialization fails, the error gets the context of the
/// current value attached (see [`State::add_error_context`]).
///
/// # Layers
///
/// [`Layer`]s sit between the serialized values and the format and see
/// every event before the format receives it.  They are added with
/// [`push_layer`](Self::push_layer) and are not supported by
/// [`next`](Self::next).
pub struct SerializeDriver<'a> {
    state: State,
    layers: Vec<Box<dyn Layer>>,
    // Values and frames refer to data borrowed from the serializables and
    // emitters of the frames below them, which is why the lifetimes are
    // erased.  `next_value` and `needs_finish` borrow from the top frame.
    //
    // A value that was produced by an emitter (or the root value) that still
    // needs to be serialized.
    next_value: Option<Held>,
    // A value that was fully emitted.  It's held until the next call as the
    // last event can borrow from it.  If the flag is set, `finish` needs to
    // be called on it.
    needs_finish: Option<(Held, bool)>,
    stack: Vec<Frame>,
    // `true` if `next` returned an event.  Its event data is detached on the
    // next call.
    delivered: bool,
    _marker: PhantomData<SerializeRef<'a>>,
}

/// A compound value that is currently being serialized.
struct Frame {
    // `emitter` must be declared (and thus dropped) before `serializable` as
    // the emitters borrow from the serializable.
    emitter: Emitter,
    serializable: Held,
    needs_finish: bool,
}

enum Emitter {
    Seq(Boxed<dyn SeqEmitter>),
    /// A map emitter, the flag is `true` if a value is expected next.
    Map(Boxed<dyn MapEmitter>, bool),
    Struct(Boxed<dyn StructEmitter>),
    /// A sequence with the index of the next element.
    IndexedSeq(&'static dyn IndexedSeq, usize),
    /// A struct with the index of the next field.
    IndexedStruct(&'static dyn IndexedStruct, usize),
    /// A value that forwarded to another value (see [`Emit::Forward`]).
    ///
    /// The frame holds the value while the forwarded value (which can
    /// borrow from it) is serialized.  It does not emit events and it's
    /// removed once it's on the top of the stack again.
    Forward,
}

impl Emitter {
    /// Drops the emitter, it's popped from the arena right away if it's on
    /// the top.
    #[inline(always)]
    fn release(self, state: &mut State) {
        match self {
            Emitter::Seq(emitter) => Boxed::release(emitter, state),
            Emitter::Map(emitter, _) => Boxed::release(emitter, state),
            Emitter::Struct(emitter) => Boxed::release(emitter, state),
            _ => {}
        }
    }
}

/// A serializable held by the driver.
///
/// This is like a [`SerializeHandle`] with an erased lifetime, but owned
/// values are held by raw pointer so that the handle can be moved while
/// events or emitters borrow from the value.
pub(crate) struct Held {
    ptr: NonNull<dyn Erased>,
    /// Where the value is allocated if it's owned, `None` if it's borrowed.
    owned: Option<Alloc>,
}

// SAFETY: a held value is either a borrowed `SerializeRef` (which is
// `Send` as serializables are `Sync`) or an owned
// `Boxed<dyn Erased + Send>`.
unsafe impl Send for Held {}

impl Held {
    /// Creates a held value from a handle.
    ///
    /// # Safety
    ///
    /// The held value must be dropped before the data the handle borrows.
    #[inline]
    pub(crate) unsafe fn new(handle: SerializeHandle<'_>) -> Held {
        unsafe {
            let (ptr, owned) = match handle.0 {
                HandleInner::Borrowed(value) => (NonNull::from(value.as_dyn()), None),
                HandleInner::Owned(value) => {
                    let (ptr, alloc) = Boxed::into_raw(value);
                    let ptr: NonNull<dyn Erased + '_> = ptr;
                    (ptr, Some(alloc))
                }
            };
            Held {
                ptr: core::mem::transmute::<NonNull<dyn Erased + '_>, NonNull<dyn Erased>>(ptr),
                owned,
            }
        }
    }

    /// Returns the value with an unbounded lifetime.
    ///
    /// # Safety
    ///
    /// The returned reference must not be used after the held value was
    /// dropped.
    #[inline(always)]
    pub(crate) unsafe fn get<'x>(&self) -> SerializeRef<'x> {
        SerializeRef::from_dyn(unsafe { &*self.ptr.as_ptr() })
    }
}

impl Drop for Held {
    #[inline(always)]
    fn drop(&mut self) {
        #[cold]
        #[inline(never)]
        unsafe fn drop_owned(ptr: NonNull<dyn Erased>, alloc: Alloc) {
            unsafe {
                drop(Boxed::from_raw(ptr, alloc));
            }
        }

        if let Some(alloc) = self.owned {
            // SAFETY: owned values were created from a box of the allocation
            unsafe { drop_owned(self.ptr, alloc) };
        }
    }
}

impl<'a> Drop for SerializeDriver<'a> {
    fn drop(&mut self) {
        // the pending values borrow from the top frame and inner frames can
        // borrow from outer frames, drop in inverse order.  This order is
        // kept if a drop panics: the guard drops the remaining frames in
        // inverse order and as locals are dropped in inverse order (also
        // when unwinding), the pending values are dropped before it.
        let frames = DropInReverse(&mut self.stack);
        let needs_finish = self.needs_finish.take();
        let next_value = self.next_value.take();
        drop(next_value);
        drop(needs_finish);
        while let Some(frame) = frames.0.pop() {
            // the emitter borrows from the serializable, drop it first
            frame.emitter.release(&mut self.state);
        }
        drop(frames);
        let stack = core::mem::take(&mut self.stack);
        self.state.arena.put_vec(Buffer::SerializeStack, stack);
    }
}

const STACK_CAPACITY: usize = 128;

// an ongoing serialization can move between threads, for instance when it
// is suspended while waiting for IO.
const _: () = {
    const fn assert_send<T: Send>() {}
    assert_send::<SerializeDriver<'static>>();
};

type NextEvent<'a> = Option<(Event<'a>, SerializeRef<'a>)>;

/// The callback of [`SerializeDriver::drive`] and
/// [`SerializeDriver::drive_until`].
trait Callback {
    /// `true` if the callback receives the values of the events.
    const DESCRIBED: bool;

    /// `true` if the callback can pause the driver (see
    /// [`SerializeDriver::drive_until`]).
    ///
    /// Plain values are only emitted at once if they fit into the budget
    /// (see `PLAIN_BUDGET`), large plain sequences are emitted in pieces
    /// so that the driver can pause in between.
    const PAUSABLE: bool = false;

    /// `true` if the fast paths for plain values are used.
    ///
    /// Callbacks that describe values need to see every value.
    const FAST: bool = !Self::DESCRIBED;

    /// `true` if large plain sequences are emitted at once.
    const UNBOUNDED: bool = Self::FAST && !Self::PAUSABLE;

    fn call(
        &mut self,
        event: Event<'_>,
        value: SerializeRef<'_>,
        state: &mut State,
    ) -> Result<(), Error>;

    /// Returns `true` if the driver should pause before the next value.
    #[inline(always)]
    fn pause(&mut self) -> bool {
        false
    }
}

/// A callback that does not receive values.
struct Plain<F>(F);

impl<F: FnMut(Event<'_>, &mut State) -> Result<(), Error>> Callback for Plain<F> {
    const DESCRIBED: bool = false;

    #[inline(always)]
    fn call(
        &mut self,
        event: Event<'_>,
        _value: SerializeRef<'_>,
        state: &mut State,
    ) -> Result<(), Error> {
        (self.0)(event, state)
    }
}

/// Receives the events of [`SerializeDriver::drive_sink`] and
/// [`SerializeDriver::drive_until`].
///
/// Unlike the callback of [`drive`](SerializeDriver::drive) an event
/// sink can:
///
/// * pause the driver (with [`drive_until`](SerializeDriver::drive_until)):
///   before the next value is serialized the driver asks the sink with
///   [`pause`](Self::pause) if it should stop.  This is used to write the
///   output of large values in pieces, for instance to wait until the
///   output that was produced so far was written to a socket.
/// * receive the values of the events (see [`DESCRIBED`](Self::DESCRIBED))
///   to [describe](crate::ser::Describe) them, which formats that reflect
///   the Rust shape of values need.
///
/// ```
/// # use deser::ser::{Describe, EventSink, SerializeDriver, SerializeRef};
/// # use deser::{Error, Event, State};
/// /// Records if the values are `Some`.
/// struct IsSome(Vec<bool>);
///
/// struct Describer(bool);
///
/// impl Describe for Describer {
///     fn some(&mut self) {
///         self.0 = true;
///     }
/// }
///
/// impl EventSink for IsSome {
///     const DESCRIBED: bool = true;
///
///     fn event(
///         &mut self,
///         _event: Event<'_>,
///         value: SerializeRef<'_>,
///         _state: &mut State,
///     ) -> Result<(), Error> {
///         let mut describer = Describer(false);
///         value.describe(&mut describer);
///         self.0.push(describer.0);
///         Ok(())
///     }
/// }
///
/// # fn do_it() -> Result<(), deser::Error> {
/// let mut sink = IsSome(Vec::new());
/// let value = vec![Some(1), None];
/// SerializeDriver::new(&value).drive_sink(&mut sink)?;
/// assert_eq!(sink.0, [false, true, false, false]);
/// # Ok(()) } do_it().unwrap();
/// ```
pub trait EventSink {
    /// `true` if the sink receives the values of the events.
    ///
    /// Otherwise the value passed to [`event`](Self::event) describes
    /// nothing.  Values are only passed on if they are wanted as the
    /// driver serializes values faster if it does not need to hand out
    /// every one of them.
    const DESCRIBED: bool = false;

    /// Receives an event.
    ///
    /// Formats can mark the implementation as `#[inline(always)]` which
    /// makes the compiler specialize it for every kind of event the driver
    /// delivers (which is not possible with the callback of
    /// [`drive`](SerializeDriver::drive)).
    fn event(
        &mut self,
        event: Event<'_>,
        value: SerializeRef<'_>,
        state: &mut State,
    ) -> Result<(), Error>;

    /// Returns `true` if the driver should pause.
    ///
    /// This is invoked between values: before the values in maps and
    /// sequences (not the keys of structs) and between the pieces of large
    /// values that only hold atoms.  After the first value of a call to
    /// [`drive_until`](SerializeDriver::drive_until) returns `true`, the
    /// call returns.  The default implementation never pauses.
    ///
    /// [`drive_sink`](SerializeDriver::drive_sink) does not invoke this.
    fn pause(&mut self) -> bool {
        false
    }
}

/// A callback that delivers to an event sink and does not pause.
struct Sink<'s, S>(&'s mut S);

impl<S: EventSink> Callback for Sink<'_, S> {
    const DESCRIBED: bool = S::DESCRIBED;

    #[inline(always)]
    fn call(
        &mut self,
        event: Event<'_>,
        value: SerializeRef<'_>,
        state: &mut State,
    ) -> Result<(), Error> {
        self.0.event(event, value, state)
    }
}

/// A callback that delivers to an event sink which can pause.
struct Pausable<'s, S>(&'s mut S);

impl<S: EventSink> Callback for Pausable<'_, S> {
    const DESCRIBED: bool = S::DESCRIBED;
    const PAUSABLE: bool = true;

    #[inline(always)]
    fn call(
        &mut self,
        event: Event<'_>,
        value: SerializeRef<'_>,
        state: &mut State,
    ) -> Result<(), Error> {
        self.0.event(event, value, state)
    }

    #[inline(always)]
    fn pause(&mut self) -> bool {
        self.0.pause()
    }
}

/// The value of the keys of structs, it describes nothing.
static FIELD_KEY: () = ();

/// Returns the value of the keys of structs.
#[inline(always)]
fn field_key() -> SerializeRef<'static> {
    SerializeRef::new(&FIELD_KEY)
}

/// Serializes a plain value into an `Emit`, for when every value is driven
/// on its own.
#[inline(never)]
fn serialize_plain<'x>(plain: SerializeRef<'x>, state: &mut State) -> Result<BeginKind<'x>, Error> {
    Ok(BeginKind::Emit(plain.serialize(state)?))
}

/// Delivers the events of plain values (see [`PlainSink`]).
///
/// The first event of a value is a key if `is_map_key` is set in the
/// state, it's reset after every such event.
struct PlainDelivery<'d, 'a, C> {
    driver: &'d mut SerializeDriver<'a>,
    f: &'d mut C,
}

impl<C: Callback> PlainDelivery<'_, '_, C> {
    /// Delivers the first event of a value.
    #[inline(always)]
    fn begin(&mut self, event: Event<'_>) -> Result<(), Error> {
        self.driver.deliver(self.f, event, field_key())?;
        self.driver.state.is_map_key = false;
        Ok(())
    }
}

impl<C: Callback> PlainSink for PlainDelivery<'_, '_, C> {
    #[inline]
    fn atom(&mut self, atom: Atom<'_>) -> Result<(), Error> {
        self.begin(Event::Atom(atom))
    }

    #[inline]
    fn seq_start(&mut self, shape: ContainerShape) -> Result<(), Error> {
        self.driver.state.depth += 1;
        self.begin(Event::SeqStart(shape))
    }

    #[inline]
    fn seq_end(&mut self) -> Result<(), Error> {
        self.driver.state.depth -= 1;
        self.driver.deliver(self.f, Event::SeqEnd, field_key())
    }

    #[inline]
    fn map_start(&mut self, shape: ContainerShape) -> Result<(), Error> {
        self.driver.state.depth += 1;
        self.begin(Event::MapStart(shape))
    }

    #[inline]
    fn map_end(&mut self) -> Result<(), Error> {
        self.driver.state.depth -= 1;
        self.driver.deliver(self.f, Event::MapEnd, field_key())
    }

    #[inline]
    fn key(&mut self) {
        self.driver.state.is_map_key = true;
    }

    #[inline]
    fn field(&mut self, name: &str) -> Result<(), Error> {
        self.driver.state.is_map_key = true;
        self.begin(Event::Atom(Atom::Str(Text::borrowed(name))))
    }
}

impl<'a> SerializeDriver<'a> {
    /// Creates a new driver which serializes the given value implementing [`Serialize`].
    #[inline]
    pub fn new<T: Serialize>(value: &'a T) -> SerializeDriver<'a> {
        SerializeDriver::from_ref(SerializeRef::new(value))
    }

    /// Creates a new driver which serializes the value of a reference.
    ///
    /// Unlike [`new`](Self::new) this is not generic, the reference can be
    /// to a value with an adapter (see [`SerializeRef::serialize_as`]).
    pub fn from_ref(serializable: SerializeRef<'a>) -> SerializeDriver<'a> {
        let mut state = State::new();
        // the stack of the last driver is reused
        let stack = state
            .arena
            .take_vec(Buffer::SerializeStack)
            .unwrap_or_else(|| Vec::with_capacity(STACK_CAPACITY));
        SerializeDriver {
            state,
            layers: Vec::new(),
            // SAFETY: the driver cannot outlive 'a
            next_value: Some(unsafe { Held::new(SerializeHandle::from(serializable)) }),
            needs_finish: None,
            stack,
            delivered: false,
            _marker: PhantomData,
        }
    }

    /// Returns a borrowed reference to the current serializer state.
    pub fn state(&self) -> &State {
        &self.state
    }

    /// Returns a mutable reference to the current serializer state.
    ///
    /// This can be used to place extension values into the state which the
    /// serializable values can then pick up.
    pub fn state_mut(&mut self) -> &mut State {
        &mut self.state
    }

    /// Sets the context of the serialization.
    ///
    /// The values of the context are the defaults of the extension values
    /// of the state (see [`Context`]).  This replaces the context of the
    /// driver.  Formats add the values of their own context for the types
    /// it has no value for (see
    /// [`set_default_context`](Self::set_default_context)).
    pub fn set_context(&mut self, context: Context) {
        self.state.set_context(context);
    }

    /// Adds the values of a context that the context of the driver has no
    /// value for.
    ///
    /// Formats use this for the context they were given (for instance the
    /// one of their configuration).  A context that was set on the driver
    /// before (for instance in the setup callback of `serialize_with`)
    /// takes precedence: its values are kept and the values of the given
    /// context are only added for the types it has no value for.
    #[inline(never)]
    pub fn set_default_context(&mut self, context: Context) {
        let mut merged = self.state.context().clone();
        if merged.fill_from(&context) {
            self.set_context(merged);
        }
    }

    /// Returns the context of the serialization.
    pub fn context(&self) -> &Context {
        self.state.context()
    }

    /// Adds a layer.
    ///
    /// Layers see the events in the order they were added: the layer that
    /// was added first sees the events produced by the values, the last one
    /// passes them on to the format.  See [`Layer`] for more information.
    pub fn push_layer<L: Layer + 'static>(&mut self, layer: L) {
        self.layers.push(Box::new(layer));
    }

    /// Produces the next serialization event.
    ///
    /// # Panics
    ///
    /// The driver will panic if the data fed from the serializer is
    /// malformed.  As layers can change the number of events, this method
    /// panics if layers were added.
    #[allow(clippy::should_implement_trait)]
    #[inline]
    pub fn next(&mut self) -> Result<Option<(Event<'_>, SerializeRef<'_>, &mut State)>, Error> {
        assert!(
            self.layers.is_empty(),
            "layers are only supported by SerializeDriver::drive"
        );
        let rv = match self.advance() {
            Ok(rv) => rv,
            Err(err) => return Err(self.state.error_in_context(err)),
        };
        self.delivered = rv.is_some();
        // The event and the value borrow from the values held by the driver
        // but never from the state (serializables cannot return `Emit`s
        // borrowing from it), which is why the state can be handed out
        // mutably.
        Ok(rv.map(|(event, value)| (event, value, &mut self.state)))
    }

    /// Detaches the event data of an event returned by `next`.
    #[inline(always)]
    fn detach_delivered_event_data(&mut self) {
        if self.delivered {
            self.delivered = false;
            self.state.clear_event_data();
        }
    }

    /// Drives the serialization to the end and invokes a callback for every
    /// event.
    ///
    /// This produces the same events as calling [`next`](Self::next) until
    /// it returns `None` but it's faster.  The first error (either produced
    /// by a serializable or returned by the callback) aborts the
    /// serialization.  To receive the values of the events or to pause the
    /// serialization, use an [`EventSink`].
    ///
    /// ```
    /// # use deser::ser::SerializeDriver;
    /// # fn do_it() -> Result<(), deser::Error> {
    /// let serializable = vec!["foo", "bar", "baz"];
    /// let mut events = Vec::new();
    /// SerializeDriver::new(&serializable).drive(|event, _state| {
    ///     events.push(event.to_static());
    ///     Ok(())
    /// })?;
    /// assert_eq!(events.len(), 5);
    /// # Ok(()) } do_it().unwrap();
    /// ```
    #[inline]
    pub fn drive<F>(&mut self, f: F) -> Result<(), Error>
    where
        F: FnMut(Event<'_>, &mut State) -> Result<(), Error>,
    {
        match self.drive_impl(Plain(f)) {
            Ok(_) => Ok(()),
            Err(err) => Err(self.state.error_in_context(err)),
        }
    }

    /// Like [`drive`](Self::drive) but delivers the events to an
    /// [`EventSink`].
    ///
    /// The sink is not asked to pause (see [`drive_until`](Self::drive_until)),
    /// the value is serialized at once.
    #[inline]
    pub fn drive_sink<S: EventSink>(&mut self, sink: &mut S) -> Result<(), Error> {
        match self.drive_impl(Sink(sink)) {
            Ok(_) => Ok(()),
            Err(err) => Err(self.state.error_in_context(err)),
        }
    }

    /// Drives the serialization until it's complete or the sink pauses it.
    ///
    /// Returns `true` once the serialization is complete.  If the sink
    /// paused the driver (see [`EventSink::pause`]), `false` is returned
    /// and the next call continues where this one stopped.  At least one
    /// value is serialized per call.
    ///
    /// Unlike [`drive`](Self::drive), which emits values that only hold
    /// atoms (like a `Vec<u64>`) at once, the driver emits such values in
    /// pieces of a few hundred atoms and can pause in between.  The amount
    /// of events between two pauses only depends on the size of the atoms,
    /// not on the size of the value.
    ///
    /// ```
    /// # use deser::ser::{EventSink, SerializeDriver, SerializeRef};
    /// # use deser::{Error, Event, State};
    /// /// Collects events and pauses once it holds 100.
    /// struct Collect(Vec<Event<'static>>);
    ///
    /// impl EventSink for Collect {
    ///     fn event(
    ///         &mut self,
    ///         event: Event<'_>,
    ///         _value: SerializeRef<'_>,
    ///         _state: &mut State,
    ///     ) -> Result<(), Error> {
    ///         self.0.push(event.to_static());
    ///         Ok(())
    ///     }
    ///
    ///     fn pause(&mut self) -> bool {
    ///         self.0.len() >= 100
    ///     }
    /// }
    ///
    /// # fn do_it() -> Result<(), deser::Error> {
    /// let value: Vec<u64> = (0..10_000).collect();
    /// let mut driver = SerializeDriver::new(&value);
    /// let mut sink = Collect(Vec::new());
    /// let mut events = 0;
    /// loop {
    ///     let done = driver.drive_until(&mut sink)?;
    ///     // the events so far are processed while the driver is paused
    ///     assert!(sink.0.len() < 1000);
    ///     events += sink.0.len();
    ///     sink.0.clear();
    ///     if done {
    ///         break;
    ///     }
    /// }
    /// assert_eq!(events, 10_002);
    /// # Ok(()) } do_it().unwrap();
    /// ```
    #[inline]
    pub fn drive_until<S: EventSink>(&mut self, sink: &mut S) -> Result<bool, Error> {
        match self.drive_impl(Pausable(sink)) {
            Ok(done) => Ok(done),
            Err(err) => Err(self.state.error_in_context(err)),
        }
    }

    /// Delivers an event to the layers and the callback of
    /// [`drive`](Self::drive).
    #[inline(always)]
    fn deliver<C: Callback>(
        &mut self,
        f: &mut C,
        event: Event<'_>,
        value: SerializeRef<'_>,
    ) -> Result<(), Error> {
        // the value is only passed on if the callback wants it
        let value = if C::DESCRIBED { value } else { field_key() };
        if self.layers.is_empty() {
            f.call(event, value, &mut self.state)?;
        } else {
            self.deliver_layered(
                &mut |event, value, state| f.call(event, value, state),
                event,
                value,
            )?;
        }
        self.state.clear_event_data();
        Ok(())
    }

    /// Passes an event through the layers.
    #[inline(never)]
    fn deliver_layered(
        &mut self,
        f: &mut EventFn<'_>,
        event: Event<'_>,
        value: SerializeRef<'_>,
    ) -> Result<(), Error> {
        Next::new(&mut self.layers, &mut self.state, f, value).emit(event)
    }

    /// Drives the serialization, returns `false` if the callback paused it.
    #[inline(always)]
    fn drive_impl<C: Callback>(&mut self, mut f: C) -> Result<bool, Error> {
        // `next` might have been used before.
        self.detach_delivered_event_data();
        if let Some((held, true)) = self.needs_finish.take() {
            // SAFETY: the value is alive until the end of this block
            unsafe { held.get() }.finish(&mut self.state)?;
        }
        if let Some(value) = self.next_value.take() {
            self.drive_value(value, false, &mut f)?;
        }

        // at least one value is serialized per call
        let mut first = true;
        while let Some(frame) = self.stack.last_mut() {
            // the state is complete between the iterations, the driver can
            // continue from here in the next call
            if C::PAUSABLE {
                if !first && f.pause() {
                    return Ok(false);
                }
                first = false;
            }
            // SAFETY: values produced by the emitter borrow from it.  The
            // frame stays on the stack until all of them are dropped.
            let emitter = unsafe { &mut *(&mut frame.emitter as *mut Emitter) };
            let value = match emitter {
                Emitter::Forward => {
                    self.finish_forward()?;
                    continue;
                }
                Emitter::IndexedStruct(fields, index) => {
                    if C::FAST {
                        *index = fields.emit_plain_fields(
                            *index,
                            C::PAUSABLE,
                            &mut PlainDelivery {
                                driver: self,
                                f: &mut f,
                            },
                        )?;
                        if *index == FIELDS_END {
                            self.drive_end(&mut f)?;
                            continue;
                        }
                    }
                    let field = fields.field(*index);
                    *index += 1;
                    match field {
                        StructField::Field(key, value) => {
                            self.state.is_map_key = true;
                            self.deliver(
                                &mut f,
                                Event::Atom(Atom::Str(Text::borrowed(key))),
                                field_key(),
                            )?;
                            (value, false)
                        }
                        StructField::Skip => continue,
                        StructField::End => {
                            self.drive_end(&mut f)?;
                            continue;
                        }
                    }
                }
                Emitter::IndexedSeq(seq, index) => {
                    // large plain sequences are emitted in pieces
                    if C::FAST && C::PAUSABLE {
                        // the sequence might be a key, its values are not
                        self.state.is_map_key = false;
                        let next = seq.emit_plain_chunk(
                            *index,
                            PLAIN_BUDGET,
                            &mut PlainDelivery {
                                driver: self,
                                f: &mut f,
                            },
                        )?;
                        if next != *index {
                            *index = next;
                            continue;
                        }
                    }
                    let element = seq.element(*index, &mut self.state)?;
                    *index += 1;
                    match element {
                        Some(value) => (value, false),
                        None => {
                            self.drive_end(&mut f)?;
                            continue;
                        }
                    }
                }
                Emitter::Struct(emitter) => match emitter.next(&mut self.state)? {
                    Some((key, value)) => {
                        self.state.is_map_key = true;
                        self.deliver(&mut f, Event::Atom(Atom::Str(key.into())), field_key())?;
                        (value, false)
                    }
                    None => {
                        self.drive_end(&mut f)?;
                        continue;
                    }
                },
                Emitter::Seq(emitter) => match emitter.next(&mut self.state)? {
                    Some(value) => (value, false),
                    None => {
                        self.drive_end(&mut f)?;
                        continue;
                    }
                },
                Emitter::Map(emitter, is_value) => {
                    if *is_value {
                        *is_value = false;
                        (emitter.next_value(&mut self.state)?, false)
                    } else {
                        match emitter.next_key(&mut self.state)? {
                            Some(key) => {
                                *is_value = true;
                                (key, true)
                            }
                            None => {
                                self.drive_end(&mut f)?;
                                continue;
                            }
                        }
                    }
                }
            };
            // SAFETY: the value borrows from the emitter on the top of the
            // stack.
            self.drive_value(unsafe { Held::new(value.0) }, value.1, &mut f)?;
        }

        Ok(true)
    }

    /// Removes a forwarding frame from the top of the stack.
    ///
    /// This is invoked once the forwarded value was serialized.
    #[cold]
    fn finish_forward(&mut self) -> Result<(), Error> {
        let Frame {
            emitter,
            serializable,
            needs_finish,
        } = self.stack.pop().unwrap();
        debug_assert!(matches!(emitter, Emitter::Forward));
        if needs_finish {
            // SAFETY: the value is alive until the end of this block
            unsafe { serializable.get() }.finish(&mut self.state)?;
        }
        Ok(())
    }

    /// Places a value that forwarded to another value on the stack.
    ///
    /// Returns the forwarded value.
    #[cold]
    fn push_forward(
        &mut self,
        value: Held,
        needs_finish: bool,
        forwarded: SerializeHandle<'_>,
    ) -> Held {
        self.stack.push(Frame {
            emitter: Emitter::Forward,
            serializable: value,
            needs_finish,
        });
        // SAFETY: the forwarded value can borrow from the value which is
        // held by the frame.  It's dropped before the frame.
        unsafe { Held::new(forwarded) }
    }

    /// Serializes a forwarded value and emits its first event.
    #[inline(never)]
    fn drive_forwarded<C: Callback>(
        &mut self,
        value: Held,
        is_key: bool,
        f: &mut C,
    ) -> Result<(), Error> {
        self.drive_value(value, is_key, f)
    }

    /// Serializes a value and emits its first event.
    #[inline(always)]
    fn drive_value<C: Callback>(
        &mut self,
        value: Held,
        is_key: bool,
        f: &mut C,
    ) -> Result<(), Error> {
        // SAFETY: the value is held until the event and the emitters derived
        // from it are dropped.
        let serializable = unsafe { value.get() };
        self.state.is_map_key = is_key;
        let Begin {
            kind,
            shape,
            needs_finish,
        } = serializable.begin(&mut self.state)?;
        let kind = match kind {
            // callbacks that describe values need to see every value,
            // large plain values are driven on their own for callbacks that
            // pause
            BeginKind::Plain(plain)
                if C::UNBOUNDED || (C::FAST && plain.plain_cost(PLAIN_BUDGET).is_some()) =>
            {
                return plain.emit_plain(&mut PlainDelivery { driver: self, f });
            }
            BeginKind::Plain(plain) => serialize_plain(plain, &mut self.state)?,
            kind => kind,
        };
        let (emitter, event) = match kind {
            BeginKind::Emit(Emit::Atom(atom)) => {
                self.deliver(f, Event::Atom(atom), serializable)?;
                if needs_finish {
                    serializable.finish(&mut self.state)?;
                }
                return Ok(());
            }
            BeginKind::Emit(Emit::Struct(emitter)) => {
                (Emitter::Struct(emitter), Event::MapStart(shape))
            }
            BeginKind::Emit(Emit::Map(emitter)) => {
                (Emitter::Map(emitter, false), Event::MapStart(shape))
            }
            BeginKind::Emit(Emit::Seq(emitter)) => (Emitter::Seq(emitter), Event::SeqStart(shape)),
            // callbacks that describe values need to see every value
            BeginKind::Struct(fields) if C::FAST => {
                // an owned value is dropped here, not in the callee where
                // `fields` (which borrows from it) is an argument.
                let mut value = Some(value);
                let rv = self.drive_indexed_struct(&mut value, fields, shape, f);
                drop(value);
                return rv;
            }
            BeginKind::Struct(fields) => {
                (Emitter::IndexedStruct(fields, 0), Event::MapStart(shape))
            }
            // large sequences are emitted in pieces for callbacks that pause
            BeginKind::Seq(seq)
                if C::UNBOUNDED || (C::FAST && serializable.plain_cost(PLAIN_BUDGET).is_some()) =>
            {
                // see above
                let mut value = Some(value);
                let rv = self.drive_indexed_seq(&mut value, seq, shape, f);
                drop(value);
                return rv;
            }
            BeginKind::Seq(seq) => (Emitter::IndexedSeq(seq, 0), Event::SeqStart(shape)),
            BeginKind::Emit(Emit::Forward(forwarded)) => {
                let forwarded = self.push_forward(value, needs_finish, forwarded);
                return self.drive_forwarded(forwarded, is_key, f);
            }
            BeginKind::Plain(_) => unreachable!(),
        };
        self.stack.push(Frame {
            emitter,
            serializable: value,
            needs_finish,
        });
        self.state.depth += 1;
        self.deliver(f, event, serializable)
    }

    /// Starts an indexed struct.
    ///
    /// Its leading plain fields are emitted right away.  If all fields are
    /// plain the struct is ended, otherwise it's placed on the stack.
    ///
    /// The value is taken if it's placed on the stack, otherwise the caller
    /// drops it.  An owned value must not be dropped in here as `fields`
    /// borrows from it (it's an argument, which must stay valid for the
    /// whole call).
    #[inline(always)]
    fn drive_indexed_struct<C: Callback>(
        &mut self,
        value: &mut Option<Held>,
        fields: &'static dyn IndexedStruct,
        shape: ContainerShape,
        f: &mut C,
    ) -> Result<(), Error> {
        // SAFETY: the value is held by the caller or by the frame
        let serializable = unsafe { value.as_ref().unwrap().get() };
        self.state.depth += 1;
        self.deliver(f, Event::MapStart(shape), serializable)?;
        self.state.is_map_key = false;
        let index =
            fields.emit_plain_fields(0, C::PAUSABLE, &mut PlainDelivery { driver: self, f })?;
        if index == FIELDS_END {
            self.state.depth -= 1;
            self.deliver(f, Event::MapEnd, serializable)
        } else {
            self.stack.push(Frame {
                emitter: Emitter::IndexedStruct(fields, index),
                serializable: value.take().unwrap(),
                // indexed structs do not need `finish`
                needs_finish: false,
            });
            Ok(())
        }
    }

    /// Starts an indexed sequence.
    ///
    /// If its elements are plain, they are emitted right away and the
    /// sequence is ended.  Otherwise it's placed on the stack.
    ///
    /// The value is taken if it's placed on the stack, otherwise the caller
    /// drops it.  An owned value must not be dropped in here as `seq`
    /// borrows from it (it's an argument, which must stay valid for the
    /// whole call).
    #[inline(always)]
    fn drive_indexed_seq<C: Callback>(
        &mut self,
        value: &mut Option<Held>,
        seq: &'static dyn IndexedSeq,
        shape: ContainerShape,
        f: &mut C,
    ) -> Result<(), Error> {
        // SAFETY: the value is held by the caller or by the frame
        let serializable = unsafe { value.as_ref().unwrap().get() };
        self.state.depth += 1;
        self.deliver(f, Event::SeqStart(shape), serializable)?;
        self.state.is_map_key = false;
        let emitted = seq.emit_plain(&mut PlainDelivery { driver: self, f })?;
        if emitted {
            self.state.depth -= 1;
            self.deliver(f, Event::SeqEnd, serializable)
        } else {
            self.stack.push(Frame {
                emitter: Emitter::IndexedSeq(seq, 0),
                serializable: value.take().unwrap(),
                // indexed sequences do not need `finish`
                needs_finish: false,
            });
            Ok(())
        }
    }

    /// Ends the container on the top of the stack and emits the end event.
    ///
    /// Unlike with `next` the value does not need to outlive this call.
    #[inline]
    fn drive_end<C: Callback>(&mut self, f: &mut C) -> Result<(), Error> {
        let Frame {
            emitter,
            serializable,
            needs_finish,
        } = self.stack.pop().unwrap();
        let event = match emitter {
            Emitter::Seq(_) | Emitter::IndexedSeq(..) => Event::SeqEnd,
            _ => Event::MapEnd,
        };
        // the emitter borrows from the serializable, drop it first.
        emitter.release(&mut self.state);
        self.state.depth -= 1;
        // SAFETY: the value is held until the end of this function
        let value = unsafe { serializable.get() };
        self.deliver(f, event, value)?;
        if needs_finish {
            value.finish(&mut self.state)?;
        }
        Ok(())
    }

    /// Advances the driver.
    ///
    /// The returned event is bound to `'static` but it actually borrows from
    /// the values and frames held by the driver.  It's only valid until the
    /// next call.
    fn advance(&mut self) -> Result<NextEvent<'static>, Error> {
        self.detach_delivered_event_data();
        if let Some((held, true)) = self.needs_finish.take() {
            // SAFETY: the value is alive until the end of this block
            unsafe { held.get() }.finish(&mut self.state)?;
        }

        let mut is_key = false;
        let value = match self.next_value.take() {
            Some(value) => value,
            None => loop {
                let frame = match self.stack.last_mut() {
                    Some(frame) => frame,
                    None => return Ok(None),
                };
                // SAFETY: values produced by the emitter borrow from it.  The
                // frame stays on the stack until all of them are dropped.
                let emitter = unsafe { &mut *(&mut frame.emitter as *mut Emitter) };
                let next = match emitter {
                    Emitter::Forward => {
                        self.finish_forward()?;
                        continue;
                    }
                    Emitter::Seq(emitter) => emitter.next(&mut self.state)?,
                    Emitter::Map(emitter, is_value) => {
                        if *is_value {
                            *is_value = false;
                            Some(emitter.next_value(&mut self.state)?)
                        } else {
                            let key = emitter.next_key(&mut self.state)?;
                            *is_value = key.is_some();
                            is_key = true;
                            key
                        }
                    }
                    Emitter::Struct(emitter) => match emitter.next(&mut self.state)? {
                        Some((key, value)) => {
                            // the key is emitted directly as event, the value
                            // is serialized on the next call.
                            // SAFETY: the value and key borrow from the emitter
                            // which stays alive until the next call.
                            self.next_value = Some(unsafe { Held::new(value) });
                            let key = unsafe {
                                core::mem::transmute::<Cow<'_, str>, Cow<'static, str>>(key)
                            };
                            self.state.is_map_key = true;
                            return Ok(Some((Event::Atom(Atom::Str(key.into())), field_key())));
                        }
                        None => None,
                    },
                    Emitter::IndexedSeq(seq, index) => {
                        let rv = seq.element(*index, &mut self.state)?;
                        *index += 1;
                        rv
                    }
                    Emitter::IndexedStruct(fields, index) => loop {
                        let field = fields.field(*index);
                        *index += 1;
                        match field {
                            StructField::Field(key, value) => {
                                // SAFETY: the value and key borrow from the
                                // serializable of the frame.
                                self.next_value = Some(unsafe { Held::new(value) });
                                self.state.is_map_key = true;
                                return Ok(Some((
                                    Event::Atom(Atom::Str(Text::borrowed(key))),
                                    field_key(),
                                )));
                            }
                            StructField::Skip => continue,
                            StructField::End => break None,
                        }
                    },
                };
                match next {
                    // SAFETY: the value borrows from the emitter on the top
                    // of the stack.
                    Some(value) => break unsafe { Held::new(value) },
                    None => {
                        let event = self.end_container();
                        // SAFETY: the value is held until the next call
                        return Ok(Some((event, unsafe { self.finished_value() })));
                    }
                }
            },
        };

        self.serialize_value(value, is_key)
    }

    /// Ends the container on the top of the stack.
    fn end_container(&mut self) -> Event<'static> {
        let Frame {
            emitter,
            serializable,
            needs_finish,
        } = self.stack.pop().unwrap();
        let event = match emitter {
            Emitter::Seq(_) | Emitter::IndexedSeq(..) => Event::SeqEnd,
            _ => Event::MapEnd,
        };
        // the emitter borrows from the serializable, drop it first.
        emitter.release(&mut self.state);
        self.needs_finish = Some((serializable, needs_finish));
        self.state.depth -= 1;
        event
    }

    /// Returns the value of the container that was ended last.
    ///
    /// # Safety
    ///
    /// The value must not be used after the next event.
    #[inline(always)]
    unsafe fn finished_value(&self) -> SerializeRef<'static> {
        match self.needs_finish {
            // SAFETY: the caller guarantees the value is not used for longer
            Some((ref held, _)) => unsafe { held.get() },
            None => field_key(),
        }
    }

    /// Serializes a value and returns its first event.
    #[inline]
    fn serialize_value(&mut self, value: Held, is_key: bool) -> Result<NextEvent<'static>, Error> {
        // SAFETY: the value is held by the driver until the event and the
        // emitters derived from it are dropped.  Moving `value` only moves a
        // pointer to it.
        let serializable = unsafe { value.get() };
        self.state.is_map_key = is_key;
        let Begin {
            kind,
            shape,
            needs_finish,
        } = serializable.begin(&mut self.state)?;
        let kind = match kind {
            BeginKind::Plain(plain) => serialize_plain(plain, &mut self.state)?,
            kind => kind,
        };
        let (emitter, event) = match kind {
            BeginKind::Emit(Emit::Atom(atom)) => {
                self.needs_finish = Some((value, needs_finish));
                return Ok(Some((Event::Atom(atom), serializable)));
            }
            BeginKind::Emit(Emit::Struct(emitter)) => {
                (Emitter::Struct(emitter), Event::MapStart(shape))
            }
            BeginKind::Emit(Emit::Map(emitter)) => {
                (Emitter::Map(emitter, false), Event::MapStart(shape))
            }
            BeginKind::Emit(Emit::Seq(emitter)) => (Emitter::Seq(emitter), Event::SeqStart(shape)),
            BeginKind::Struct(fields) => {
                (Emitter::IndexedStruct(fields, 0), Event::MapStart(shape))
            }
            BeginKind::Seq(seq) => (Emitter::IndexedSeq(seq, 0), Event::SeqStart(shape)),
            BeginKind::Emit(Emit::Forward(forwarded)) => {
                let forwarded = self.push_forward(value, needs_finish, forwarded);
                return self.serialize_forwarded(forwarded, is_key);
            }
            BeginKind::Plain(_) => unreachable!(),
        };
        self.stack.push(Frame {
            emitter,
            serializable: value,
            needs_finish,
        });
        self.state.depth += 1;
        Ok(Some((event, serializable)))
    }

    /// Serializes a forwarded value and returns its first event.
    #[inline(never)]
    fn serialize_forwarded(
        &mut self,
        value: Held,
        is_key: bool,
    ) -> Result<NextEvent<'static>, Error> {
        self.serialize_value(value, is_key)
    }
}

#[test]
fn test_seq_emitting() {
    let vec = vec![vec![1u64, 2], vec![3, 4]];

    let mut driver = SerializeDriver::new(&vec);
    let mut events = Vec::new();
    while let Some((event, _, _)) = driver.next().unwrap() {
        events.push(crate::event::without_len(event.to_static()));
    }

    assert_eq!(
        events,
        vec![
            Event::seq_start(),
            Event::seq_start(),
            1u64.into(),
            2u64.into(),
            Event::SeqEnd,
            Event::seq_start(),
            3u64.into(),
            4u64.into(),
            Event::SeqEnd,
            Event::SeqEnd,
        ],
    );
}

#[test]
fn test_map_emitting() {
    let mut map = alloc::collections::BTreeMap::new();
    map.insert((1u32, 2u32), "first");
    map.insert((2, 3), "second");

    let mut driver = SerializeDriver::new(&map);
    let mut events = Vec::new();
    while let Some((event, _, _)) = driver.next().unwrap() {
        events.push(crate::event::without_len(event.to_static()));
    }

    assert_eq!(
        events,
        vec![
            Event::MapStart(crate::ContainerShape::with_order(crate::Order::Sorted)),
            Event::seq_start(),
            1u64.into(),
            2u64.into(),
            Event::SeqEnd,
            "first".into(),
            Event::seq_start(),
            2u64.into(),
            3u64.into(),
            Event::SeqEnd,
            "second".into(),
            Event::MapEnd
        ]
    );
}

#[test]
fn test_state_mut() {
    #[derive(Debug, Default)]
    struct Uppercase(bool);

    struct Name(&'static str);

    impl Serialize for Name {
        fn serialize<'a>(value: &'a Self, state: &mut State) -> Result<Emit<'a>, Error> {
            Ok(Emit::Atom(Atom::Str(
                if state.get::<Uppercase>().is_some_and(|x| x.0) {
                    value.0.to_uppercase().into()
                } else {
                    value.0.into()
                },
            )))
        }
    }

    let names = vec![Name("foo"), Name("bar")];
    let mut driver = SerializeDriver::new(&names);
    driver.state_mut().get_mut::<Uppercase>().0 = true;
    let mut events = Vec::new();
    while let Some((event, _, _)) = driver.next().unwrap() {
        events.push(crate::event::without_len(event.to_static()));
    }

    assert_eq!(
        events,
        vec![
            Event::seq_start(),
            "FOO".into(),
            "BAR".into(),
            Event::SeqEnd
        ],
    );
}