bevy/crates
Corvus a312170749
Increase upper limit of children! (#18865)
# Objective

Currently, `bevy_ecs`'s `children!` macro only supports spawning up to
twelve children at once. Ideally there would be no limit.

## Solution

`children!` is limited because `SpawnableList`, [the primary trait bound
here](https://docs.rs/bevy/0.16.0-rc.5/bevy/ecs/hierarchy/struct.Children.html#method.spawn),
uses the fake variadics pattern on tuples of up to twelve elements.
However, since a tuple itself implements `SpawnableList`, we can simply
nest tuples of entities when we run out of room.

This PR achieves this using `macro_rules` macros with a bit of brute
force, following [some discussion on
Discord](https://discord.com/channels/691052431525675048/692572690833473578/1362174415458013314).
If we create patterns for lists of up to eleven bundles, then use a
repetition pattern to handle the rest, we can "special-case" the
recursion into a nested tuple.

In principle, this would permit an arbitrary number of children, but
Rust's recursion limits will cut things short at around 1400 elements by
default. Of course, it's generally not a good idea to stick that many
bundles in a single invocation, but it might be worth mentioning in the
docs.

## Implementation notes

### Why are cases 0-11 expanded by hand?

We could make use of a tertiary macro:

```rs
macro_rules! recursive_spawn {
    // so that this...
    ($a:expr, $b:expr) => {
        (
            $crate::spawn::Spawn($a),
            $crate::spawn::Spawn($b),
        )
    };
    
    // becomes this...
    ($a:expr, $b:expr) => {
        $crate::spawn_tuple!($a, $b)
    };
}
```

But I already feel a little bad exporting `recursive_spawn`. I'd really
like to avoid exposing more internals, even if they are annotated with
`#[doc(hidden)]`. If I had to guess, I'd say it'll also make the
expansion a tiny bit slower.

### Do we really need to handle up to twelve elements in the macro?

The macro is a little long, but doing it this way maximizes the
"flatness" of the types to be spawned. This should improve the codegen a
bit and makes the macro output a little bit easier to look at.

## Future work

The `related!` macro is essentially the same as `children!`, so if this
direction is accepted, `related!` should receive the same treatment. I
imagine we'd want to extract out the `recursive_spawn` macro into its
own file since it can be used for both. If this should be tackled in
this PR, let me know!

## Testing

This change is fairly trivial, but I added a single test to verify that
it compiles and nothing goes wrong once recursion starts happening. It's
pretty easy to verify that the change works in practice -- just spawn
over twelve entities as children at once!
2025-05-06 00:58:30 +00:00
..
bevy_a11y Update accesskit and accesskit_winit requirements (#18285) 2025-03-25 04:04:28 +00:00
bevy_animation Revert "Allow partial support for bevy_log in no_std (#18782)" (#18816) 2025-04-14 21:15:01 +00:00
bevy_anti_aliasing Remove Image::from_buffer name argument (only present in debug "dds" builds) (#18538) 2025-03-25 19:25:01 +00:00
bevy_app Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00
bevy_asset ignore files starting with . when loading folders (#11214) 2025-05-05 22:42:01 +00:00
bevy_audio Audio sink seek adopted (#18971) 2025-05-03 11:29:38 +00:00
bevy_color Implemented Alpha for f32. (#18653) 2025-05-06 00:00:17 +00:00
bevy_core_pipeline Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00
bevy_derive Link iOS example with rustc, and avoid C trampoline (#14780) 2025-03-17 21:14:07 +00:00
bevy_dev_tools
bevy_diagnostic Update sysinfo version to 0.35.0 (#19028) 2025-05-05 05:49:23 +00:00
bevy_dylib don't disable std in bevy_dylib (#18807) 2025-04-11 18:44:53 +00:00
bevy_ecs Increase upper limit of children! (#18865) 2025-05-06 00:58:30 +00:00
bevy_encase_derive
bevy_gilrs Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00
bevy_gizmos Rename EntityBorrow/TrustedEntityBorrow to ContainsEntity/EntityEquivalent (#18470) 2025-03-30 06:04:26 +00:00
bevy_gltf Revert "Allow partial support for bevy_log in no_std (#18782)" (#18816) 2025-04-14 21:15:01 +00:00
bevy_image Update ktx2 to 0.4.0 (#19073) 2025-05-05 16:42:36 +00:00
bevy_input Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00
bevy_input_focus Switch ChildOf back to tuple struct (#18672) 2025-04-02 00:10:10 +00:00
bevy_internal Revert "Allow partial support for bevy_log in no_std (#18782)" (#18816) 2025-04-14 21:15:01 +00:00
bevy_log feat(log): support customizing default log formatting (#17722) 2025-05-05 23:01:06 +00:00
bevy_macro_utils Fully qualify crate paths in BevyManifest (#18938) 2025-04-28 21:43:48 +00:00
bevy_math Fix rotate_by implementation for Aabb2d (#19015) 2025-05-04 13:05:27 +00:00
bevy_mesh Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00
bevy_mikktspace fix new nightly lint on mikktspace (#18988) 2025-04-30 05:19:01 +00:00
bevy_pbr Swap order of eviction/extraction when extracting for specialization (#18846) 2025-04-15 06:44:01 +00:00
bevy_picking Removed conversion from pointer physical coordinates to viewport local coordinates in bevy_picking make_ray function (#18870) 2025-04-27 13:54:28 +00:00
bevy_platform Create bevy_platform::cfg for viral feature management (#18822) 2025-05-06 00:52:15 +00:00
bevy_ptr
bevy_reflect Add NonNilUuid support to bevy_reflect (#18604) 2025-05-04 08:22:57 +00:00
bevy_remote Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00
bevy_render Derive clone_behavior for Components (#18811) 2025-05-06 00:32:59 +00:00
bevy_scene Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00
bevy_sprite Sprite picking docs fix (#19016) 2025-05-05 17:45:14 +00:00
bevy_state Rename StateScoped to DespawnOnExitState and add DespawnOnEnterState (#18818) 2025-05-06 00:37:04 +00:00
bevy_tasks Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00
bevy_text Text background colors (#18892) 2025-05-04 08:18:46 +00:00
bevy_time Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00
bevy_transform Revert "Allow partial support for bevy_log in no_std (#18782)" (#18816) 2025-04-14 21:15:01 +00:00
bevy_ui Add a viewport UI widget (#17253) 2025-05-05 22:57:37 +00:00
bevy_utils Rename bevy_platform_support to bevy_platform (#18813) 2025-04-11 23:13:28 +00:00
bevy_window Expose deferred screen edges setting for ios devices (#18729) 2025-04-30 21:24:53 +00:00
bevy_winit Expose deferred screen edges setting for ios devices (#18729) 2025-04-30 21:24:53 +00:00