Skip to content

Commit 4f04a57

Browse files
use InteriorMutableWorld for System and ParamState
1 parent 588fa82 commit 4f04a57

File tree

12 files changed

+92
-68
lines changed

12 files changed

+92
-68
lines changed

crates/bevy_ecs/macros/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ pub fn impl_param_set(_input: TokenStream) -> TokenStream {
291291
unsafe fn get_param(
292292
state: &'s mut Self,
293293
system_meta: &SystemMeta,
294-
world: &'w World,
294+
world: InteriorMutableWorld<'w>,
295295
change_tick: u32,
296296
) -> Self::Item {
297297
ParamSet {
@@ -460,7 +460,7 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
460460
unsafe fn get_param(
461461
state: &'s mut Self,
462462
system_meta: &#path::system::SystemMeta,
463-
world: &'w #path::world::World,
463+
world: #path::world::interior_mutable_world::InteriorMutableWorld<'w>,
464464
change_tick: u32,
465465
) -> Self::Item {
466466
#struct_name {

crates/bevy_ecs/src/schedule/executor_parallel.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
archetype::ArchetypeComponentId,
33
query::Access,
44
schedule::{ParallelSystemExecutor, SystemContainer},
5-
world::World,
5+
world::{interior_mutable_world::InteriorMutableWorld, World},
66
};
77
use async_channel::{Receiver, Sender};
88
use bevy_tasks::{ComputeTaskPool, Scope, TaskPool};
@@ -124,6 +124,8 @@ impl ParallelSystemExecutor for ParallelExecutor {
124124
}
125125
}
126126

127+
let world = world.as_interior_mutable();
128+
127129
ComputeTaskPool::init(TaskPool::default).scope(|scope| {
128130
self.prepare_systems(scope, systems, world);
129131
if self.should_run.count_ones(..) == 0 {
@@ -168,7 +170,7 @@ impl ParallelExecutor {
168170
&mut self,
169171
scope: &Scope<'_, 'scope, ()>,
170172
systems: &'scope mut [SystemContainer],
171-
world: &'scope World,
173+
world: InteriorMutableWorld<'scope>,
172174
) {
173175
// These are used as a part of a unit test.
174176
#[cfg(test)]

crates/bevy_ecs/src/system/commands/parallel_scope.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
entity::Entities,
77
prelude::World,
88
system::{SystemParam, SystemParamFetch, SystemParamState},
9+
world::interior_mutable_world::InteriorMutableWorld,
910
};
1011

1112
use super::{CommandQueue, Commands};
@@ -58,7 +59,7 @@ impl<'w, 's> SystemParamFetch<'w, 's> for ParallelCommandsState {
5859
unsafe fn get_param(
5960
state: &'s mut Self,
6061
_: &crate::system::SystemMeta,
61-
world: &'w World,
62+
world: InteriorMutableWorld<'w>,
6263
_: u32,
6364
) -> Self::Item {
6465
ParallelCommands {

crates/bevy_ecs/src/system/exclusive_function_system.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
ExclusiveSystemParamItem, ExclusiveSystemParamState, IntoSystem, System, SystemMeta,
1010
SystemTypeIdLabel,
1111
},
12-
world::{World, WorldId},
12+
world::{interior_mutable_world::InteriorMutableWorld, World, WorldId},
1313
};
1414
use bevy_ecs_macros::all_tuples;
1515
use std::{borrow::Cow, marker::PhantomData};
@@ -87,7 +87,11 @@ where
8787
}
8888

8989
#[inline]
90-
unsafe fn run_unsafe(&mut self, _input: Self::In, _world: &World) -> Self::Out {
90+
unsafe fn run_unsafe(
91+
&mut self,
92+
_input: Self::In,
93+
_world: InteriorMutableWorld<'_>,
94+
) -> Self::Out {
9195
panic!("Cannot run exclusive systems with a shared World reference");
9296
}
9397

crates/bevy_ecs/src/system/function_system.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
check_system_change_tick, ReadOnlySystemParamFetch, System, SystemParam, SystemParamFetch,
1010
SystemParamItem, SystemParamState,
1111
},
12-
world::{World, WorldId},
12+
world::{interior_mutable_world::InteriorMutableWorld, World, WorldId},
1313
};
1414
use bevy_ecs_macros::all_tuples;
1515
use std::{borrow::Cow, fmt::Debug, marker::PhantomData};
@@ -169,7 +169,7 @@ impl<Param: SystemParam> SystemState<Param> {
169169
{
170170
self.validate_world_and_update_archetypes(world);
171171
// SAFETY: Param is read-only and doesn't allow mutable access to World. It also matches the World this SystemState was created with.
172-
unsafe { self.get_unchecked_manual(world) }
172+
unsafe { self.get_unchecked_manual(world.as_interior_mutable_readonly()) }
173173
}
174174

175175
/// Retrieve the mutable [`SystemParam`] values.
@@ -180,7 +180,7 @@ impl<Param: SystemParam> SystemState<Param> {
180180
) -> <Param::Fetch as SystemParamFetch<'w, 's>>::Item {
181181
self.validate_world_and_update_archetypes(world);
182182
// SAFETY: World is uniquely borrowed and matches the World this SystemState was created with.
183-
unsafe { self.get_unchecked_manual(world) }
183+
unsafe { self.get_unchecked_manual(world.as_interior_mutable()) }
184184
}
185185

186186
/// Applies all state queued up for [`SystemParam`] values. For example, this will apply commands queued up
@@ -220,7 +220,7 @@ impl<Param: SystemParam> SystemState<Param> {
220220
#[inline]
221221
pub unsafe fn get_unchecked_manual<'w, 's>(
222222
&'s mut self,
223-
world: &'w World,
223+
world: InteriorMutableWorld<'w>,
224224
) -> <Param::Fetch as SystemParamFetch<'w, 's>>::Item {
225225
let change_tick = world.increment_change_tick();
226226
let param = <Param::Fetch as SystemParamFetch>::get_param(
@@ -393,7 +393,7 @@ where
393393
}
394394

395395
#[inline]
396-
unsafe fn run_unsafe(&mut self, input: Self::In, world: &World) -> Self::Out {
396+
unsafe fn run_unsafe(&mut self, input: Self::In, world: InteriorMutableWorld<'_>) -> Self::Out {
397397
let change_tick = world.increment_change_tick();
398398

399399
// Safety:

crates/bevy_ecs/src/system/system.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ use bevy_utils::tracing::warn;
22
use core::fmt::Debug;
33

44
use crate::{
5-
archetype::ArchetypeComponentId, change_detection::MAX_CHANGE_AGE, component::ComponentId,
6-
query::Access, schedule::SystemLabelId, world::World,
5+
archetype::ArchetypeComponentId,
6+
change_detection::MAX_CHANGE_AGE,
7+
component::ComponentId,
8+
query::Access,
9+
schedule::SystemLabelId,
10+
world::{interior_mutable_world::InteriorMutableWorld, World},
711
};
812
use std::borrow::Cow;
913

@@ -42,17 +46,14 @@ pub trait System: Send + Sync + 'static {
4246
///
4347
/// # Safety
4448
///
45-
/// This might access world and resources in an unsafe manner. This should only be called in one
46-
/// of the following contexts:
47-
/// 1. This system is the only system running on the given world across all threads.
48-
/// 2. This system only runs in parallel with other systems that do not conflict with the
49-
/// [`System::archetype_component_access()`].
50-
unsafe fn run_unsafe(&mut self, input: Self::In, world: &World) -> Self::Out;
49+
/// This function may only be called with a [`InteriorMutableWorld`] that can be used for
50+
/// everything listed in [`System::archetype_component_access`].
51+
unsafe fn run_unsafe(&mut self, input: Self::In, world: InteriorMutableWorld<'_>) -> Self::Out;
5152
/// Runs the system with the given input in the world.
5253
fn run(&mut self, input: Self::In, world: &mut World) -> Self::Out {
5354
self.update_archetype_component_access(world);
5455
// SAFETY: world and resources are exclusively borrowed
55-
unsafe { self.run_unsafe(input, world) }
56+
unsafe { self.run_unsafe(input, world.as_interior_mutable()) }
5657
}
5758
fn apply_buffers(&mut self, world: &mut World);
5859
/// Initialize the system.

0 commit comments

Comments
 (0)