diff --git a/crates/bevy_ecs/src/schedule/executor/multi_threaded.rs b/crates/bevy_ecs/src/schedule/executor/multi_threaded.rs index 80c6f27d2e3d6..0606e7fbdc799 100644 --- a/crates/bevy_ecs/src/schedule/executor/multi_threaded.rs +++ b/crates/bevy_ecs/src/schedule/executor/multi_threaded.rs @@ -216,10 +216,8 @@ impl SystemExecutor for MultiThreadedExecutor { if self.apply_final_buffers { // Do one final apply buffers after all systems have completed - // SAFETY: all systems have completed, and so no outstanding accesses remain - let world = unsafe { &mut *world.get() }; // Commands should be applied while on the scope's thread, not the executor's thread - apply_system_buffers(&self.unapplied_systems, systems, world); + apply_system_buffers(&self.unapplied_systems, systems, world.get_mut()); self.unapplied_systems.clear(); debug_assert!(self.unapplied_systems.is_clear()); } diff --git a/crates/bevy_utils/src/syncunsafecell.rs b/crates/bevy_utils/src/syncunsafecell.rs index 07e2422d89a8a..839bf7b9d1a55 100644 --- a/crates/bevy_utils/src/syncunsafecell.rs +++ b/crates/bevy_utils/src/syncunsafecell.rs @@ -76,11 +76,13 @@ impl SyncUnsafeCell { } #[inline] - /// Returns a `&SyncUnsafeCell` from a `&mut T`. - pub fn from_mut(t: &mut T) -> &SyncUnsafeCell { - // SAFETY: `&mut` ensures unique access, and `UnsafeCell` and `SyncUnsafeCell` - // have #[repr(transparent)] - unsafe { &*(t as *mut T as *const SyncUnsafeCell) } + /// Returns a `&mut SyncUnsafeCell` from a `&mut T`. + pub fn from_mut(t: &mut T) -> &mut SyncUnsafeCell { + let ptr = t as *mut T as *mut SyncUnsafeCell; + // SAFETY: `ptr` must be safe to mutably dereference, since it was originally + // obtained from a mutable reference. `SyncUnsafeCell` has the same representation + // as the original type `T`, since the former is annotated with #[repr(transparent)]. + unsafe { &mut *ptr } } }