bevy/crates/bevy_ecs/src/world
JoJoJet 85a918a8dd
Improve safety for the multi-threaded executor using UnsafeWorldCell (#8292)
# Objective

Fix #7833.

Safety comments in the multi-threaded executor don't really talk about
system world accesses, which makes it unclear if the code is actually
valid.

## Solution

Update the `System` trait to use `UnsafeWorldCell`. This type's API is
written in a way that makes it much easier to cleanly maintain safety
invariants. Use this type throughout the multi-threaded executor, with a
liberal use of safety comments.

---

## Migration Guide

The `System` trait now uses `UnsafeWorldCell` instead of `&World`. This
type provides a robust API for interior mutable world access.
- The method `run_unsafe` uses this type to manage world mutations
across multiple threads.
- The method `update_archetype_component_access` uses this type to
ensure that only world metadata can be used.

```rust
let mut system = IntoSystem::into_system(my_system);
system.initialize(&mut world);

// Before:
system.update_archetype_component_access(&world);
unsafe { system.run_unsafe(&world) }

// After:
system.update_archetype_component_access(world.as_unsafe_world_cell_readonly());
unsafe { system.run_unsafe(world.as_unsafe_world_cell()) }
```

---------

Co-authored-by: James Liu <contact@jamessliu.com>
2023-05-29 15:22:10 +00:00
..
entity_ref.rs bevy_ecs: add untyped methods for inserting components and bundles (#7204) 2023-03-21 00:33:11 +00:00
error.rs Add World::try_run_schedule (#8028) 2023-03-17 01:22:54 +00:00
identifier.rs Improve safety for the multi-threaded executor using UnsafeWorldCell (#8292) 2023-05-29 15:22:10 +00:00
mod.rs Add documentation to UnsafeWorldCell::increment_change_tick (#8697) 2023-05-28 12:46:24 +00:00
spawn_batch.rs Increase type safety and clarity for change detection (#7905) 2023-03-09 17:17:02 +00:00
unsafe_world_cell.rs Improve safety for the multi-threaded executor using UnsafeWorldCell (#8292) 2023-05-29 15:22:10 +00:00
world_cell.rs Move all logic to UnsafeWorldCell (#7381) 2023-02-06 19:02:52 +00:00