sky_ecs 0.1.3

High-performance typed chunk-based ECS for Rust
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
#![deny(unsafe_op_in_unsafe_fn)]

use super::erased_value::InsertValue;
use super::{Bundle, EntityId, World};
use crate::ecs::{component_type, ComponentType};
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use std::any::Any;
use std::mem;

// ---------------------------------------------------------------------------
// Deferred command trait – closures applied to &mut World
// ---------------------------------------------------------------------------

trait DeferredWorldCommand {
    fn apply(self: Box<Self>, world: &mut World);
}

struct FnDeferredCommand<F>(F);

impl<F> DeferredWorldCommand for FnDeferredCommand<F>
where
    F: FnOnce(&mut World) + 'static,
{
    fn apply(self: Box<Self>, world: &mut World) {
        (self.0)(world);
    }
}

// ---------------------------------------------------------------------------
// Spawn-batch coalescing
// ---------------------------------------------------------------------------

trait SpawnBatchCommand {
    fn apply(&mut self, world: &mut World);
    fn clear(&mut self);
    fn as_any_mut(&mut self) -> &mut dyn Any;
}

struct TypedSpawnBatch<B> {
    bundles: Vec<B>,
}

impl<B> SpawnBatchCommand for TypedSpawnBatch<B>
where
    B: Bundle,
{
    fn apply(&mut self, world: &mut World) {
        world.spawn_batch(self.bundles.drain(..));
    }

    fn clear(&mut self) {
        self.bundles.clear();
    }

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

// ---------------------------------------------------------------------------
// Command queue
// ---------------------------------------------------------------------------

enum Command {
    Vacant,
    EntityBatch(PendingEntityBuffer),
    SpawnBatch(Box<dyn SpawnBatchCommand>),
    Deferred(Box<dyn DeferredWorldCommand>),
}

impl Command {
    fn apply(self, world: &mut World) -> Self {
        match self {
            Self::Vacant => Self::Vacant,
            Self::EntityBatch(mut batch) => {
                batch.flush(world);
                Self::EntityBatch(batch)
            }
            Self::SpawnBatch(mut batch) => {
                batch.apply(world);
                Self::SpawnBatch(batch)
            }
            Self::Deferred(command) => {
                command.apply(world);
                Self::Vacant
            }
        }
    }

    fn clear(self) -> Self {
        match self {
            Self::Vacant => Self::Vacant,
            Self::EntityBatch(mut batch) => {
                batch.clear();
                Self::EntityBatch(batch)
            }
            Self::SpawnBatch(mut batch) => {
                batch.clear();
                Self::SpawnBatch(batch)
            }
            Self::Deferred(command) => {
                drop(command);
                Self::Vacant
            }
        }
    }
}

enum EntityCommand {
    Despawn(EntityId),
    Insert {
        entity: EntityId,
        component: ComponentType,
        value: InsertValue,
    },
    Remove {
        entity: EntityId,
        component: ComponentType,
    },
}

// ---------------------------------------------------------------------------
// Per-entity coalesced command state
// ---------------------------------------------------------------------------

pub(super) enum PendingComponentCommand {
    Insert(InsertValue),
    Remove,
}

pub(super) struct PendingComponentEntry {
    pub(super) component: ComponentType,
    pub(super) command: PendingComponentCommand,
}

#[derive(Default)]
struct PendingEntityCommands {
    despawn: bool,
    components: SmallVec<[PendingComponentEntry; 4]>,
}

impl PendingEntityCommands {
    fn queue_insert(&mut self, component: ComponentType, value: InsertValue) {
        if self.despawn {
            return;
        }

        if let Some(pending) = self
            .components
            .iter_mut()
            .find(|p| p.component.id() == component.id())
        {
            pending.command = PendingComponentCommand::Insert(value);
            return;
        }

        self.components.push(PendingComponentEntry {
            component,
            command: PendingComponentCommand::Insert(value),
        });
    }

    fn queue_remove(&mut self, component: ComponentType) {
        if self.despawn {
            return;
        }

        if let Some(pending) = self
            .components
            .iter_mut()
            .find(|p| p.component.id() == component.id())
        {
            pending.command = PendingComponentCommand::Remove;
            return;
        }

        self.components.push(PendingComponentEntry {
            component,
            command: PendingComponentCommand::Remove,
        });
    }

    fn queue_despawn(&mut self) {
        self.despawn = true;
        self.components.clear();
    }
}

// ---------------------------------------------------------------------------
// PendingEntityBuffer – Vec-backed coalesced entity command buffer
//
// Primary storage is `entries`, preserving first-seen entity order.
// `index` is a dedup-only map from EntityId to position in `entries`.
// ---------------------------------------------------------------------------

#[derive(Default)]
struct PendingEntityBuffer {
    entries: Vec<(EntityId, PendingEntityCommands)>,
    index: FxHashMap<EntityId, u32>,
}

impl PendingEntityBuffer {
    fn push(&mut self, command: EntityCommand) {
        let entity = match &command {
            EntityCommand::Despawn(entity) => *entity,
            EntityCommand::Insert { entity, .. } => *entity,
            EntityCommand::Remove { entity, .. } => *entity,
        };

        let pending = if let Some(&pos) = self.index.get(&entity) {
            &mut self.entries[pos as usize].1
        } else {
            let pos = self.entries.len();
            debug_assert!(
                pos <= u32::MAX as usize,
                "PendingEntityBuffer: entry count exceeds u32 index capacity"
            );
            self.index.insert(entity, pos as u32);
            self.entries
                .push((entity, PendingEntityCommands::default()));
            &mut self.entries.last_mut().unwrap().1
        };

        match command {
            EntityCommand::Despawn(_) => pending.queue_despawn(),
            EntityCommand::Insert {
                component, value, ..
            } => pending.queue_insert(component, value),
            EntityCommand::Remove { component, .. } => pending.queue_remove(component),
        }
    }

    fn flush(&mut self, world: &mut World) {
        // The map is only needed while recording. Clear it before any user
        // destructor can unwind so the buffer is observably empty on panic,
        // while retaining its allocation for the next frame.
        self.index.clear();
        for (entity, pending) in self.entries.drain(..) {
            if pending.despawn {
                world.despawn(entity);
                continue;
            }

            let mut components = pending.components;
            world.apply_component_commands(entity, &mut components);
        }
    }

    fn clear(&mut self) {
        self.index.clear();
        self.entries.clear();
    }
}

// ---------------------------------------------------------------------------
// Owned command buffer
// ---------------------------------------------------------------------------

/// A deferred command buffer for batching structural ECS changes.
///
/// Commands are recorded and then applied in one explicit pass via
/// [`apply`](Self::apply).
/// This is useful when you need to make structural changes (spawn, despawn,
/// insert, remove) from within a query loop or a system, where direct
/// mutation of the [`World`] is not possible.
///
/// Commands targeting the same entity are **coalesced** — only the final
/// state per component is applied, reducing archetype migrations.
///
/// # Examples
///
/// ```
/// # use sky_ecs::{CommandBuffer, World};
/// # #[derive(Clone, Copy)] struct Health(f32);
/// # let mut world = World::new();
/// let entity = world.spawn((Health(100.0),));
///
/// let mut cmds = CommandBuffer::new();
/// cmds.insert(entity, Health(50.0));
/// cmds.apply(&mut world);
///
/// assert_eq!(world.get::<Health>(entity).unwrap().0, 50.0);
/// ```
#[derive(Default)]
pub struct CommandBuffer {
    queue: Vec<Command>,
    active_commands: usize,
    queued_count: usize,
}

/// Temporarily owns the active command prefix while preserving reusable slots.
///
/// Each command is replaced with `Vacant` before it can run user code. If that
/// code unwinds, `Drop` discards the unvisited commands and the public buffer
/// has already been restored to its empty invariant.
struct ActiveCommands<'a> {
    slots: &'a mut [Command],
    next: usize,
}

impl<'a> ActiveCommands<'a> {
    fn new(slots: &'a mut [Command]) -> Self {
        Self { slots, next: 0 }
    }

    fn next(&mut self) -> Option<(usize, Command)> {
        if self.next == self.slots.len() {
            return None;
        }
        let index = self.next;
        self.next += 1;
        Some((index, mem::replace(&mut self.slots[index], Command::Vacant)))
    }

    fn restore(&mut self, index: usize, command: Command) {
        debug_assert!(matches!(self.slots[index], Command::Vacant));
        self.slots[index] = command;
    }
}

impl Drop for ActiveCommands<'_> {
    fn drop(&mut self) {
        let already_panicking = std::thread::panicking();
        let mut first_cleanup_panic = None;

        for slot in &mut self.slots[self.next..] {
            let command = mem::replace(slot, Command::Vacant);
            let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                drop(command);
            }));

            if let Err(payload) = result {
                if already_panicking || first_cleanup_panic.is_some() {
                    // Cleanup must not replace an in-flight panic or trigger a
                    // double-panic abort. Most panic payloads can still be
                    // released normally; only an adversarial payload whose
                    // own destructor panics has to be leaked as a last resort.
                    drop_panic_payload_without_unwinding(payload);
                } else {
                    first_cleanup_panic = Some(payload);
                }
            }
        }

        if let Some(payload) = first_cleanup_panic {
            // No panic was active when cleanup began. Finish clearing every
            // slot, then propagate the first cleanup failure.
            std::panic::resume_unwind(payload);
        }
    }
}

fn drop_panic_payload_without_unwinding(payload: Box<dyn Any + Send>) {
    let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| drop(payload)));
    if let Err(nested_payload) = result {
        // A panic payload is user-owned and may itself have a panicking Drop.
        // Letting that unwind while another panic is active would abort the
        // process, so this final adversarial payload is intentionally leaked.
        mem::forget(nested_payload);
    }
}

struct CommandApplyGuard<'w> {
    world: &'w mut World,
    completed: bool,
}

impl<'w> CommandApplyGuard<'w> {
    fn new(world: &'w mut World) -> Self {
        world.assert_command_apply_allowed();
        Self {
            world,
            completed: false,
        }
    }

    #[inline(always)]
    fn world(&mut self) -> &mut World {
        self.world
    }

    fn complete(mut self) {
        self.completed = true;
    }
}

impl Drop for CommandApplyGuard<'_> {
    fn drop(&mut self) {
        if !self.completed && std::thread::panicking() {
            self.world.poison_after_command_panic();
        }
    }
}

impl CommandBuffer {
    /// Creates a new, empty command buffer.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns `true` if no commands have been recorded.
    pub fn is_empty(&self) -> bool {
        self.queued_count == 0
    }

    /// Returns the number of commands recorded so far.
    pub fn len(&self) -> usize {
        self.queued_count
    }

    fn push_deferred<F>(&mut self, f: F)
    where
        F: FnOnce(&mut World) + 'static,
    {
        self.queued_count += 1;
        self.activate(Command::Deferred(Box::new(FnDeferredCommand(f))));
    }

    fn push_entity(&mut self, command: EntityCommand) {
        self.queued_count += 1;

        if let Some(Command::EntityBatch(batch)) = self.active_last_mut() {
            batch.push(command);
            return;
        }

        let index = self.active_commands;
        self.active_commands += 1;
        if let Some(Command::EntityBatch(batch)) = self.queue.get_mut(index) {
            batch.push(command);
            return;
        }

        let mut batch = PendingEntityBuffer::default();
        batch.push(command);
        self.replace_or_push(index, Command::EntityBatch(batch));
    }

    /// Records a deferred spawn.  Consecutive spawns of the same bundle
    /// type are coalesced into a single batch for efficiency.
    pub fn spawn<B>(&mut self, bundle: B)
    where
        B: Bundle,
    {
        self.queued_count += 1;

        if let Some(Command::SpawnBatch(batch)) = self.active_last_mut() {
            if let Some(batch) = batch.as_any_mut().downcast_mut::<TypedSpawnBatch<B>>() {
                batch.bundles.push(bundle);
                return;
            }
        }

        let index = self.active_commands;
        self.active_commands += 1;
        if let Some(Command::SpawnBatch(batch)) = self.queue.get_mut(index) {
            if let Some(batch) = batch.as_any_mut().downcast_mut::<TypedSpawnBatch<B>>() {
                batch.bundles.push(bundle);
                return;
            }
        }

        self.replace_or_push(
            index,
            Command::SpawnBatch(Box::new(TypedSpawnBatch {
                bundles: vec![bundle],
            })),
        );
    }

    /// Records a deferred entity despawn.
    pub fn despawn(&mut self, entity: EntityId) {
        self.push_entity(EntityCommand::Despawn(entity));
    }

    /// Records a deferred component insertion (or overwrite).
    pub fn insert<T>(&mut self, entity: EntityId, component: T)
    where
        T: 'static,
    {
        self.push_entity(EntityCommand::Insert {
            entity,
            component: component_type::<T>(),
            value: InsertValue::from_value(component),
        });
    }

    /// Records a deferred component removal.
    pub fn remove<T>(&mut self, entity: EntityId)
    where
        T: 'static,
    {
        self.push_entity(EntityCommand::Remove {
            entity,
            component: component_type::<T>(),
        });
    }

    /// Records a deferred resource insertion.
    pub fn insert_resource<R>(&mut self, resource: R)
    where
        R: 'static,
    {
        self.push_deferred(move |world| {
            world.insert_resource(resource);
        });
    }

    /// Records a deferred resource removal.
    pub fn remove_resource<R>(&mut self)
    where
        R: 'static,
    {
        self.push_deferred(move |world| {
            world.remove_resource::<R>();
        });
    }

    /// Applies all recorded commands to the world and clears the buffer.
    ///
    /// # Panics and poisoning
    ///
    /// Commands may contain arbitrary user values whose code or destructors
    /// can panic. General rollback is therefore impossible. If a panic escapes
    /// this apply pass, the World is marked poisoned and rejects later command
    /// application and schedule ticks rather than running with a partial
    /// commit. The World remains inspectable and may be shut down.
    pub fn apply(&mut self, world: &mut World) {
        let mut apply_guard = CommandApplyGuard::new(world);
        // Restore the observable invariant before user-owned drops/deferred
        // work can unwind. The active prefix remains as reusable empty slots
        // after a successful pass.
        let active_commands = mem::take(&mut self.active_commands);
        self.queued_count = 0;
        let mut commands = ActiveCommands::new(&mut self.queue[..active_commands]);
        while let Some((index, command)) = commands.next() {
            let command = command.apply(apply_guard.world());
            commands.restore(index, command);
        }
        drop(commands);
        apply_guard.complete();
    }

    /// Discards every pending command while preserving allocated capacity.
    pub fn clear(&mut self) {
        let active_commands = mem::take(&mut self.active_commands);
        self.queued_count = 0;
        let mut commands = ActiveCommands::new(&mut self.queue[..active_commands]);
        while let Some((index, command)) = commands.next() {
            let command = command.clear();
            commands.restore(index, command);
        }
    }

    fn active_last_mut(&mut self) -> Option<&mut Command> {
        self.active_commands
            .checked_sub(1)
            .map(|index| &mut self.queue[index])
    }

    fn activate(&mut self, command: Command) {
        let index = self.active_commands;
        self.active_commands += 1;
        self.replace_or_push(index, command);
    }

    fn replace_or_push(&mut self, index: usize, command: Command) {
        if let Some(slot) = self.queue.get_mut(index) {
            *slot = command;
        } else {
            debug_assert_eq!(index, self.queue.len());
            self.queue.push(command);
        }
    }
}

// ---------------------------------------------------------------------------
// Borrowed system command writer
// ---------------------------------------------------------------------------

/// A system-local deferred structural writer.
///
/// Each scheduled system receives an isolated writer. The scheduler applies
/// the underlying buffers in deterministic registration order at the next
/// documented flush boundary.
pub struct Commands<'w> {
    buffer: &'w mut CommandBuffer,
}

impl<'w> Commands<'w> {
    /// # Safety
    ///
    /// `buffer` must be non-null, point to a live `CommandBuffer`, and be
    /// exclusively borrowed for the returned lifetime.
    pub(crate) unsafe fn from_ptr(buffer: *mut CommandBuffer) -> Self {
        Self {
            // SAFETY: the caller provides validity, alignment, and exclusive
            // access for `'w` as required by this constructor.
            buffer: unsafe { &mut *buffer },
        }
    }

    pub fn is_empty(&self) -> bool {
        self.buffer.is_empty()
    }

    pub fn len(&self) -> usize {
        self.buffer.len()
    }

    pub fn spawn<B>(&mut self, bundle: B)
    where
        B: Bundle + Send,
    {
        self.buffer.spawn(bundle);
    }

    pub fn despawn(&mut self, entity: EntityId) {
        self.buffer.despawn(entity);
    }

    pub fn insert<T>(&mut self, entity: EntityId, component: T)
    where
        T: Send + 'static,
    {
        self.buffer.insert(entity, component);
    }

    pub fn remove<T>(&mut self, entity: EntityId)
    where
        T: 'static,
    {
        self.buffer.remove::<T>(entity);
    }

    pub fn insert_resource<R>(&mut self, resource: R)
    where
        R: Send + 'static,
    {
        self.buffer.insert_resource(resource);
    }

    pub fn remove_resource<R>(&mut self)
    where
        R: 'static,
    {
        self.buffer.remove_resource::<R>();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::panic::{catch_unwind, AssertUnwindSafe};
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    #[derive(Clone, Copy)]
    struct Marker;

    #[test]
    fn entity_batches_preserve_first_seen_order_and_reuse_storage() {
        let mut world = World::new();
        let entities = (0..64).map(|_| world.spawn((Marker,))).collect::<Vec<_>>();
        let mut commands = CommandBuffer::new();

        commands.despawn(entities[7]);
        commands.remove::<Marker>(entities[2]);
        commands.insert(entities[7], Marker);
        commands.despawn(entities[40]);

        let (entries_capacity, index_capacity) = match &commands.queue[0] {
            Command::EntityBatch(batch) => {
                assert_eq!(
                    batch
                        .entries
                        .iter()
                        .map(|(entity, _)| *entity)
                        .collect::<Vec<_>>(),
                    vec![entities[7], entities[2], entities[40]]
                );
                (batch.entries.capacity(), batch.index.capacity())
            }
            _ => panic!("expected an entity batch"),
        };

        commands.apply(&mut world);
        assert!(commands.is_empty());
        match &commands.queue[0] {
            Command::EntityBatch(batch) => {
                assert!(batch.entries.is_empty());
                assert!(batch.index.is_empty());
                assert_eq!(batch.entries.capacity(), entries_capacity);
                assert_eq!(batch.index.capacity(), index_capacity);
            }
            _ => panic!("expected the reusable entity batch"),
        }

        let replacements = (0..3).map(|_| world.spawn((Marker,))).collect::<Vec<_>>();
        for entity in &replacements {
            commands.despawn(*entity);
        }
        match &commands.queue[0] {
            Command::EntityBatch(batch) => {
                assert_eq!(batch.entries.capacity(), entries_capacity);
                assert_eq!(batch.index.capacity(), index_capacity);
            }
            _ => panic!("expected the reused entity batch"),
        }
    }

    #[test]
    fn typed_spawn_batches_reuse_bundle_capacity() {
        let mut world = World::new();
        let mut commands = CommandBuffer::new();
        for _ in 0..64 {
            commands.spawn((Marker,));
        }

        let capacity = match &mut commands.queue[0] {
            Command::SpawnBatch(batch) => batch
                .as_any_mut()
                .downcast_mut::<TypedSpawnBatch<(Marker,)>>()
                .unwrap()
                .bundles
                .capacity(),
            _ => panic!("expected a typed spawn batch"),
        };

        commands.apply(&mut world);
        assert_eq!(world.entity_count(), 64);
        match &mut commands.queue[0] {
            Command::SpawnBatch(batch) => {
                let batch = batch
                    .as_any_mut()
                    .downcast_mut::<TypedSpawnBatch<(Marker,)>>()
                    .unwrap();
                assert!(batch.bundles.is_empty());
                assert_eq!(batch.bundles.capacity(), capacity);
            }
            _ => panic!("expected the reusable spawn batch"),
        }

        for _ in 0..64 {
            commands.spawn((Marker,));
        }
        match &mut commands.queue[0] {
            Command::SpawnBatch(batch) => assert_eq!(
                batch
                    .as_any_mut()
                    .downcast_mut::<TypedSpawnBatch<(Marker,)>>()
                    .unwrap()
                    .bundles
                    .capacity(),
                capacity
            ),
            _ => panic!("expected the reused spawn batch"),
        }
    }

    struct DropCounter(Arc<AtomicUsize>);

    impl Drop for DropCounter {
        fn drop(&mut self) {
            self.0.fetch_add(1, Ordering::Relaxed);
        }
    }

    struct PanicOnDrop(Arc<AtomicUsize>);

    impl Drop for PanicOnDrop {
        fn drop(&mut self) {
            self.0.fetch_add(1, Ordering::Relaxed);
            panic!("secondary cleanup panic");
        }
    }

    #[test]
    fn apply_panic_discards_unvisited_reusable_slots() {
        let drops = Arc::new(AtomicUsize::new(0));
        let mut world = World::new();
        let mut commands = CommandBuffer::new();
        commands.push_deferred(|_| {});
        commands.push_deferred(|_| panic!("intentional deferred command panic"));
        commands.spawn((DropCounter(drops.clone()),));

        let result = catch_unwind(AssertUnwindSafe(|| commands.apply(&mut world)));

        assert!(result.is_err());
        assert!(commands.is_empty());
        assert_eq!(commands.active_commands, 0);
        assert_eq!(drops.load(Ordering::Relaxed), 1);
        assert!(world.is_poisoned());
    }

    #[test]
    fn apply_panic_swallows_panics_from_unvisited_command_drops() {
        let panicking_drops = Arc::new(AtomicUsize::new(0));
        let ordinary_drops = Arc::new(AtomicUsize::new(0));
        let mut world = World::new();
        let mut commands = CommandBuffer::new();

        commands.push_deferred(|_| panic!("primary command panic"));
        let panic_on_drop = PanicOnDrop(panicking_drops.clone());
        commands.push_deferred(move |_| {
            let _ = &panic_on_drop;
        });
        commands.spawn((DropCounter(ordinary_drops.clone()),));

        let result = catch_unwind(AssertUnwindSafe(|| commands.apply(&mut world)));
        let payload = result.expect_err("the primary command must still unwind");

        assert_eq!(
            payload.downcast_ref::<&'static str>(),
            Some(&"primary command panic")
        );
        assert_eq!(panicking_drops.load(Ordering::Relaxed), 1);
        assert_eq!(ordinary_drops.load(Ordering::Relaxed), 1);
        assert!(commands.is_empty());
        assert_eq!(commands.active_commands, 0);
        assert!(world.is_poisoned());
    }
}