From 9fea77e04fadc6324a4421c35343ae656c839b44 Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Fri, 20 Jan 2023 12:32:42 -0500 Subject: [PATCH 01/10] deprecate `ChangeTrackers` --- crates/bevy_ecs/src/query/fetch.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index 5733ffd262829..d52cd76a627dd 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -1115,6 +1115,7 @@ unsafe impl ReadOnlyWorldQuery for Option {} /// } /// # bevy_ecs::system::assert_is_system(print_moving_objects_system); /// ``` +#[deprecated = "Use `bevy_ecs::Ref` instead"] pub struct ChangeTrackers { pub(crate) component_ticks: ComponentTicks, pub(crate) last_change_tick: u32, @@ -1122,18 +1123,17 @@ pub struct ChangeTrackers { marker: PhantomData, } +#[allow(deprecated)] impl Clone for ChangeTrackers { fn clone(&self) -> Self { - Self { - component_ticks: self.component_ticks, - last_change_tick: self.last_change_tick, - change_tick: self.change_tick, - marker: PhantomData, - } + *self } } + +#[allow(deprecated)] impl Copy for ChangeTrackers {} +#[allow(deprecated)] impl std::fmt::Debug for ChangeTrackers { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("ChangeTrackers") @@ -1144,6 +1144,7 @@ impl std::fmt::Debug for ChangeTrackers { } } +#[allow(deprecated)] impl ChangeTrackers { /// Returns true if this component has been added since the last execution of this system. pub fn is_added(&self) -> bool { @@ -1172,6 +1173,7 @@ pub struct ChangeTrackersFetch<'w, T> { } // SAFETY: `ROQueryFetch` is the same as `QueryFetch` +#[allow(deprecated)] unsafe impl WorldQuery for ChangeTrackers { type Fetch<'w> = ChangeTrackersFetch<'w, T>; type Item<'w> = ChangeTrackers; @@ -1318,6 +1320,7 @@ unsafe impl WorldQuery for ChangeTrackers { } /// SAFETY: access is read only +#[allow(deprecated)] unsafe impl ReadOnlyWorldQuery for ChangeTrackers {} macro_rules! impl_tuple_fetch { From c11b2c0dca968bc4bc682a6c5f2fa1e31cc581a9 Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Fri, 20 Jan 2023 12:32:59 -0500 Subject: [PATCH 02/10] update unit tests --- crates/bevy_ecs/src/change_detection.rs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 3802af55dd349..4bdd9a3ef7e98 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -716,10 +716,9 @@ mod tests { use crate::{ self as bevy_ecs, change_detection::{ - Mut, NonSendMut, ResMut, TicksMut, CHECK_TICK_THRESHOLD, MAX_CHANGE_AGE, + Mut, NonSendMut, Ref, ResMut, TicksMut, CHECK_TICK_THRESHOLD, MAX_CHANGE_AGE, }, component::{Component, ComponentTicks, Tick}, - query::ChangeTrackers, system::{IntoSystem, Query, System}, world::World, }; @@ -738,11 +737,11 @@ mod tests { #[test] fn change_expiration() { - fn change_detected(query: Query>) -> bool { + fn change_detected(query: Query>) -> bool { query.single().is_changed() } - fn change_expired(query: Query>) -> bool { + fn change_expired(query: Query>) -> bool { query.single().is_changed() } @@ -773,7 +772,7 @@ mod tests { #[test] fn change_tick_wraparound() { - fn change_detected(query: Query>) -> bool { + fn change_detected(query: Query>) -> bool { query.single().is_changed() } @@ -804,10 +803,10 @@ mod tests { *world.change_tick.get_mut() += MAX_CHANGE_AGE + CHECK_TICK_THRESHOLD; let change_tick = world.change_tick(); - let mut query = world.query::>(); + let mut query = world.query::>(); for tracker in query.iter(&world) { - let ticks_since_insert = change_tick.wrapping_sub(tracker.component_ticks.added.tick); - let ticks_since_change = change_tick.wrapping_sub(tracker.component_ticks.changed.tick); + let ticks_since_insert = change_tick.wrapping_sub(tracker.ticks.added.tick); + let ticks_since_change = change_tick.wrapping_sub(tracker.ticks.changed.tick); assert!(ticks_since_insert > MAX_CHANGE_AGE); assert!(ticks_since_change > MAX_CHANGE_AGE); } @@ -816,8 +815,8 @@ mod tests { world.check_change_ticks(); for tracker in query.iter(&world) { - let ticks_since_insert = change_tick.wrapping_sub(tracker.component_ticks.added.tick); - let ticks_since_change = change_tick.wrapping_sub(tracker.component_ticks.changed.tick); + let ticks_since_insert = change_tick.wrapping_sub(tracker.ticks.added.tick); + let ticks_since_change = change_tick.wrapping_sub(tracker.ticks.changed.tick); assert!(ticks_since_insert == MAX_CHANGE_AGE); assert!(ticks_since_change == MAX_CHANGE_AGE); } From dddc75567d7bbf6228ad5b3f9f5ec959d1afde86 Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Fri, 20 Jan 2023 12:34:51 -0500 Subject: [PATCH 03/10] improve deprecation message --- crates/bevy_ecs/src/query/fetch.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index d52cd76a627dd..5cb2ad9934879 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -1115,7 +1115,7 @@ unsafe impl ReadOnlyWorldQuery for Option {} /// } /// # bevy_ecs::system::assert_is_system(print_moving_objects_system); /// ``` -#[deprecated = "Use `bevy_ecs::Ref` instead"] +#[deprecated = "`ChangeTrackers` will be removed in bevy 0.10. Use `bevy_ecs::prelude::Ref` instead."] pub struct ChangeTrackers { pub(crate) component_ticks: ComponentTicks, pub(crate) last_change_tick: u32, From ae04be3ac731a962d19f0d569fafc71f7c365d2f Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Fri, 20 Jan 2023 12:42:58 -0500 Subject: [PATCH 04/10] fix future version --- crates/bevy_ecs/src/query/fetch.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index 5cb2ad9934879..1f1686263dfe4 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -1115,7 +1115,7 @@ unsafe impl ReadOnlyWorldQuery for Option {} /// } /// # bevy_ecs::system::assert_is_system(print_moving_objects_system); /// ``` -#[deprecated = "`ChangeTrackers` will be removed in bevy 0.10. Use `bevy_ecs::prelude::Ref` instead."] +#[deprecated = "`ChangeTrackers` will be removed in bevy 0.11. Use `bevy_ecs::prelude::Ref` instead."] pub struct ChangeTrackers { pub(crate) component_ticks: ComponentTicks, pub(crate) last_change_tick: u32, From 91800ad772b336e316074b04b15dcf728d898a28 Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Fri, 20 Jan 2023 12:54:11 -0500 Subject: [PATCH 05/10] fix `bevy_ecs` tests --- crates/bevy_ecs/src/lib.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index b0a9f9f155a06..a3bd4a1067c9d 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -23,6 +23,8 @@ pub use bevy_ptr as ptr; /// Most commonly used re-exported types. pub mod prelude { + #[allow(deprecated)] + pub use crate::query::ChangeTrackers; #[doc(hidden)] #[cfg(feature = "bevy_reflect")] pub use crate::reflect::{ReflectComponent, ReflectResource}; @@ -33,7 +35,7 @@ pub mod prelude { component::Component, entity::Entity, event::{EventReader, EventWriter, Events}, - query::{Added, AnyOf, ChangeTrackers, Changed, Or, QueryState, With, Without}, + query::{Added, AnyOf, Changed, Or, QueryState, With, Without}, schedule::{ IntoSystemDescriptor, RunCriteria, RunCriteriaDescriptorCoercion, RunCriteriaLabel, Schedule, Stage, StageLabel, State, SystemLabel, SystemSet, SystemStage, @@ -53,14 +55,13 @@ pub use bevy_ecs_macros::all_tuples; #[cfg(test)] mod tests { use crate as bevy_ecs; + use crate::change_detection::Ref; use crate::prelude::Or; use crate::{ bundle::Bundle, component::{Component, ComponentId}, entity::Entity, - query::{ - Added, ChangeTrackers, Changed, FilteredAccess, ReadOnlyWorldQuery, With, Without, - }, + query::{Added, Changed, FilteredAccess, ReadOnlyWorldQuery, With, Without}, system::Resource, world::{Mut, World}, }; @@ -1290,7 +1291,10 @@ mod tests { } #[test] + #[allow(deprecated)] fn trackers_query() { + use crate::prelude::ChangeTrackers; + let mut world = World::default(); let e1 = world.spawn((A(0), B(0))).id(); world.spawn(B(0)); @@ -1534,7 +1538,7 @@ mod tests { assert_eq!(1, query_min_size![&B, (With, With)],); assert_eq!(1, query_min_size![(&A, &B), With],); assert_eq!(4, query_min_size![&A, ()], "Simple Archetypal"); - assert_eq!(4, query_min_size![ChangeTrackers, ()],); + assert_eq!(4, query_min_size![Ref, ()],); // All the following should set minimum size to 0, as it's impossible to predict // how many entites the filters will trim. assert_eq!(0, query_min_size![(), Added], "Simple Added"); From d4216e4a554e02309fb0e619b913299ef17c82c9 Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Fri, 20 Jan 2023 12:55:02 -0500 Subject: [PATCH 06/10] tweak formatting --- crates/bevy_ecs/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index a3bd4a1067c9d..9b35dac02999d 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -55,10 +55,10 @@ pub use bevy_ecs_macros::all_tuples; #[cfg(test)] mod tests { use crate as bevy_ecs; - use crate::change_detection::Ref; use crate::prelude::Or; use crate::{ bundle::Bundle, + change_detection::Ref, component::{Component, ComponentId}, entity::Entity, query::{Added, Changed, FilteredAccess, ReadOnlyWorldQuery, With, Without}, From 5b2798c432437d18866cd9605307eebf9c60c047 Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Fri, 20 Jan 2023 12:59:22 -0500 Subject: [PATCH 07/10] move attributes above docs --- crates/bevy_ecs/src/query/fetch.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index 1f1686263dfe4..dc6f42c90bd1d 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -1172,8 +1172,8 @@ pub struct ChangeTrackersFetch<'w, T> { change_tick: u32, } -// SAFETY: `ROQueryFetch` is the same as `QueryFetch` #[allow(deprecated)] +// SAFETY: `ROQueryFetch` is the same as `QueryFetch` unsafe impl WorldQuery for ChangeTrackers { type Fetch<'w> = ChangeTrackersFetch<'w, T>; type Item<'w> = ChangeTrackers; @@ -1319,8 +1319,8 @@ unsafe impl WorldQuery for ChangeTrackers { } } -/// SAFETY: access is read only #[allow(deprecated)] +/// SAFETY: access is read only unsafe impl ReadOnlyWorldQuery for ChangeTrackers {} macro_rules! impl_tuple_fetch { From f6e5d938e452e2f9b53f0bdeb070fd00855fe12d Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Fri, 20 Jan 2023 14:00:42 -0500 Subject: [PATCH 08/10] update a doc link --- crates/bevy_ecs/src/query/filter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/query/filter.rs b/crates/bevy_ecs/src/query/filter.rs index a067acbd89932..9b6a72ad2f6dc 100644 --- a/crates/bevy_ecs/src/query/filter.rs +++ b/crates/bevy_ecs/src/query/filter.rs @@ -572,7 +572,7 @@ impl_tick_filter!( /// A common use for this filter is one-time initialization. /// /// To retain all results without filtering but still check whether they were added after the - /// system last ran, use [`ChangeTrackers`](crate::query::ChangeTrackers). + /// system last ran, use [`Ref`](crate::change_detection::Ref). /// /// # Examples /// From ad9c65526e6e1613abf9a5a75394638ac969991b Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Fri, 20 Jan 2023 14:08:43 -0500 Subject: [PATCH 09/10] use `Ref<>` in an example --- examples/ecs/component_change_detection.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/ecs/component_change_detection.rs b/examples/ecs/component_change_detection.rs index c706f0fdb6da5..3579bd2b676c9 100644 --- a/examples/ecs/component_change_detection.rs +++ b/examples/ecs/component_change_detection.rs @@ -1,6 +1,6 @@ //! This example illustrates how to react to component change. -use bevy::prelude::*; +use bevy::{ecs::world::Ref, prelude::*}; use rand::Rng; fn main() { @@ -43,15 +43,15 @@ fn change_detection(query: Query<(Entity, &MyComponent), Changed>) } } -// By looking at trackers, the query is not filtered but the information is available -fn tracker_monitoring( - query: Query<( - Entity, - Option<&MyComponent>, - Option>, - )>, -) { - for (entity, component, trackers) in &query { - info!("{:?}: {:?} -> {:?}", entity, component, trackers); +// By using `Ref`, the query is not filtered but the information is available +fn tracker_monitoring(query: Query<(Entity, Ref)>) { + for (entity, component) in &query { + info!( + "{:?}: {:?} -> {{is_added: {}, is_changed: {}}}", + entity, + component, + component.is_added(), + component.is_changed() + ); } } From 1be031a1288396d4213da82ae57f9dabe46d3f5d Mon Sep 17 00:00:00 2001 From: JoJoJet <21144246+JoJoJet@users.noreply.github.com> Date: Mon, 6 Feb 2023 22:13:08 -0500 Subject: [PATCH 10/10] add `#[doc(hidden)]` to the prelude re-export --- crates/bevy_ecs/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bevy_ecs/src/lib.rs b/crates/bevy_ecs/src/lib.rs index 6d50aa258d1c5..e05e34896c9f6 100644 --- a/crates/bevy_ecs/src/lib.rs +++ b/crates/bevy_ecs/src/lib.rs @@ -23,6 +23,7 @@ pub use bevy_ptr as ptr; /// Most commonly used re-exported types. pub mod prelude { + #[doc(hidden)] #[allow(deprecated)] pub use crate::query::ChangeTrackers; #[doc(hidden)]