bevy/crates
JaySpruce c64f628bfb Fix sparse set components ignoring insert_if_new/InsertMode (#19059)
# Objective

I've been tinkering with ECS insertion/removal lately, and noticed that
sparse sets just... don't interact with `InsertMode` at all. Sure
enough, using `insert_if_new` with a sparse component does the same
thing as `insert`.

# Solution

- Add a check in `BundleInfo::write_components` to drop the new value if
the entity already has the component and `InsertMode` is `Keep`.
- Add necessary methods to sparse set internals to fetch the drop
function.

# Testing

Minimal reproduction:
<details>
<summary>Code</summary>

```
use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .add_systems(PostStartup, component_print)
        .run();
}

#[derive(Component)]
#[component(storage = "SparseSet")]
struct SparseComponent(u32);

fn setup(mut commands: Commands) {
    let mut entity = commands.spawn_empty();
    entity.insert(SparseComponent(1));
    entity.insert(SparseComponent(2));

    let mut entity = commands.spawn_empty();
    entity.insert(SparseComponent(3));
    entity.insert_if_new(SparseComponent(4));
}

fn component_print(query: Query<&SparseComponent>) {
    for component in &query {
        info!("{}", component.0);
    }
}
```

</details>

Here it is on Bevy Playground (0.15.3): 

https://learnbevy.com/playground?share=2a96a68a81e804d3fdd644a833c1d51f7fa8dd33fc6192fbfd077b082a6b1a41

Output on `main`:
```
2025-05-04T17:50:50.401328Z  INFO system{name="fork::component_print"}: fork: 2
2025-05-04T17:50:50.401583Z  INFO system{name="fork::component_print"}: fork: 4
```

Output with this PR :
```
2025-05-04T17:51:33.461835Z  INFO system{name="fork::component_print"}: fork: 2
2025-05-04T17:51:33.462091Z  INFO system{name="fork::component_print"}: fork: 3
```
2025-05-30 22:59:09 +02:00
..
bevy_a11y Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_animation Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_app Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_asset Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_audio Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_color Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_core_pipeline Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_derive Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_dev_tools Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_diagnostic Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_dylib Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_ecs Fix sparse set components ignoring insert_if_new/InsertMode (#19059) 2025-05-30 22:59:09 +02:00
bevy_encase_derive Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_gilrs Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_gizmos Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_gltf Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_image Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_input Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_input_focus Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_internal Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_log Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_macro_utils Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_math Fix rotate_by implementation for Aabb2d (#19015) 2025-05-30 22:59:09 +02:00
bevy_mesh Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_mikktspace Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_pbr Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_picking Removed conversion from pointer physical coordinates to viewport local coordinates in bevy_picking make_ray function (#18870) 2025-05-30 22:59:09 +02:00
bevy_platform Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_ptr Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_reflect bevy_reflect: Re-reflect hashbrown types (#18944) 2025-05-30 22:59:09 +02:00
bevy_remote Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_render Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_scene Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_sprite Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_state Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_tasks Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_text Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_time Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_transform Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_ui Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_utils Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_window Release 0.16.0 2025-04-21 23:14:00 +02:00
bevy_winit Release 0.16.0 2025-04-21 23:14:00 +02:00