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
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::any::{Any, TypeId, type_name};
use core::fmt::{self, Debug};

#[derive(Copy, Clone)]
pub(crate) struct TypeKey(pub(crate) TypeId, &'static str);

impl TypeKey {
    pub(crate) fn of<T: 'static>() -> TypeKey {
        TypeKey(TypeId::of::<T>(), type_name::<T>())
    }
}

impl PartialEq for TypeKey {
    fn eq(&self, other: &Self) -> bool {
        self.0 == other.0
    }
}

impl Eq for TypeKey {}

impl Debug for TypeKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.1)
    }
}

pub(crate) trait DebugAny: Any + Debug + Send + Sync {
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;
    fn into_any(self: Box<Self>) -> Box<dyn Any>;
}

impl<T: Any + Debug + Send + Sync + 'static> DebugAny for T {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn into_any(self: Box<Self>) -> Box<dyn Any> {
        self
    }
}

/// The values of event data are also `Sync` so that captured event data
/// can be shared between threads (see [`EventData`]).
trait EventAny: DebugAny + Sync {}

impl<T: DebugAny + Sync> EventAny for T {}

// The values are stored as `Box<dyn DebugAny>` (or `Arc`), which is a
// `DebugAny` itself.  Coercing a reference to the box (rather than to the
// value in it) into a `&dyn DebugAny` compiles and would cast the box.
// The casts are methods of the trait objects so that they are called on
// the value: the boxes do not have them, they are reached by auto-deref.
// The type is checked in debug builds.

impl dyn DebugAny {
    /// Returns the value as `T`.
    ///
    /// # Safety
    ///
    /// The value must be of type `T`.
    #[inline(always)]
    pub(crate) unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T {
        debug_assert!(self.as_any().is::<T>());
        // SAFETY: see above
        unsafe { &*(self as *const dyn DebugAny).cast::<T>() }
    }

    /// Returns the value as `T` mutably.
    ///
    /// # Safety
    ///
    /// The value must be of type `T`.
    #[inline(always)]
    pub(crate) unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T {
        debug_assert!(self.as_any_mut().is::<T>());
        // SAFETY: see above
        unsafe { &mut *(self as *mut dyn DebugAny).cast::<T>() }
    }
}

impl dyn EventAny {
    /// Returns the value as `T`.
    ///
    /// # Safety
    ///
    /// The value must be of type `T`.
    #[inline(always)]
    unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T {
        debug_assert!(self.as_any().is::<T>());
        // SAFETY: see above
        unsafe { &*(self as *const dyn EventAny).cast::<T>() }
    }

    /// Returns the value as `T` mutably.
    ///
    /// # Safety
    ///
    /// The value must be of type `T`.
    #[inline(always)]
    unsafe fn downcast_mut_unchecked<T: Any>(&mut self) -> &mut T {
        debug_assert!(self.as_any_mut().is::<T>());
        // SAFETY: see above
        unsafe { &mut *(self as *mut dyn EventAny).cast::<T>() }
    }
}

/// Functions to clone values of a type behind a `dyn DebugAny`.
#[derive(Copy, Clone)]
struct CloneFns {
    clone: fn(&dyn DebugAny) -> Box<dyn DebugAny>,
    clone_into: fn(&mut dyn DebugAny, &dyn DebugAny),
}

impl CloneFns {
    fn of<T: Clone + Debug + Send + Sync + 'static>() -> CloneFns {
        CloneFns {
            clone: |value| Box::new(value.as_any().downcast_ref::<T>().unwrap().clone()),
            clone_into: |target, value| {
                target
                    .as_any_mut()
                    .downcast_mut::<T>()
                    .unwrap()
                    .clone_from(value.as_any().downcast_ref::<T>().unwrap())
            },
        }
    }
}

/// Functions to clone event data behind a `dyn EventAny`.
#[derive(Copy, Clone)]
struct EventFns {
    clone: fn(&dyn EventAny) -> Box<dyn EventAny>,
    clone_into: fn(&mut dyn EventAny, &dyn EventAny),
}

impl EventFns {
    fn of<T: Clone + Debug + Send + Sync + 'static>() -> EventFns {
        EventFns {
            clone: |value| Box::new(value.as_any().downcast_ref::<T>().unwrap().clone()),
            clone_into: |target, value| {
                target
                    .as_any_mut()
                    .downcast_mut::<T>()
                    .unwrap()
                    .clone_from(value.as_any().downcast_ref::<T>().unwrap())
            },
        }
    }
}

/// A value attached to the current event.
struct EventEntry {
    key: TypeKey,
    // values are retained when they are deactivated so that their memory
    // can be reused for the next event.
    active: bool,
    value: Box<dyn EventAny>,
    fns: EventFns,
}

/// Typed values stored in a state.
///
/// There are typically only a handful of extension types in a state which
/// is why they are held in a vector and looked up linearly.  This is much
/// faster than a hash map for small numbers of entries.
#[derive(Default)]
pub(crate) struct Extensions {
    // Invariant: the value of an entry is always of the type of its key.
    entries: Vec<(TypeKey, Box<dyn DebugAny>)>,
    replayable: Vec<(TypeKey, CloneFns)>,
    // Invariant: the value of an entry is always of the type of its key.
    events: Vec<EventEntry>,
    // `true` if any of the event entries is active
    has_event_data: bool,
}

impl Debug for Extensions {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map()
            .entries(self.entries.iter().map(|(key, value)| (key, value)))
            .entries(
                self.events
                    .iter()
                    .filter(|entry| entry.active)
                    .map(|entry| (entry.key, &entry.value)),
            )
            .finish()
    }
}

impl Extensions {
    #[inline]
    fn position(&self, key: TypeId) -> Option<usize> {
        self.entries.iter().position(|(k, _)| k.0 == key)
    }

    #[inline]
    pub(crate) fn get<T: Debug + Send + Sync + 'static>(&self) -> Option<&T> {
        let index = self.position(TypeId::of::<T>())?;
        // SAFETY: values are always stored with the key of their type
        Some(unsafe { self.entries[index].1.downcast_ref_unchecked::<T>() })
    }

    #[inline]
    pub(crate) fn get_mut<T: Default + Debug + Send + Sync + 'static>(&mut self) -> &mut T {
        let index = match self.position(TypeId::of::<T>()) {
            Some(index) => index,
            None => self.insert_default::<T>(),
        };
        // SAFETY: values are always stored with the key of their type
        unsafe { self.entries[index].1.downcast_mut_unchecked::<T>() }
    }

    #[cold]
    fn insert_default<T: Default + Debug + Send + Sync + 'static>(&mut self) -> usize {
        self.entries
            .push((TypeKey::of::<T>(), Box::new(T::default())));
        self.entries.len() - 1
    }

    /// Marks an extension type as replayable.
    pub(crate) fn set_replayable<T: Clone + Debug + Send + Sync + 'static>(&mut self) {
        let key = TypeKey::of::<T>();
        if !self.replayable.iter().any(|(k, _)| *k == key) {
            self.replayable.push((key, CloneFns::of::<T>()));
        }
    }

    /// Returns `true` if data is attached to the current event.
    #[cfg(test)]
    pub fn has_event_data(&self) -> bool {
        self.has_event_data
    }

    #[inline]
    fn event_position(&self, key: TypeId) -> Option<usize> {
        self.events.iter().position(|entry| entry.key.0 == key)
    }

    /// Returns the data of a type attached to the current event.
    ///
    /// Most events have no data.  The check for that is always inlined, so
    /// that looking up event data is cheap for them.
    #[inline(always)]
    pub(crate) fn event<T: Debug + Send + Sync + 'static>(&self) -> Option<&T> {
        if !self.has_event_data {
            return None;
        }
        self.find_event()
    }

    #[inline]
    fn find_event<T: Debug + Send + Sync + 'static>(&self) -> Option<&T> {
        let entry = &self.events[self.event_position(TypeId::of::<T>())?];
        if !entry.active {
            return None;
        }
        // SAFETY: values are always stored with the key of their type
        Some(unsafe { entry.value.downcast_ref_unchecked::<T>() })
    }

    /// Returns the data of a type attached to the current event mutably.
    ///
    /// If no such data is attached yet, the default value is attached.
    #[inline]
    pub(crate) fn event_mut<T: Default + Clone + Debug + Send + Sync + 'static>(
        &mut self,
    ) -> &mut T {
        let index = match self.event_position(TypeId::of::<T>()) {
            Some(index) => index,
            None => self.insert_event::<T>(),
        };
        let entry = &mut self.events[index];
        // SAFETY: values are always stored with the key of their type
        let value = unsafe { entry.value.downcast_mut_unchecked::<T>() };
        if !entry.active {
            // the value of a previous event is reset in place which retains
            // the memory of collections
            value.clone_from(&T::default());
            entry.active = true;
            self.has_event_data = true;
        }
        value
    }

    /// Takes the data of a type from the current event.
    ///
    /// The data is detached from the event.
    pub(crate) fn take_event<T: Default + Debug + Send + Sync + 'static>(&mut self) -> Option<T> {
        if !self.has_event_data {
            return None;
        }
        let index = self.event_position(TypeId::of::<T>())?;
        let entry = &mut self.events[index];
        if !entry.active {
            return None;
        }
        // `as_any_mut` of the box itself would be the box
        let value = core::mem::take((*entry.value).as_any_mut().downcast_mut::<T>()?);
        entry.active = false;
        self.has_event_data = self.events.iter().any(|entry| entry.active);
        Some(value)
    }

    #[cold]
    fn insert_event<T: Default + Clone + Debug + Send + Sync + 'static>(&mut self) -> usize {
        self.events.push(EventEntry {
            key: TypeKey::of::<T>(),
            active: false,
            value: Box::new(T::default()),
            fns: EventFns::of::<T>(),
        });
        self.events.len() - 1
    }

    /// Detaches all data from the current event.
    #[inline(always)]
    pub(crate) fn clear_event_data(&mut self) {
        if self.has_event_data {
            self.deactivate_events();
        }
    }

    #[inline(never)]
    fn deactivate_events(&mut self) {
        for entry in self.events.iter_mut() {
            entry.active = false;
        }
        self.has_event_data = false;
    }

    /// Captures the values of the replayable extensions and the event data.
    ///
    /// Returns `None` if there is nothing to capture.  Most of the time
    /// there is neither, this is cheap to check.
    #[inline]
    pub(crate) fn snapshot_if_any(&self) -> Option<Snapshot> {
        if self.replayable.is_empty() && !self.has_event_data {
            return None;
        }
        Some(self.snapshot()).filter(|snapshot| !snapshot.is_empty())
    }

    /// Captures the values of the replayable extensions and the event data.
    pub(crate) fn snapshot(&self) -> Snapshot {
        let mut snapshot = Snapshot::default();
        if !self.replayable.is_empty() {
            snapshot.replayable = self
                .replayable
                .iter()
                .filter_map(|&(key, fns)| {
                    self.position(key.0)
                        .map(|index| (key, (fns.clone)(&*self.entries[index].1), fns))
                })
                .collect();
        }
        snapshot.events = self.capture_event_data();
        snapshot
    }

    /// Restores the values from a snapshot.
    ///
    /// The event data is replaced by the event data of the snapshot.
    pub(crate) fn restore(&mut self, snapshot: &Snapshot) {
        // the clone functions belong to the type of the key
        for (key, value, fns) in snapshot.replayable.iter() {
            match self.position(key.0) {
                Some(index) => (fns.clone_into)(&mut *self.entries[index].1, &**value),
                None => self.entries.push((*key, (fns.clone)(&**value))),
            }
        }
        self.restore_event_data(&snapshot.events);
    }

    /// Captures the data attached to the current event.
    pub(crate) fn capture_event_data(&self) -> EventData {
        if !self.has_event_data {
            return EventData::default();
        }
        EventData {
            entries: self
                .events
                .iter()
                .filter(|entry| entry.active)
                .map(|entry| EventDataEntry {
                    key: entry.key,
                    value: (entry.fns.clone)(&*entry.value),
                    fns: entry.fns,
                })
                .collect(),
        }
    }

    /// Replaces the data attached to the current event.
    pub(crate) fn restore_event_data(&mut self, data: &EventData) {
        self.clear_event_data();
        self.attach_event_data(data);
    }

    /// Attaches data to the current event.
    ///
    /// Data of the same types that is already attached is replaced, other
    /// data is retained.
    pub(crate) fn attach_event_data(&mut self, data: &EventData) {
        for entry in data.entries.iter() {
            match self.event_position(entry.key.0) {
                Some(index) => {
                    let target = &mut self.events[index];
                    (entry.fns.clone_into)(&mut *target.value, &*entry.value);
                    target.active = true;
                }
                None => self.events.push(EventEntry {
                    key: entry.key,
                    active: true,
                    value: (entry.fns.clone)(&*entry.value),
                    fns: entry.fns,
                }),
            }
            self.has_event_data = true;
        }
    }
}

/// The captured values of the replayable extensions and the event data.
#[derive(Default)]
pub(crate) struct Snapshot {
    replayable: Vec<(TypeKey, Box<dyn DebugAny>, CloneFns)>,
    events: EventData,
}

impl Snapshot {
    /// Returns `true` if nothing was captured.
    pub(crate) fn is_empty(&self) -> bool {
        self.replayable.is_empty() && self.events.is_empty()
    }

    /// Returns the captured event data.
    pub(crate) fn event_data(&self) -> &EventData {
        &self.events
    }
}

impl Clone for Snapshot {
    fn clone(&self) -> Snapshot {
        Snapshot {
            replayable: self
                .replayable
                .iter()
                .map(|(key, value, fns)| (*key, (fns.clone)(&**value), *fns))
                .collect(),
            events: self.events.clone(),
        }
    }
}

impl Debug for Snapshot {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map()
            .entries(self.replayable.iter().map(|(key, value, _)| (key, value)))
            .entries(
                self.events
                    .entries
                    .iter()
                    .map(|entry| (entry.key, &entry.value)),
            )
            .finish()
    }
}

/// Data attached to an event, detached from the event.
///
/// Formats and values attach data to individual events (see
/// [`State::event`](crate::State::event)), for instance tags or formatting
/// hints.  This captures such data so that it can be attached to an event
/// again later.  This is used by types which hold on to values outside of a
/// serialization or deserialization, such as the values of the
/// `deser-value` crate, so that data like CBOR tags survives.
///
/// ```
/// use deser::de::DeserializeDriver;
/// use deser::EventData;
///
/// #[derive(Debug, Default, Clone, PartialEq)]
/// struct Tag(u64);
///
/// let mut data = EventData::new();
/// data.insert(Tag(42));
/// assert_eq!(data.get::<Tag>(), Some(&Tag(42)));
///
/// let mut out = None::<bool>;
/// let mut driver = DeserializeDriver::new(&mut out);
/// driver.state_mut().attach_event_data(&data);
/// assert_eq!(driver.state().event::<Tag>(), Some(&Tag(42)));
/// assert_eq!(
///     driver.state().capture_event_data().get::<Tag>(),
///     Some(&Tag(42))
/// );
/// ```
///
/// Event data has to be [`Send`] and [`Sync`], which means that captured
/// event data is too.
#[derive(Default)]
pub struct EventData {
    // Invariant: the value of an entry is always of the type of its key and
    // there is at most one entry per key.
    entries: Vec<EventDataEntry>,
}

struct EventDataEntry {
    key: TypeKey,
    value: Box<dyn EventAny>,
    fns: EventFns,
}

impl EventData {
    /// Creates empty event data.
    pub const fn new() -> EventData {
        EventData {
            entries: Vec::new(),
        }
    }

    /// Returns `true` if no data is held.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    fn position(&self, key: TypeId) -> Option<usize> {
        self.entries.iter().position(|entry| entry.key.0 == key)
    }

    /// Returns the data of a type.
    pub fn get<T: Debug + Send + 'static>(&self) -> Option<&T> {
        let entry = &self.entries[self.position(TypeId::of::<T>())?];
        // SAFETY: values are always stored with the key of their type
        Some(unsafe { entry.value.downcast_ref_unchecked::<T>() })
    }

    /// Returns the data of a type mutably.
    ///
    /// If there is no data of this type, the default value is inserted.
    pub fn get_mut<T: Default + Clone + Debug + Send + Sync + 'static>(&mut self) -> &mut T {
        let index = match self.position(TypeId::of::<T>()) {
            Some(index) => index,
            None => {
                self.entries.push(EventDataEntry {
                    key: TypeKey::of::<T>(),
                    value: Box::new(T::default()),
                    fns: EventFns::of::<T>(),
                });
                self.entries.len() - 1
            }
        };
        let entry = &mut self.entries[index];
        // SAFETY: values are always stored with the key of their type
        unsafe { entry.value.downcast_mut_unchecked::<T>() }
    }

    /// Inserts data, replacing data of the same type.
    pub fn insert<T: Clone + Debug + Send + Sync + 'static>(&mut self, value: T) {
        let entry = EventDataEntry {
            key: TypeKey::of::<T>(),
            value: Box::new(value),
            fns: EventFns::of::<T>(),
        };
        match self.position(TypeId::of::<T>()) {
            Some(index) => self.entries[index] = entry,
            None => self.entries.push(entry),
        }
    }

    /// Removes the data of a type and returns it.
    pub fn remove<T: Debug + Send + 'static>(&mut self) -> Option<T> {
        let entry = self.entries.remove(self.position(TypeId::of::<T>())?);
        entry
            .value
            .into_any()
            .downcast::<T>()
            .ok()
            .map(|value| *value)
    }
}

impl Clone for EventData {
    fn clone(&self) -> EventData {
        EventData {
            entries: self
                .entries
                .iter()
                .map(|entry| EventDataEntry {
                    key: entry.key,
                    value: (entry.fns.clone)(&*entry.value),
                    fns: entry.fns,
                })
                .collect(),
        }
    }
}

impl Debug for EventData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map()
            .entries(self.entries.iter().map(|entry| (entry.key, &entry.value)))
            .finish()
    }
}

#[test]
fn test_extensions() {
    #[derive(Debug, Default, Clone, PartialEq)]
    struct A(u32);
    #[derive(Debug, Default, Clone, PartialEq)]
    struct B(String);
    #[derive(Debug, Default, Clone, PartialEq)]
    struct C(u8);

    let mut ext = Extensions::default();
    assert_eq!(ext.get::<A>(), None);
    ext.get_mut::<A>().0 = 42;
    ext.get_mut::<B>().0.push_str("hello");
    ext.get_mut::<C>().0 = 1;
    assert_eq!(ext.get::<A>(), Some(&A(42)));
    assert_eq!(ext.get::<B>(), Some(&B("hello".into())));
    assert_eq!(ext.get::<C>(), Some(&C(1)));

    ext.set_replayable::<A>();
    ext.set_replayable::<B>();
    ext.set_replayable::<A>();
    let snapshot = ext.snapshot();
    ext.get_mut::<A>().0 = 1;
    ext.get_mut::<B>().0.clear();
    ext.get_mut::<C>().0 = 2;
    ext.restore(&snapshot);
    assert_eq!(ext.get::<A>(), Some(&A(42)));
    assert_eq!(ext.get::<B>(), Some(&B("hello".into())));
    assert_eq!(ext.get::<C>(), Some(&C(2)));

    // restoring into empty extensions inserts the values
    let mut other = Extensions::default();
    other.restore(&snapshot.clone());
    assert_eq!(other.get::<A>(), Some(&A(42)));
    assert_eq!(other.get::<C>(), None);
    assert_eq!(
        format!("{:?}", other),
        format!(
            "{{{}: A(42), {}: B(\"hello\")}}",
            core::any::type_name::<A>(),
            core::any::type_name::<B>()
        )
    );
}

#[test]
fn test_event_data() {
    #[derive(Debug, Default, PartialEq)]
    struct Tags(Vec<u64>);

    // derived clones do not forward `clone_from`
    impl Clone for Tags {
        fn clone(&self) -> Tags {
            Tags(self.0.clone())
        }

        fn clone_from(&mut self, source: &Tags) {
            self.0.clone_from(&source.0);
        }
    }
    #[derive(Debug, Default, Clone, PartialEq)]
    struct Span(usize, usize);

    let mut ext = Extensions::default();
    assert!(!ext.has_event_data());
    assert_eq!(ext.event::<Tags>(), None);

    ext.event_mut::<Tags>().0.extend([1, 2]);
    assert!(ext.has_event_data());
    assert_eq!(ext.event::<Tags>(), Some(&Tags(vec![1, 2])));
    assert_eq!(ext.event::<Span>(), None);

    // clearing detaches the data but retains the memory
    ext.clear_event_data();
    assert!(!ext.has_event_data());
    assert_eq!(ext.event::<Tags>(), None);
    let tags = ext.event_mut::<Tags>();
    assert_eq!(tags, &Tags(vec![]));
    assert!(tags.0.capacity() >= 2);
    tags.0.push(3);
    *ext.event_mut::<Span>() = Span(1, 2);

    // snapshots capture only the attached data
    let snapshot = ext.snapshot();
    ext.clear_event_data();
    *ext.event_mut::<Span>() = Span(3, 4);
    ext.restore(&snapshot);
    assert_eq!(ext.event::<Tags>(), Some(&Tags(vec![3])));
    assert_eq!(ext.event::<Span>(), Some(&Span(1, 2)));

    ext.clear_event_data();
    ext.event_mut::<Span>();
    let empty = ext.snapshot();
    *ext.event_mut::<Tags>() = Tags(vec![4]);
    ext.restore(&empty);
    assert_eq!(ext.event::<Tags>(), None);
    assert_eq!(ext.event::<Span>(), Some(&Span(0, 0)));

    // taking detaches the data of one type, it's not captured anymore
    *ext.event_mut::<Tags>() = Tags(vec![5]);
    assert_eq!(ext.take_event::<Tags>(), Some(Tags(vec![5])));
    assert_eq!(ext.take_event::<Tags>(), None);
    assert_eq!(ext.event::<Tags>(), None);
    assert_eq!(ext.event::<Span>(), Some(&Span(0, 0)));
    assert!(ext.capture_event_data().entries.len() == 1);
    assert_eq!(ext.take_event::<Span>(), Some(Span(0, 0)));
    assert!(!ext.has_event_data());
    assert!(ext.capture_event_data().is_empty());
}

#[test]
fn test_downcast_through_box() {
    use alloc::sync::Arc;

    // the boxes are values of the traits too: auto-deref has to reach the
    // value in them
    let boxed: Box<dyn DebugAny> = Box::new(42u32);
    // SAFETY: the value is a `u32`
    assert_eq!(unsafe { *boxed.downcast_ref_unchecked::<u32>() }, 42);
    let arc: Arc<dyn DebugAny> = Arc::new(42u32);
    // SAFETY: the value is a `u32`
    assert_eq!(unsafe { *arc.downcast_ref_unchecked::<u32>() }, 42);
    let mut boxed: Box<dyn EventAny> = Box::new(1u32);
    // SAFETY: the value is a `u32`
    unsafe { *boxed.downcast_mut_unchecked::<u32>() += 1 };
    // SAFETY: the value is a `u32`
    assert_eq!(unsafe { *boxed.downcast_ref_unchecked::<u32>() }, 2);
}