Remove some superfluous unsafe code (#3297)
# Objective - This `unsafe` is weird ## Solution - Don't use `unsafe` here Hopefully this isn't already in an open PR.
This commit is contained in:
parent
d8abfdbc6a
commit
0ee4195fb0
@ -175,7 +175,7 @@ impl ParallelExecutor {
|
|||||||
fn prepare_systems<'scope>(
|
fn prepare_systems<'scope>(
|
||||||
&mut self,
|
&mut self,
|
||||||
scope: &mut Scope<'scope, ()>,
|
scope: &mut Scope<'scope, ()>,
|
||||||
systems: &'scope [ParallelSystemContainer],
|
systems: &'scope mut [ParallelSystemContainer],
|
||||||
world: &'scope World,
|
world: &'scope World,
|
||||||
) {
|
) {
|
||||||
#[cfg(feature = "trace")]
|
#[cfg(feature = "trace")]
|
||||||
@ -183,13 +183,15 @@ impl ParallelExecutor {
|
|||||||
#[cfg(feature = "trace")]
|
#[cfg(feature = "trace")]
|
||||||
let _guard = span.enter();
|
let _guard = span.enter();
|
||||||
self.should_run.clear();
|
self.should_run.clear();
|
||||||
for (index, system_data) in self.system_metadata.iter_mut().enumerate() {
|
for (index, (system_data, system)) in
|
||||||
|
self.system_metadata.iter_mut().zip(systems).enumerate()
|
||||||
|
{
|
||||||
// Spawn the system task.
|
// Spawn the system task.
|
||||||
if systems[index].should_run() {
|
if system.should_run() {
|
||||||
self.should_run.set(index, true);
|
self.should_run.set(index, true);
|
||||||
let start_receiver = system_data.start_receiver.clone();
|
let start_receiver = system_data.start_receiver.clone();
|
||||||
let finish_sender = self.finish_sender.clone();
|
let finish_sender = self.finish_sender.clone();
|
||||||
let system = unsafe { systems[index].system_mut_unsafe() };
|
let system = system.system_mut();
|
||||||
#[cfg(feature = "trace")] // NB: outside the task to get the TLS current span
|
#[cfg(feature = "trace")] // NB: outside the task to get the TLS current span
|
||||||
let system_span = bevy_utils::tracing::info_span!("system", name = &*system.name());
|
let system_span = bevy_utils::tracing::info_span!("system", name = &*system.name());
|
||||||
let task = async move {
|
let task = async move {
|
||||||
|
|||||||
@ -7,7 +7,7 @@ use crate::{
|
|||||||
},
|
},
|
||||||
system::{ExclusiveSystem, System},
|
system::{ExclusiveSystem, System},
|
||||||
};
|
};
|
||||||
use std::{borrow::Cow, cell::UnsafeCell};
|
use std::borrow::Cow;
|
||||||
|
|
||||||
/// System metadata like its name, labels, order requirements and component access.
|
/// System metadata like its name, labels, order requirements and component access.
|
||||||
pub trait SystemContainer: GraphNode<Label = BoxedSystemLabel> {
|
pub trait SystemContainer: GraphNode<Label = BoxedSystemLabel> {
|
||||||
@ -106,7 +106,7 @@ impl SystemContainer for ExclusiveSystemContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct ParallelSystemContainer {
|
pub struct ParallelSystemContainer {
|
||||||
system: Box<UnsafeCell<dyn System<In = (), Out = ()>>>,
|
system: Box<dyn System<In = (), Out = ()>>,
|
||||||
pub(crate) run_criteria_index: Option<usize>,
|
pub(crate) run_criteria_index: Option<usize>,
|
||||||
pub(crate) run_criteria_label: Option<BoxedRunCriteriaLabel>,
|
pub(crate) run_criteria_label: Option<BoxedRunCriteriaLabel>,
|
||||||
pub(crate) should_run: bool,
|
pub(crate) should_run: bool,
|
||||||
@ -123,8 +123,7 @@ unsafe impl Sync for ParallelSystemContainer {}
|
|||||||
impl ParallelSystemContainer {
|
impl ParallelSystemContainer {
|
||||||
pub(crate) fn from_descriptor(descriptor: ParallelSystemDescriptor) -> Self {
|
pub(crate) fn from_descriptor(descriptor: ParallelSystemDescriptor) -> Self {
|
||||||
ParallelSystemContainer {
|
ParallelSystemContainer {
|
||||||
// SAFE: it is fine to wrap inner value with UnsafeCell, as it is repr(transparent)
|
system: descriptor.system,
|
||||||
system: unsafe { Box::from_raw(Box::into_raw(descriptor.system) as *mut _) },
|
|
||||||
should_run: false,
|
should_run: false,
|
||||||
run_criteria_index: None,
|
run_criteria_index: None,
|
||||||
run_criteria_label: None,
|
run_criteria_label: None,
|
||||||
@ -141,19 +140,11 @@ impl ParallelSystemContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn system(&self) -> &dyn System<In = (), Out = ()> {
|
pub fn system(&self) -> &dyn System<In = (), Out = ()> {
|
||||||
// SAFE: statically enforced shared access
|
&*self.system
|
||||||
unsafe { self.system.get().as_ref().unwrap() }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn system_mut(&mut self) -> &mut dyn System<In = (), Out = ()> {
|
pub fn system_mut(&mut self) -> &mut dyn System<In = (), Out = ()> {
|
||||||
self.system.get_mut()
|
&mut *self.system
|
||||||
}
|
|
||||||
|
|
||||||
/// # Safety
|
|
||||||
/// Ensure no other borrows exist along with this one.
|
|
||||||
#[allow(clippy::mut_from_ref)]
|
|
||||||
pub unsafe fn system_mut_unsafe(&self) -> &mut dyn System<In = (), Out = ()> {
|
|
||||||
self.system.get().as_mut().unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn should_run(&self) -> bool {
|
pub fn should_run(&self) -> bool {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user