Skip to content
Closed
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: 2 additions & 2 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ impl App {
self
}

/// Inserts the [`!Send`](Send) resource into the app, overwriting any existing resource
/// Inserts the [`NonSendRes`] resource into the app, overwriting any existing resource
/// of the same type.
///
/// There is also an [`init_non_send_resource`](Self::init_non_send_resource) for
Expand All @@ -424,7 +424,7 @@ impl App {
self
}

/// Inserts the [`!Send`](Send) resource into the app if there is no existing instance of `R`.
/// Inserts the [`NonSendRes`] resource into the app if there is no existing instance of `R`.
///
/// `R` must implement [`FromWorld`].
/// If `R` implements [`Default`], [`FromWorld`] will be automatically implemented and
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ impl Plugin for TaskPoolPlugin {
_app.add_systems(Last, tick_global_task_pools);
}
}
/// A dummy type that is [`!Send`](Send), to force systems to run on the main thread.
/// A dummy type that is `!Send`, to force systems to run on the main thread.
pub struct NonSendMarker(PhantomData<*mut ()>);

/// A system used to check and advanced our task pools.
///
/// Calls [`tick_global_task_pools_on_main_thread`],
/// and uses [`NonSendMarker`] to ensure that this system runs on the main thread
#[cfg(not(target_arch = "wasm32"))]
fn tick_global_task_pools(_main_thread_marker: Option<NonSend<NonSendMarker>>) {
fn tick_global_task_pools(_main_thread_marker: Option<NonSendRes<NonSendMarker>>) {
tick_global_task_pools_on_main_thread();
}

Expand Down
32 changes: 18 additions & 14 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub trait DetectChangesMut: DetectChanges {
/// then consider applying a `map_unchanged` beforehand to allow changing only the relevant
/// field and prevent unnecessary copying and cloning.
/// See the docs of [`Mut::map_unchanged`], [`MutUntyped::map_unchanged`],
/// [`ResMut::map_unchanged`] or [`NonSendMut::map_unchanged`] for an example
/// [`ResMut::map_unchanged`] or [`NonSendResMut::map_unchanged`] for an example
///
/// If you need the previous value, use [`replace_if_neq`](DetectChangesMut::replace_if_neq).
///
Expand Down Expand Up @@ -191,7 +191,7 @@ pub trait DetectChangesMut: DetectChanges {
/// then consider applying a [`map_unchanged`](Mut::map_unchanged) beforehand to allow
/// changing only the relevant field and prevent unnecessary copying and cloning.
/// See the docs of [`Mut::map_unchanged`], [`MutUntyped::map_unchanged`],
/// [`ResMut::map_unchanged`] or [`NonSendMut::map_unchanged`] for an example
/// [`ResMut::map_unchanged`] or [`NonSendResMut::map_unchanged`] for an example
///
/// If you don't need the previous value, use [`set_if_neq`](DetectChangesMut::set_if_neq).
///
Expand Down Expand Up @@ -615,21 +615,25 @@ impl<'w, T: Resource> From<ResMut<'w, T>> for Mut<'w, T> {
///
/// Panics when used as a `SystemParameter` if the resource does not exist.
///
/// Use `Option<NonSendMut<T>>` instead if the resource might not always exist.
pub struct NonSendMut<'w, T: ?Sized + 'static> {
/// Use `Option<NonSendResMut<T>>` instead if the resource might not always exist.
pub struct NonSendResMut<'w, T: ?Sized + 'static> {
pub(crate) value: &'w mut T,
pub(crate) ticks: TicksMut<'w>,
}

change_detection_impl!(NonSendMut<'w, T>, T,);
change_detection_mut_impl!(NonSendMut<'w, T>, T,);
impl_methods!(NonSendMut<'w, T>, T,);
impl_debug!(NonSendMut<'w, T>,);
/// See [`NonSendResMut`]
#[deprecated = "Use `NonSendResMut` instead"]
pub type NonSendMut<'w, T> = NonSendResMut<'w, T>;

impl<'w, T: 'static> From<NonSendMut<'w, T>> for Mut<'w, T> {
/// Convert this `NonSendMut` into a `Mut`. This allows keeping the change-detection feature of `Mut`
/// while losing the specificity of `NonSendMut`.
fn from(other: NonSendMut<'w, T>) -> Mut<'w, T> {
change_detection_impl!(NonSendResMut<'w, T>, T,);
change_detection_mut_impl!(NonSendResMut<'w, T>, T,);
impl_methods!(NonSendResMut<'w, T>, T,);
impl_debug!(NonSendResMut<'w, T>,);

impl<'w, T: 'static> From<NonSendResMut<'w, T>> for Mut<'w, T> {
/// Convert this `NonSendResMut` into a `Mut`. This allows keeping the change-detection feature of `Mut`
/// while losing the specificity of `NonSendResMut`.
fn from(other: NonSendResMut<'w, T>) -> Mut<'w, T> {
Mut {
value: other.value,
ticks: other.ticks,
Expand Down Expand Up @@ -1034,7 +1038,7 @@ mod tests {
use crate::{
self as bevy_ecs,
change_detection::{
Mut, NonSendMut, Ref, ResMut, TicksMut, CHECK_TICK_THRESHOLD, MAX_CHANGE_AGE,
Mut, NonSendResMut, Ref, ResMut, TicksMut, CHECK_TICK_THRESHOLD, MAX_CHANGE_AGE,
},
component::{Component, ComponentTicks, Tick},
system::{IntoSystem, Query, System},
Expand Down Expand Up @@ -1205,7 +1209,7 @@ mod tests {
this_run: Tick::new(4),
};
let mut res = R {};
let non_send_mut = NonSendMut {
let non_send_mut = NonSendResMut {
value: &mut res,
ticks,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,7 @@ impl Components {
}
}

/// Initializes a [non-send resource](crate::system::NonSend) of type `T` with this instance.
/// Initializes a [non-send resource](crate::system::NonSendRes) of type `T` with this instance.
/// If a resource of this type has already been initialized, this will return
/// the ID of the pre-existing resource.
#[inline]
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub mod prelude {
IntoSystemSetConfigs, Schedule, Schedules, SystemSet,
},
system::{
Commands, Deferred, In, IntoSystem, Local, NonSend, NonSendMut, ParallelCommands,
Commands, Deferred, In, IntoSystem, Local, NonSendRes, NonSendResMut, ParallelCommands,
ParamSet, Query, ReadOnlySystem, Res, ResMut, Resource, System, SystemBuilder,
SystemParamFunction,
},
Expand Down Expand Up @@ -98,7 +98,7 @@ mod tests {

#[allow(dead_code)]
#[derive(Default)]
struct NonSendA(usize, PhantomData<*mut ()>);
struct NonSendResA(usize, PhantomData<*mut ()>);

#[derive(Component, Clone, Debug)]
struct DropCk(Arc<AtomicUsize>);
Expand Down Expand Up @@ -1430,11 +1430,11 @@ mod tests {

#[test]
#[should_panic(
expected = "Attempted to access or drop non-send resource bevy_ecs::tests::NonSendA from thread"
expected = "Attempted to access or drop non-send resource bevy_ecs::tests::NonSendResA from thread"
)]
fn non_send_resource_drop_from_different_thread() {
let mut world = World::default();
world.insert_non_send_resource(NonSendA::default());
world.insert_non_send_resource(NonSendResA::default());

let thread = std::thread::spawn(move || {
// Dropping the non-send resource on a different thread
Expand All @@ -1450,7 +1450,7 @@ mod tests {
#[test]
fn non_send_resource_drop_from_same_thread() {
let mut world = World::default();
world.insert_non_send_resource(NonSendA::default());
world.insert_non_send_resource(NonSendResA::default());
drop(world);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/schedule/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn new_condition<M>(condition: impl Condition<M>) -> BoxedCondition {
let condition_system = IntoSystem::into_system(condition);
assert!(
condition_system.is_send(),
"Condition `{}` accesses `NonSend` resources. This is not currently supported.",
"Condition `{}` accesses `NonSendRes` resources. This is not currently supported.",
condition_system.name()
);

Expand Down
16 changes: 8 additions & 8 deletions crates/bevy_ecs/src/schedule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,8 @@ mod tests {
fn empty_system() {}
fn res_system(_res: Res<R>) {}
fn resmut_system(_res: ResMut<R>) {}
fn nonsend_system(_ns: NonSend<R>) {}
fn nonsendmut_system(_ns: NonSendMut<R>) {}
fn nonsendres_system(_ns: NonSendRes<R>) {}
fn nonsendresmut_system(_ns: NonSendResMut<R>) {}
fn read_component_system(_query: Query<&A>) {}
fn write_component_system(_query: Query<&mut A>) {}
fn with_filtered_component_system(_query: Query<&mut A, With<B>>) {}
Expand All @@ -763,7 +763,7 @@ mod tests {

let mut schedule = Schedule::default();
schedule
// nonsendmut system deliberately conflicts with resmut system
// nonsendresmut system deliberately conflicts with resmut system
.add_systems((resmut_system, write_component_system, event_writer_system));

let _ = schedule.initialize(&mut world);
Expand All @@ -784,8 +784,8 @@ mod tests {
empty_system,
res_system,
res_system,
nonsend_system,
nonsend_system,
nonsendres_system,
nonsendres_system,
read_component_system,
read_component_system,
event_reader_system,
Expand Down Expand Up @@ -838,7 +838,7 @@ mod tests {
world.insert_resource(R);

let mut schedule = Schedule::default();
schedule.add_systems((nonsendmut_system, nonsend_system));
schedule.add_systems((nonsendresmut_system, nonsendres_system));

let _ = schedule.initialize(&mut world);

Expand Down Expand Up @@ -940,7 +940,7 @@ mod tests {
schedule.add_systems((
resmut_system.ambiguous_with_all(),
res_system,
nonsend_system,
nonsendres_system,
));

let _ = schedule.initialize(&mut world);
Expand All @@ -960,7 +960,7 @@ mod tests {
schedule.add_systems((
resmut_system.ambiguous_with(IgnoreMe),
res_system.in_set(IgnoreMe),
nonsend_system.in_set(IgnoreMe),
nonsendres_system.in_set(IgnoreMe),
));

let _ = schedule.initialize(&mut world);
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ pub struct Storages {
pub tables: Tables,
/// Backing storage for resources.
pub resources: Resources<true>,
/// Backing storage for `!Send` resources.
/// Backing storage for [`NonSendRes`] resources.
pub non_send_resources: Resources<false>,
}
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/storage/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<const SEND: bool> ResourceData<SEND> {
/// The only row in the underlying `BlobVec`.
const ROW: usize = 0;

/// Validates the access to `!Send` resources is only done on the thread they were created from.
/// Validates the access to [`NonSendRes`] resources is only done on the thread they were created from.
///
/// # Panics
/// If `SEND` is false, this will panic if called from a different thread than the one it was inserted from.
Expand Down
18 changes: 9 additions & 9 deletions crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@
//! - [`Local`]
//! - [`EventReader`](crate::event::EventReader)
//! - [`EventWriter`](crate::event::EventWriter)
//! - [`NonSend`] and `Option<NonSend>`
//! - [`NonSendMut`] and `Option<NonSendMut>`
//! - [`NonSendRes`] and `Option<NonSendRes>`
//! - [`NonSendResMut`] and `Option<NonSendResMut>`
//! - [`RemovedComponents`](crate::removal_detection::RemovedComponents)
//! - [`SystemName`]
//! - [`SystemChangeTick`]
Expand Down Expand Up @@ -359,8 +359,8 @@ mod tests {
Schedule,
},
system::{
Commands, In, IntoSystem, Local, NonSend, NonSendMut, ParamSet, Query, Res, ResMut,
Resource, StaticSystemParam, System, SystemState,
Commands, In, IntoSystem, Local, NonSendRes, NonSendResMut, ParamSet, Query, Res,
ResMut, Resource, StaticSystemParam, System, SystemState,
},
world::{FromWorld, World},
};
Expand Down Expand Up @@ -859,11 +859,11 @@ mod tests {
world.insert_non_send_resource(NotSend1(std::rc::Rc::new(0)));

fn sys(
op: Option<NonSend<NotSend1>>,
mut _op2: Option<NonSendMut<NotSend2>>,
op: Option<NonSendRes<NotSend1>>,
mut _op2: Option<NonSendResMut<NotSend2>>,
mut system_ran: ResMut<SystemRan>,
) {
op.expect("NonSend should exist");
op.expect("NonSendRes should exist");
*system_ran = SystemRan::Yes;
}

Expand All @@ -886,8 +886,8 @@ mod tests {
world.insert_non_send_resource(NotSend2(std::rc::Rc::new(2)));

fn sys(
_op: NonSend<NotSend1>,
mut _op2: NonSendMut<NotSend2>,
_op: NonSendRes<NotSend1>,
mut _op2: NonSendResMut<NotSend2>,
mut system_ran: ResMut<SystemRan>,
) {
*system_ran = SystemRan::Yes;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/system/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ mod tests {

#[test]
fn non_send_resources() {
fn non_send_count_down(mut ns: NonSendMut<Counter>) {
fn non_send_count_down(mut ns: NonSendResMut<Counter>) {
ns.0 -= 1;
}

Expand Down
Loading