From 52999624267392500e9fe6459df32d98f2244945 Mon Sep 17 00:00:00 2001 From: pablo-lua Date: Sat, 12 Oct 2024 15:04:50 -0300 Subject: [PATCH 1/9] [alt] Changed to use Single instead of Query::single where possible --- .../bevy_ecs/scheduling/run_condition.rs | 4 +- crates/bevy_app/src/app.rs | 28 ++++++++--- crates/bevy_ecs/src/change_detection.rs | 10 ++-- crates/bevy_ecs/src/system/mod.rs | 21 ++++---- crates/bevy_ecs/src/system/query.rs | 8 ++- crates/bevy_ecs/src/system/system_param.rs | 5 +- crates/bevy_hierarchy/src/query_extension.rs | 10 ++-- examples/2d/2d_viewport_to_world.rs | 4 +- examples/2d/bloom_2d.rs | 8 +-- examples/2d/bounding_2d.rs | 4 +- examples/2d/pixel_grid_snap.rs | 4 +- examples/2d/rotation.rs | 12 ++--- examples/2d/sprite_animation.rs | 4 +- examples/2d/wireframe_2d.rs | 4 +- examples/3d/3d_viewport_to_world.rs | 11 ++-- examples/3d/anti_aliasing.rs | 13 +++-- examples/3d/atmospheric_fog.rs | 4 +- examples/3d/auto_exposure.rs | 13 +++-- examples/3d/blend_modes.rs | 9 ++-- examples/3d/bloom_3d.rs | 11 ++-- examples/3d/color_grading.rs | 31 +++++------- examples/3d/deferred_rendering.rs | 4 +- examples/3d/fog.rs | 9 ++-- examples/3d/lighting.rs | 8 +-- examples/3d/load_gltf_extras.rs | 4 +- examples/3d/motion_blur.rs | 16 +++--- examples/3d/order_independent_transparency.rs | 4 +- examples/3d/parallax_mapping.rs | 15 +++--- examples/3d/shadow_biases.rs | 26 +++++----- examples/3d/spotlight.rs | 3 +- examples/3d/ssao.rs | 10 ++-- examples/3d/tonemapping.rs | 50 ++++++++----------- examples/3d/transmission.rs | 23 ++++----- examples/3d/wireframe.rs | 4 +- examples/app/log_layers_ecs.rs | 4 +- examples/audio/spatial_audio_2d.rs | 12 ++--- examples/audio/spatial_audio_3d.rs | 12 ++--- examples/camera/camera_orbit.rs | 9 ++-- examples/camera/projection_zoom.rs | 12 ++--- examples/ecs/iter_combinations.rs | 6 +-- examples/ecs/observers.rs | 7 ++- examples/ecs/one_shot_systems.rs | 16 +++--- examples/ecs/parallel_query.rs | 3 +- examples/games/alien_cake_addict.rs | 4 +- examples/games/breakout.rs | 11 ++-- examples/games/contributors.rs | 7 ++- examples/games/desk_toy.rs | 30 ++++------- examples/games/game_menu.rs | 4 +- examples/games/loading_screen.rs | 4 +- examples/games/stepping.rs | 8 +-- examples/gizmos/light_gizmos.rs | 8 ++- examples/input/mouse_grab.rs | 4 +- examples/input/text_input.rs | 32 ++++++------ examples/math/custom_primitives.rs | 4 +- examples/math/random_sampling.rs | 3 +- examples/math/render_primitives.rs | 8 +-- examples/math/sampling_primitives.rs | 8 +-- examples/mobile/src/lib.rs | 20 +++----- examples/movement/smooth_follow.rs | 10 ++-- examples/remote/server.rs | 4 +- .../shader/compute_shader_game_of_life.rs | 3 +- .../shader_material_screenspace_texture.rs | 4 +- examples/shader/shader_prepass.rs | 9 ++-- examples/state/custom_transitions.rs | 4 +- examples/stress_tests/bevymark.rs | 19 +++---- .../stress_tests/many_animated_sprites.rs | 5 +- examples/stress_tests/many_cubes.rs | 3 +- examples/stress_tests/many_gizmos.rs | 6 +-- examples/stress_tests/many_lights.rs | 3 +- examples/stress_tests/many_sprites.rs | 5 +- examples/tools/gamepad_viewer.rs | 4 +- .../tools/scene_viewer/morph_viewer_plugin.rs | 4 +- examples/transforms/align.rs | 37 ++++++-------- examples/ui/overflow_debug.rs | 4 +- examples/ui/relative_cursor_position.rs | 8 ++- examples/ui/size_constraints.rs | 11 ++-- examples/ui/window_fallthrough.rs | 3 +- examples/window/low_power.rs | 7 ++- examples/window/scale_factor_override.rs | 15 ++---- examples/window/window_resizing.rs | 9 ++-- examples/window/window_settings.rs | 35 ++++--------- tests/window/minimising.rs | 3 +- tests/window/resizing.rs | 3 +- 83 files changed, 363 insertions(+), 485 deletions(-) diff --git a/benches/benches/bevy_ecs/scheduling/run_condition.rs b/benches/benches/bevy_ecs/scheduling/run_condition.rs index 1a033f36ef8b8..c88d89b1fe8f8 100644 --- a/benches/benches/bevy_ecs/scheduling/run_condition.rs +++ b/benches/benches/bevy_ecs/scheduling/run_condition.rs @@ -67,8 +67,8 @@ pub fn run_condition_yes_with_query(criterion: &mut Criterion) { group.warm_up_time(core::time::Duration::from_millis(500)); group.measurement_time(core::time::Duration::from_secs(3)); fn empty() {} - fn yes_with_query(query: Query<&TestBool>) -> bool { - query.single().0 + fn yes_with_query(query: Single<&TestBool>) -> bool { + query.0 } for amount in 0..21 { let mut schedule = Schedule::default(); diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index 7050c61d5888a..da6c3bbf77e51 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -800,8 +800,11 @@ impl App { /// commands.spawn(A); /// } /// - /// fn validate(query: Query<(&A, &B, &C)>) { - /// let (a, b, c) = query.single(); + /// fn validate(query: Option>) { + /// assert!(query.is_some()); + /// let Some((a, b, c)) = *query else { + /// return; + /// }; /// assert_eq!(b, &B(0)); /// assert_eq!(c, &C(0)); /// } @@ -863,8 +866,11 @@ impl App { /// commands.spawn(A); /// } /// - /// fn validate(query: Query<(&A, &B, &C)>) { - /// let (a, b, c) = query.single(); + /// fn validate(query: Option>) { + /// assert!(query.is_some()); + /// let Some((a, b, c)) = *query else { + /// return; + /// }; /// assert_eq!(b, &B(0)); /// assert_eq!(c, &C(2)); /// } @@ -928,8 +934,11 @@ impl App { /// commands.spawn(A); /// } /// - /// fn validate(query: Query<(&A, &B, &C)>) { - /// let (a, b, c) = query.single(); + /// fn validate(query: Option>) { + /// assert!(query.is_some()); + /// let Some((a, b, c)) = *query else { + /// return; + /// }; /// assert_eq!(b, &B(0)); /// assert_eq!(c, &C(0)); /// } @@ -993,8 +1002,11 @@ impl App { /// commands.spawn(A); /// } /// - /// fn validate(query: Query<(&A, &B, &C)>) { - /// let (a, b, c) = query.single(); + /// fn validate(query: Option>) { + /// assert!(query.is_some()); + /// let Some((a, b, c)) = *query else { + /// return; + /// }; /// assert_eq!(b, &B(0)); /// assert_eq!(c, &C(2)); /// } diff --git a/crates/bevy_ecs/src/change_detection.rs b/crates/bevy_ecs/src/change_detection.rs index 5a3adac96fe20..025c3804e73e2 100644 --- a/crates/bevy_ecs/src/change_detection.rs +++ b/crates/bevy_ecs/src/change_detection.rs @@ -1206,7 +1206,7 @@ mod tests { Mut, NonSendMut, Ref, ResMut, TicksMut, CHECK_TICK_THRESHOLD, MAX_CHANGE_AGE, }, component::{Component, ComponentTicks, Tick}, - system::{IntoSystem, Query, System}, + system::{IntoSystem, Single, System}, world::World, }; @@ -1236,12 +1236,12 @@ mod tests { #[test] fn change_expiration() { - fn change_detected(query: Query>) -> bool { - query.single().is_changed() + fn change_detected(query: Option>>) -> bool { + query.unwrap().is_changed() } - fn change_expired(query: Query>) -> bool { - query.single().is_changed() + fn change_expired(query: Option>>) -> bool { + query.unwrap().is_changed() } let mut world = World::new(); diff --git a/crates/bevy_ecs/src/system/mod.rs b/crates/bevy_ecs/src/system/mod.rs index dd6a950f72a8e..7661d33e76bbb 100644 --- a/crates/bevy_ecs/src/system/mod.rs +++ b/crates/bevy_ecs/src/system/mod.rs @@ -327,8 +327,7 @@ mod tests { Schedule, }, system::{ - Commands, In, IntoSystem, Local, NonSend, NonSendMut, ParamSet, Query, Res, ResMut, - Resource, StaticSystemParam, System, SystemState, + Commands, In, IntoSystem, Local, NonSend, NonSendMut, ParamSet, Query, Res, ResMut, Resource, Single, StaticSystemParam, System, SystemState }, world::{EntityMut, FromWorld, World}, }; @@ -1157,12 +1156,12 @@ mod tests { world.insert_resource(A(42)); world.spawn(B(7)); - let mut system_state: SystemState<(Res, Query<&B>, ParamSet<(Query<&C>, Query<&D>)>)> = + let mut system_state: SystemState<(Res, Option>, ParamSet<(Query<&C>, Query<&D>)>)> = SystemState::new(&mut world); let (a, query, _) = system_state.get(&world); assert_eq!(*a, A(42), "returned resource matches initial value"); assert_eq!( - *query.single(), + **query.unwrap(), B(7), "returned component matches initial value" ); @@ -1180,16 +1179,16 @@ mod tests { world.insert_resource(A(42)); world.spawn(B(7)); - let mut system_state: SystemState<(ResMut, Query<&mut B>)> = + let mut system_state: SystemState<(ResMut, Option>)> = SystemState::new(&mut world); // The following line shouldn't compile because the parameters used are not ReadOnlySystemParam // let (a, query) = system_state.get(&world); - let (a, mut query) = system_state.get_mut(&mut world); + let (a, query) = system_state.get_mut(&mut world); assert_eq!(*a, A(42), "returned resource matches initial value"); assert_eq!( - *query.single_mut(), + **query.unwrap(), B(7), "returned component matches initial value" ); @@ -1203,21 +1202,21 @@ mod tests { let mut world = World::default(); let entity = world.spawn(A(1)).id(); - let mut system_state: SystemState>> = SystemState::new(&mut world); + let mut system_state: SystemState>>> = SystemState::new(&mut world); { let query = system_state.get(&world); - assert_eq!(*query.single(), A(1)); + assert_eq!(**query.unwrap(), A(1)); } { let query = system_state.get(&world); - assert!(query.get_single().is_err()); + assert!(query.is_none()); } world.entity_mut(entity).get_mut::().unwrap().0 = 2; { let query = system_state.get(&world); - assert_eq!(*query.single(), A(2)); + assert_eq!(**query.unwrap(), A(1)); } } diff --git a/crates/bevy_ecs/src/system/query.rs b/crates/bevy_ecs/src/system/query.rs index f0116e60da50c..5553e475ed9d6 100644 --- a/crates/bevy_ecs/src/system/query.rs +++ b/crates/bevy_ecs/src/system/query.rs @@ -1119,8 +1119,7 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// # struct Player; /// # #[derive(Component)] /// # struct Position(f32, f32); - /// fn player_system(query: Query<&Position, With>) { - /// let player_position = query.single(); + /// fn player_system(query: Single<&Position, With>) { /// // do something with player_position /// } /// # bevy_ecs::system::assert_is_system(player_system); @@ -1196,8 +1195,7 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// # #[derive(Component)] /// # struct Health(u32); /// # - /// fn regenerate_player_health_system(mut query: Query<&mut Health, With>) { - /// let mut health = query.single_mut(); + /// fn regenerate_player_health_system(mut query: Single<&mut Health, With>) { /// health.0 += 1; /// } /// # bevy_ecs::system::assert_is_system(regenerate_player_health_system); @@ -1384,7 +1382,7 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// * `&mut T` -> `&T` /// * `&mut T` -> `Ref` /// * [`EntityMut`](crate::world::EntityMut) -> [`EntityRef`](crate::world::EntityRef) - /// + /// /// [`EntityLocation`]: crate::entity::EntityLocation /// [`&Archetype`]: crate::archetype::Archetype #[track_caller] diff --git a/crates/bevy_ecs/src/system/system_param.rs b/crates/bevy_ecs/src/system/system_param.rs index 9d06aec04d9ef..df98e49785176 100644 --- a/crates/bevy_ecs/src/system/system_param.rs +++ b/crates/bevy_ecs/src/system/system_param.rs @@ -1214,18 +1214,17 @@ pub trait SystemBuffer: FromWorld + Send + 'static { /// /// // Sound the alarm if there are any criminals who pose a threat. /// fn alert_criminal( -/// settlements: Query<&Settlement>, +/// settlement: Single<&Settlement>, /// criminals: Query<&Criminal>, /// mut alarm: Deferred /// ) { -/// let settlement = settlements.single(); /// for criminal in &criminals { /// // Only sound the alarm if the criminal is a threat. /// // For this example, assume that this check is expensive to run. /// // Since the majority of this system's run-time is dominated /// // by calling `is_threat()`, we defer sounding the alarm to /// // allow this system to run in parallel with other alarm systems. -/// if criminal.is_threat(settlement) { +/// if criminal.is_threat(*settlement) { /// alarm.flag(); /// } /// } diff --git a/crates/bevy_hierarchy/src/query_extension.rs b/crates/bevy_hierarchy/src/query_extension.rs index 5cd8631310762..6396ddcfb756f 100644 --- a/crates/bevy_hierarchy/src/query_extension.rs +++ b/crates/bevy_hierarchy/src/query_extension.rs @@ -60,9 +60,8 @@ pub trait HierarchyQueryExt<'w, 's, D: QueryData, F: QueryFilter> { /// # use bevy_hierarchy::prelude::*; /// # #[derive(Component)] /// # struct Marker; - /// fn system(query: Query>, children_query: Query<&Children>) { - /// let entity = query.single(); - /// for descendant in children_query.iter_descendants(entity) { + /// fn system(entity: Single>, children_query: Query<&Children>) { + /// for descendant in children_query.iter_descendants(*entity) { /// // Do something! /// } /// } @@ -95,9 +94,8 @@ pub trait HierarchyQueryExt<'w, 's, D: QueryData, F: QueryFilter> { /// # use bevy_hierarchy::prelude::*; /// # #[derive(Component)] /// # struct Marker; - /// fn system(query: Query>, parent_query: Query<&Parent>) { - /// let entity = query.single(); - /// for ancestor in parent_query.iter_ancestors(entity) { + /// fn system(entity: Single>, parent_query: Query<&Parent>) { + /// for ancestor in parent_query.iter_ancestors(*entity) { /// // Do something! /// } /// } diff --git a/examples/2d/2d_viewport_to_world.rs b/examples/2d/2d_viewport_to_world.rs index 5648f30e87211..5aa3db91e0af9 100644 --- a/examples/2d/2d_viewport_to_world.rs +++ b/examples/2d/2d_viewport_to_world.rs @@ -11,11 +11,11 @@ fn main() { } fn draw_cursor( - camera_query: Query<(&Camera, &GlobalTransform)>, + camera_query: Single<(&Camera, &GlobalTransform)>, windows: Query<&Window>, mut gizmos: Gizmos, ) { - let (camera, camera_transform) = camera_query.single(); + let (camera, camera_transform) = *camera_query; let Ok(window) = windows.get_single() else { return; diff --git a/examples/2d/bloom_2d.rs b/examples/2d/bloom_2d.rs index 1f75c0062ea8f..64fa9e2b92f11 100644 --- a/examples/2d/bloom_2d.rs +++ b/examples/2d/bloom_2d.rs @@ -71,14 +71,14 @@ fn setup( // ------------------------------------------------------------------------------------------------ fn update_bloom_settings( - mut camera: Query<(Entity, Option<&mut Bloom>), With>, - mut text: Query<&mut Text>, + camera: Single<(Entity, Option<&mut Bloom>), With>, + text: Single<&mut Text>, mut commands: Commands, keycode: Res>, time: Res