Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace some unsafe system executor code with safe code #8274

Merged
merged 4 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions crates/bevy_ecs/src/schedule/executor/multi_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
12 changes: 7 additions & 5 deletions crates/bevy_utils/src/syncunsafecell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,13 @@ impl<T: ?Sized> SyncUnsafeCell<T> {
}

#[inline]
/// Returns a `&SyncUnsafeCell<T>` from a `&mut T`.
pub fn from_mut(t: &mut T) -> &SyncUnsafeCell<T> {
// SAFETY: `&mut` ensures unique access, and `UnsafeCell<T>` and `SyncUnsafeCell<T>`
// have #[repr(transparent)]
unsafe { &*(t as *mut T as *const SyncUnsafeCell<T>) }
/// Returns a `&mut SyncUnsafeCell<T>` from a `&mut T`.
pub fn from_mut(t: &mut T) -> &mut SyncUnsafeCell<T> {
let ptr = t as *mut T as *mut SyncUnsafeCell<T>;
// 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 }
}
}

Expand Down