bevy_ecs 0.19.0

Bevy Engine's entity component system
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
//! Entity spawning abstractions, largely focused on spawning related hierarchies of entities. See [`related`](crate::related) and [`SpawnRelated`]
//! for the best entry points into these APIs and examples of how to use them.

use crate::{
    bundle::{Bundle, DynamicBundle, InsertMode, NoBundleEffect},
    change_detection::MaybeLocation,
    entity::Entity,
    relationship::{RelatedSpawner, Relationship, RelationshipHookMode, RelationshipTarget},
    world::{EntityWorldMut, World},
};
use alloc::vec::Vec;
use bevy_ptr::{move_as_ptr, MovingPtr};
use core::{
    marker::PhantomData,
    mem::{self, MaybeUninit},
};
use variadics_please::all_tuples_enumerated;

/// A wrapper over a [`Bundle`] indicating that an entity should be spawned with that [`Bundle`].
/// This is intended to be used for hierarchical spawning via traits like [`SpawnableList`] and [`SpawnRelated`].
///
/// Also see the [`children`](crate::children) and [`related`](crate::related) macros that abstract over the [`Spawn`] API.
///
/// ```
/// # use bevy_ecs::hierarchy::Children;
/// # use bevy_ecs::spawn::{Spawn, SpawnRelated};
/// # use bevy_ecs::name::Name;
/// # use bevy_ecs::world::World;
/// let mut world = World::new();
/// world.spawn((
///     Name::new("Root"),
///     Children::spawn((
///         Spawn(Name::new("Child1")),
///         Spawn((
///             Name::new("Child2"),
///             Children::spawn(Spawn(Name::new("Grandchild"))),
///         ))
///     )),
/// ));
/// ```
pub struct Spawn<B: Bundle>(pub B);

/// A spawn-able list of changes to a given [`World`] and relative to a given [`Entity`]. This is generally used
/// for spawning "related" entities, such as children.
pub trait SpawnableList<R>: Sized {
    /// Spawn this list of changes in a given [`World`] and relative to a given [`Entity`]. This is generally used
    /// for spawning "related" entities, such as children.
    // This function explicitly uses `MovingPtr` to avoid potentially large stack copies of the bundle
    // when inserting into ECS storage. See https://github.com/bevyengine/bevy/issues/20571 for more
    // information.
    fn spawn(this: MovingPtr<'_, Self>, world: &mut World, entity: Entity);

    /// Returns a size hint, which is used to reserve space for this list in a [`RelationshipTarget`]. This should be
    /// less than or equal to the actual size of the list. When in doubt, just use 0.
    fn size_hint(&self) -> usize;
}

impl<R: Relationship, B: Bundle<Effect: NoBundleEffect>> SpawnableList<R> for Vec<B> {
    fn spawn(ptr: MovingPtr<'_, Self>, world: &mut World, entity: Entity) {
        let mapped_bundles = ptr.read().into_iter().map(|b| (R::from(entity), b));
        world.spawn_batch(mapped_bundles);
    }

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

impl<R: Relationship, B: Bundle> SpawnableList<R> for Spawn<B> {
    fn spawn(this: MovingPtr<'_, Self>, world: &mut World, entity: Entity) {
        #[track_caller]
        fn spawn<B: Bundle, R: Relationship>(
            this: MovingPtr<'_, Spawn<B>>,
            world: &mut World,
            entity: Entity,
        ) {
            let caller = MaybeLocation::caller();

            bevy_ptr::deconstruct_moving_ptr!({
                let Spawn { 0: bundle } = this;
            });

            let r = R::from(entity);
            move_as_ptr!(r);
            let mut entity = world.spawn_with_caller(r, caller);

            entity.insert_with_caller(
                bundle,
                InsertMode::Replace,
                caller,
                RelationshipHookMode::Run,
            );
        }

        spawn::<B, R>(this, world, entity);
    }

    fn size_hint(&self) -> usize {
        1
    }
}

/// A [`SpawnableList`] that spawns entities using an iterator of a given [`Bundle`]:
///
/// ```
/// # use bevy_ecs::hierarchy::Children;
/// # use bevy_ecs::spawn::{Spawn, SpawnIter, SpawnRelated};
/// # use bevy_ecs::name::Name;
/// # use bevy_ecs::world::World;
/// let mut world = World::new();
/// world.spawn((
///     Name::new("Root"),
///     Children::spawn((
///         Spawn(Name::new("Child1")),
///         SpawnIter(["Child2", "Child3"].into_iter().map(Name::new)),
///     )),
/// ));
/// ```
pub struct SpawnIter<I>(pub I);

impl<R: Relationship, I: Iterator<Item = B> + Send + Sync + 'static, B: Bundle> SpawnableList<R>
    for SpawnIter<I>
{
    fn spawn(mut this: MovingPtr<'_, Self>, world: &mut World, entity: Entity) {
        for bundle in &mut this.0 {
            world.spawn((R::from(entity), bundle));
        }
    }

    fn size_hint(&self) -> usize {
        self.0.size_hint().0
    }
}

/// A [`SpawnableList`] that spawns entities using a [`FnOnce`] with a [`RelatedSpawner`] as an argument:
///
/// ```
/// # use bevy_ecs::hierarchy::{Children, ChildOf};
/// # use bevy_ecs::spawn::{Spawn, SpawnWith, SpawnRelated};
/// # use bevy_ecs::name::Name;
/// # use bevy_ecs::relationship::RelatedSpawner;
/// # use bevy_ecs::world::World;
/// let mut world = World::new();
/// world.spawn((
///     Name::new("Root"),
///     Children::spawn((
///         Spawn(Name::new("Child1")),
///         SpawnWith(|parent: &mut RelatedSpawner<ChildOf>| {
///             parent.spawn(Name::new("Child2"));
///             parent.spawn(Name::new("Child3"));
///         }),
///     )),
/// ));
/// ```
pub struct SpawnWith<F>(pub F);

impl<R: Relationship, F: FnOnce(&mut RelatedSpawner<R>) + Send + Sync + 'static> SpawnableList<R>
    for SpawnWith<F>
{
    fn spawn(this: MovingPtr<'_, Self>, world: &mut World, entity: Entity) {
        world
            .entity_mut(entity)
            .with_related_entities(this.read().0);
    }

    fn size_hint(&self) -> usize {
        1
    }
}

/// A [`SpawnableList`] that links already spawned entities to the root entity via relations of type `I`.
///
/// This is useful if the entity has already been spawned earlier or if you spawn multiple relationships link to the same entity at the same time.
/// If you only need to do this for a single entity, consider using [`WithOneRelated`].
///
/// ```
/// # use bevy_ecs::hierarchy::Children;
/// # use bevy_ecs::spawn::{Spawn, WithRelated, SpawnRelated};
/// # use bevy_ecs::name::Name;
/// # use bevy_ecs::world::World;
/// let mut world = World::new();
///
/// let child2 = world.spawn(Name::new("Child2")).id();
/// let child3 = world.spawn(Name::new("Child3")).id();
///
/// world.spawn((
///     Name::new("Root"),
///     Children::spawn((
///         Spawn(Name::new("Child1")),
///         // This adds the already existing entities as children of Root.
///         WithRelated::new([child2, child3]),
///     )),
/// ));
/// ```
pub struct WithRelated<I>(pub I);

impl<I> WithRelated<I> {
    /// Creates a new [`WithRelated`] from a collection of entities.
    pub fn new(iter: impl IntoIterator<IntoIter = I>) -> Self {
        Self(iter.into_iter())
    }
}

impl<R: Relationship, I: Iterator<Item = Entity>> SpawnableList<R> for WithRelated<I> {
    fn spawn(mut this: MovingPtr<'_, Self>, world: &mut World, entity: Entity) {
        let related = (&mut this.0).collect::<Vec<_>>();
        world.entity_mut(entity).add_related::<R>(&related);
    }

    fn size_hint(&self) -> usize {
        self.0.size_hint().0
    }
}

/// A wrapper over an [`Entity`] indicating that an entity should be added.
/// This is intended to be used for hierarchical spawning via traits like [`SpawnableList`] and [`SpawnRelated`].
///
/// Unlike [`WithRelated`] this only adds one entity.
///
/// Also see the [`children`](crate::children) and [`related`](crate::related) macros that abstract over the [`Spawn`] API.
///
/// ```
/// # use bevy_ecs::hierarchy::Children;
/// # use bevy_ecs::spawn::{Spawn, WithOneRelated, SpawnRelated};
/// # use bevy_ecs::name::Name;
/// # use bevy_ecs::world::World;
/// let mut world = World::new();
///
/// let child1 = world.spawn(Name::new("Child1")).id();
///
/// world.spawn((
///     Name::new("Root"),
///     Children::spawn((
///         // This adds the already existing entity as a child of Root.
///         WithOneRelated(child1),
///     )),
/// ));
/// ```
pub struct WithOneRelated(pub Entity);

impl<R: Relationship> SpawnableList<R> for WithOneRelated {
    fn spawn(this: MovingPtr<'_, Self>, world: &mut World, entity: Entity) {
        world.entity_mut(entity).add_one_related::<R>(this.read().0);
    }

    fn size_hint(&self) -> usize {
        1
    }
}

macro_rules! spawnable_list_impl {
    ($(#[$meta:meta])* $(($index:tt, $list: ident, $alias: ident)),*) => {
        $(#[$meta])*
        impl<R: Relationship, $($list: SpawnableList<R>),*> SpawnableList<R> for ($($list,)*) {
            #[expect(
                clippy::allow_attributes,
                reason = "This is a tuple-related macro; as such, the lints below may not always apply."
            )]
            #[allow(unused_unsafe, reason = "The empty tuple will leave the unsafe blocks unused.")]
            fn spawn(_this: MovingPtr<'_, Self>, _world: &mut World, _entity: Entity)
            where
                Self: Sized,
            {
                bevy_ptr::deconstruct_moving_ptr!({
                    let tuple { $($index: $alias),* } = _this;
                });
                $( SpawnableList::<R>::spawn($alias, _world, _entity); )*
            }

            fn size_hint(&self) -> usize {
                let ($($alias,)*) = self;
                0 $(+ $alias.size_hint())*
            }
       }
    }
}

all_tuples_enumerated!(
    #[doc(fake_variadic)]
    spawnable_list_impl,
    0,
    12,
    P,
    field_
);

/// A [`Bundle`] that:
/// 1. Contains a [`RelationshipTarget`] component (associated with the given [`Relationship`]). This reserves space for the [`SpawnableList`].
/// 2. Spawns a [`SpawnableList`] of related entities with a given [`Relationship`].
///
/// This is intended to be created using [`SpawnRelated`].
pub struct SpawnRelatedBundle<R: Relationship, L: SpawnableList<R>> {
    list: L,
    marker: PhantomData<R>,
}

// SAFETY: This internally relies on the RelationshipTarget's Bundle implementation, which is sound.
unsafe impl<R: Relationship, L: SpawnableList<R> + Send + Sync + 'static> Bundle
    for SpawnRelatedBundle<R, L>
{
    fn component_ids(
        components: &mut crate::component::ComponentsRegistrator,
    ) -> impl Iterator<Item = crate::component::ComponentId> + use<R, L> {
        <R::RelationshipTarget as Bundle>::component_ids(components)
    }

    fn get_component_ids(
        components: &crate::component::Components,
    ) -> impl Iterator<Item = Option<crate::component::ComponentId>> {
        <R::RelationshipTarget as Bundle>::get_component_ids(components)
    }
}

impl<R: Relationship, L: SpawnableList<R>> DynamicBundle for SpawnRelatedBundle<R, L> {
    type Effect = Self;

    unsafe fn get_components(
        ptr: MovingPtr<'_, Self>,
        func: &mut impl FnMut(crate::component::StorageType, bevy_ptr::OwningPtr<'_>),
    ) {
        let target =
            <R::RelationshipTarget as RelationshipTarget>::with_capacity(ptr.list.size_hint());
        move_as_ptr!(target);
        // SAFETY:
        // - The caller must ensure that this is called exactly once before `apply_effect`.
        // - Assuming `DynamicBundle` is implemented correctly for `R::Relationship` target, `func` should be
        //   called exactly once for each component being fetched with the correct `StorageType`
        // - `Effect: !NoBundleEffect`, which means the caller is responsible for calling this type's `apply_effect`
        //   at least once before returning to safe code.
        unsafe { <R::RelationshipTarget as DynamicBundle>::get_components(target, func) };
        // Forget the pointer so that the value is available in `apply_effect`.
        mem::forget(ptr);
    }

    unsafe fn apply_effect(ptr: MovingPtr<'_, MaybeUninit<Self>>, entity: &mut EntityWorldMut) {
        // SAFETY: The value was not moved out in `get_components`, only borrowed, and thus should still
        // be valid and initialized.
        let effect = unsafe { ptr.assume_init() };
        bevy_ptr::deconstruct_moving_ptr!({
            let Self { list, marker: _ } = effect;
        });
        let id = entity.id();

        if entity.is_despawned() {
            return;
        }

        entity.world_scope(|world: &mut World| {
            L::spawn(list, world, id);
        });
    }
}

/// A [`Bundle`] that:
/// 1. Contains a [`RelationshipTarget`] component (associated with the given [`Relationship`]). This reserves space for a single entity.
/// 2. Spawns a single related entity containing the given `B` [`Bundle`] and the given [`Relationship`].
///
/// This is intended to be created using [`SpawnRelated`].
pub struct SpawnOneRelated<R: Relationship, B: Bundle> {
    bundle: B,
    marker: PhantomData<R>,
}

impl<R: Relationship, B: Bundle> DynamicBundle for SpawnOneRelated<R, B> {
    type Effect = Self;

    unsafe fn get_components(
        ptr: MovingPtr<'_, Self>,
        func: &mut impl FnMut(crate::component::StorageType, bevy_ptr::OwningPtr<'_>),
    ) {
        let target = <R::RelationshipTarget as RelationshipTarget>::with_capacity(1);
        move_as_ptr!(target);
        // SAFETY:
        // - The caller must ensure that this is called exactly once before `apply_effect`.
        // - Assuming `DynamicBundle` is implemented correctly for `R::Relationship` target, `func` should be
        //   called exactly once for each component being fetched with the correct `StorageType`
        // - `Effect: !NoBundleEffect`, which means the caller is responsible for calling this type's `apply_effect`
        //   at least once before returning to safe code.
        unsafe { <R::RelationshipTarget as DynamicBundle>::get_components(target, func) };
        // Forget the pointer so that the value is available in `apply_effect`.
        mem::forget(ptr);
    }

    unsafe fn apply_effect(ptr: MovingPtr<'_, MaybeUninit<Self>>, entity: &mut EntityWorldMut) {
        // SAFETY: The value was not moved out in `get_components`, only borrowed, and thus should still
        // be valid and initialized.
        let effect = unsafe { ptr.assume_init() };
        if entity.is_despawned() {
            return;
        }

        let effect = effect.read();
        entity.with_related::<R>(effect.bundle);
    }
}

// SAFETY: This internally relies on the RelationshipTarget's Bundle implementation, which is sound.
unsafe impl<R: Relationship, B: Bundle> Bundle for SpawnOneRelated<R, B> {
    fn component_ids(
        components: &mut crate::component::ComponentsRegistrator,
    ) -> impl Iterator<Item = crate::component::ComponentId> + use<R, B> {
        <R::RelationshipTarget as Bundle>::component_ids(components)
    }

    fn get_component_ids(
        components: &crate::component::Components,
    ) -> impl Iterator<Item = Option<crate::component::ComponentId>> {
        <R::RelationshipTarget as Bundle>::get_component_ids(components)
    }
}

/// [`RelationshipTarget`] methods that create a [`Bundle`] with a [`DynamicBundle::Effect`] that:
///
/// 1. Contains the [`RelationshipTarget`] component, pre-allocated with the necessary space for spawned entities.
/// 2. Spawns an entity (or a list of entities) that relate to the entity the [`Bundle`] is added to via the [`RelationshipTarget::Relationship`].
pub trait SpawnRelated: RelationshipTarget {
    /// Returns a [`Bundle`] containing this [`RelationshipTarget`] component. It also spawns a [`SpawnableList`] of entities, each related to the bundle's entity
    /// via [`RelationshipTarget::Relationship`]. The [`RelationshipTarget`] (when possible) will pre-allocate space for the related entities.
    ///
    /// See [`Spawn`], [`SpawnIter`], [`SpawnWith`], [`WithRelated`] and [`WithOneRelated`] for usage examples.
    fn spawn<L: SpawnableList<Self::Relationship>>(
        list: L,
    ) -> SpawnRelatedBundle<Self::Relationship, L>;

    /// Returns a [`Bundle`] containing this [`RelationshipTarget`] component. It also spawns a single entity containing [`Bundle`] that is related to the bundle's entity
    /// via [`RelationshipTarget::Relationship`].
    ///
    /// ```
    /// # use bevy_ecs::hierarchy::Children;
    /// # use bevy_ecs::spawn::SpawnRelated;
    /// # use bevy_ecs::name::Name;
    /// # use bevy_ecs::world::World;
    /// let mut world = World::new();
    /// world.spawn((
    ///     Name::new("Root"),
    ///     Children::spawn_one(Name::new("Child")),
    /// ));
    /// ```
    fn spawn_one<B: Bundle>(bundle: B) -> SpawnOneRelated<Self::Relationship, B>;
}

impl<T: RelationshipTarget> SpawnRelated for T {
    fn spawn<L: SpawnableList<Self::Relationship>>(
        list: L,
    ) -> SpawnRelatedBundle<Self::Relationship, L> {
        SpawnRelatedBundle {
            list,
            marker: PhantomData,
        }
    }

    fn spawn_one<B: Bundle>(bundle: B) -> SpawnOneRelated<Self::Relationship, B> {
        SpawnOneRelated {
            bundle,
            marker: PhantomData,
        }
    }
}

/// Returns a [`SpawnRelatedBundle`] that will insert the given [`RelationshipTarget`], spawn a [`SpawnableList`] of entities with given bundles that
/// relate to the [`RelationshipTarget`] entity via the [`RelationshipTarget::Relationship`] component, and reserve space in the [`RelationshipTarget`] for each spawned entity.
///
/// The first argument is the [`RelationshipTarget`] type. Any additional arguments will be interpreted as bundles to be spawned.
///
/// Also see [`children`](crate::children) for a [`Children`](crate::hierarchy::Children)-specific equivalent.
///
/// ```
/// # use bevy_ecs::hierarchy::Children;
/// # use bevy_ecs::name::Name;
/// # use bevy_ecs::world::World;
/// # use bevy_ecs::related;
/// let mut world = World::new();
/// world.spawn((
///     Name::new("Root"),
///     related!(Children[
///         Name::new("Child1"),
///         (
///             Name::new("Child2"),
///             related!(Children[
///                 Name::new("Grandchild"),
///             ])
///         )
///     ])
/// ));
/// ```
#[macro_export]
macro_rules! related {
    ($relationship_target:ty [$($child:expr),*$(,)?]) => {
       <$relationship_target as $crate::spawn::SpawnRelated>::spawn($crate::recursive_spawn!($($child),*))
    };
}

// A tail-recursive spawn utility.
//
// Since `SpawnableList` is only implemented for tuples
// up to twelve elements long, this macro will nest
// longer sequences recursively. By default, this recursion
// will top out at around 1400 elements, but it would be
// ill-advised to spawn that many entities with this method.
//
// For spawning large batches of entities at a time,
// consider `SpawnIter` or eagerly spawning with `Commands`.
#[macro_export]
#[doc(hidden)]
macro_rules! recursive_spawn {
    // direct expansion
    () => { () };
    ($a:expr) => {
        $crate::spawn::Spawn($a)
    };
    ($a:expr, $b:expr) => {
        (
            $crate::spawn::Spawn($a),
            $crate::spawn::Spawn($b),
        )
    };
    ($a:expr, $b:expr, $c:expr) => {
        (
            $crate::spawn::Spawn($a),
            $crate::spawn::Spawn($b),
            $crate::spawn::Spawn($c),
        )
    };
    ($a:expr, $b:expr, $c:expr, $d:expr) => {
        (
            $crate::spawn::Spawn($a),
            $crate::spawn::Spawn($b),
            $crate::spawn::Spawn($c),
            $crate::spawn::Spawn($d),
        )
    };
    ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr) => {
        (
            $crate::spawn::Spawn($a),
            $crate::spawn::Spawn($b),
            $crate::spawn::Spawn($c),
            $crate::spawn::Spawn($d),
            $crate::spawn::Spawn($e),
        )
    };
    ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr) => {
        (
            $crate::spawn::Spawn($a),
            $crate::spawn::Spawn($b),
            $crate::spawn::Spawn($c),
            $crate::spawn::Spawn($d),
            $crate::spawn::Spawn($e),
            $crate::spawn::Spawn($f),
        )
    };
    ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr) => {
        (
            $crate::spawn::Spawn($a),
            $crate::spawn::Spawn($b),
            $crate::spawn::Spawn($c),
            $crate::spawn::Spawn($d),
            $crate::spawn::Spawn($e),
            $crate::spawn::Spawn($f),
            $crate::spawn::Spawn($g),
        )
    };
    ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr) => {
        (
            $crate::spawn::Spawn($a),
            $crate::spawn::Spawn($b),
            $crate::spawn::Spawn($c),
            $crate::spawn::Spawn($d),
            $crate::spawn::Spawn($e),
            $crate::spawn::Spawn($f),
            $crate::spawn::Spawn($g),
            $crate::spawn::Spawn($h),
        )
    };
    ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr) => {
        (
            $crate::spawn::Spawn($a),
            $crate::spawn::Spawn($b),
            $crate::spawn::Spawn($c),
            $crate::spawn::Spawn($d),
            $crate::spawn::Spawn($e),
            $crate::spawn::Spawn($f),
            $crate::spawn::Spawn($g),
            $crate::spawn::Spawn($h),
            $crate::spawn::Spawn($i),
        )
    };
    ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr) => {
        (
            $crate::spawn::Spawn($a),
            $crate::spawn::Spawn($b),
            $crate::spawn::Spawn($c),
            $crate::spawn::Spawn($d),
            $crate::spawn::Spawn($e),
            $crate::spawn::Spawn($f),
            $crate::spawn::Spawn($g),
            $crate::spawn::Spawn($h),
            $crate::spawn::Spawn($i),
            $crate::spawn::Spawn($j),
        )
    };
    ($a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr, $g:expr, $h:expr, $i:expr, $j:expr, $k:expr) => {
        (
            $crate::spawn::Spawn($a),
            $crate::spawn::Spawn($b),
            $crate::spawn::Spawn($c),
            $crate::spawn::Spawn($d),
            $crate::spawn::Spawn($e),
            $crate::spawn::Spawn($f),
            $crate::spawn::Spawn($g),
            $crate::spawn::Spawn($h),
            $crate::spawn::Spawn($i),
            $crate::spawn::Spawn($j),
            $crate::spawn::Spawn($k),
        )
    };

    // recursive expansion
    (
        $a:expr, $b:expr, $c:expr, $d:expr, $e:expr, $f:expr,
        $g:expr, $h:expr, $i:expr, $j:expr, $k:expr, $($rest:expr),*
    ) => {
        (
            $crate::spawn::Spawn($a),
            $crate::spawn::Spawn($b),
            $crate::spawn::Spawn($c),
            $crate::spawn::Spawn($d),
            $crate::spawn::Spawn($e),
            $crate::spawn::Spawn($f),
            $crate::spawn::Spawn($g),
            $crate::spawn::Spawn($h),
            $crate::spawn::Spawn($i),
            $crate::spawn::Spawn($j),
            $crate::spawn::Spawn($k),
            $crate::recursive_spawn!($($rest),*)
        )
    };
}

#[cfg(test)]
mod tests {

    use crate::{
        name::Name,
        prelude::{ChildOf, Children, RelationshipTarget},
        relationship::RelatedSpawner,
        world::World,
    };

    use super::{Spawn, SpawnIter, SpawnRelated, SpawnWith, WithOneRelated, WithRelated};

    #[test]
    fn spawn() {
        let mut world = World::new();

        let parent = world
            .spawn((
                Name::new("Parent"),
                Children::spawn(Spawn(Name::new("Child1"))),
            ))
            .id();

        let children = world
            .query::<&Children>()
            .get(&world, parent)
            .expect("An entity with Children should exist");

        assert_eq!(children.iter().count(), 1);

        for ChildOf(child) in world.query::<&ChildOf>().iter(&world) {
            assert_eq!(child, &parent);
        }
    }

    #[test]
    fn spawn_iter() {
        let mut world = World::new();

        let parent = world
            .spawn((
                Name::new("Parent"),
                Children::spawn(SpawnIter(["Child1", "Child2"].into_iter().map(Name::new))),
            ))
            .id();

        let children = world
            .query::<&Children>()
            .get(&world, parent)
            .expect("An entity with Children should exist");

        assert_eq!(children.iter().count(), 2);

        for ChildOf(child) in world.query::<&ChildOf>().iter(&world) {
            assert_eq!(child, &parent);
        }
    }

    #[test]
    fn spawn_with() {
        let mut world = World::new();

        let parent = world
            .spawn((
                Name::new("Parent"),
                Children::spawn(SpawnWith(|parent: &mut RelatedSpawner<ChildOf>| {
                    parent.spawn(Name::new("Child1"));
                })),
            ))
            .id();

        let children = world
            .query::<&Children>()
            .get(&world, parent)
            .expect("An entity with Children should exist");

        assert_eq!(children.iter().count(), 1);

        for ChildOf(child) in world.query::<&ChildOf>().iter(&world) {
            assert_eq!(child, &parent);
        }
    }

    #[test]
    fn with_related() {
        let mut world = World::new();

        let child1 = world.spawn(Name::new("Child1")).id();
        let child2 = world.spawn(Name::new("Child2")).id();

        let parent = world
            .spawn((
                Name::new("Parent"),
                Children::spawn(WithRelated::new([child1, child2])),
            ))
            .id();

        let children = world
            .query::<&Children>()
            .get(&world, parent)
            .expect("An entity with Children should exist");

        assert_eq!(children.iter().count(), 2);

        assert_eq!(
            world.entity(child1).get::<ChildOf>(),
            Some(&ChildOf(parent))
        );
        assert_eq!(
            world.entity(child2).get::<ChildOf>(),
            Some(&ChildOf(parent))
        );
    }

    #[test]
    fn with_one_related() {
        let mut world = World::new();

        let child1 = world.spawn(Name::new("Child1")).id();

        let parent = world
            .spawn((Name::new("Parent"), Children::spawn(WithOneRelated(child1))))
            .id();

        let children = world
            .query::<&Children>()
            .get(&world, parent)
            .expect("An entity with Children should exist");

        assert_eq!(children.iter().count(), 1);

        assert_eq!(
            world.entity(child1).get::<ChildOf>(),
            Some(&ChildOf(parent))
        );
    }
}