@@ -3,7 +3,7 @@ use crate::{
3
3
entity:: Entity ,
4
4
query:: {
5
5
Fetch , FilterFetch , NopFetch , QueryCombinationIter , QueryEntityError , QueryIter ,
6
- QueryState , WorldQuery ,
6
+ QueryState , ReadOnlyFetch , WorldQuery ,
7
7
} ,
8
8
world:: { Mut , World } ,
9
9
} ;
@@ -299,7 +299,7 @@ where
299
299
/// # bevy_ecs::system::assert_is_system(report_names_system);
300
300
/// ```
301
301
#[ inline]
302
- pub fn iter ( & ' s self ) -> QueryIter < ' w , ' s , Q , Q :: ReadOnlyFetch , F > {
302
+ pub fn iter ( & self ) -> QueryIter < ' _ , ' s , Q , Q :: ReadOnlyFetch , F > {
303
303
// SAFE: system runs without conflicts with other systems.
304
304
// same-system queries have runtime borrow checks when they conflict
305
305
unsafe {
@@ -454,17 +454,19 @@ where
454
454
/// # bevy_ecs::system::assert_is_system(report_names_system);
455
455
/// ```
456
456
#[ inline]
457
- pub fn for_each < FN : FnMut ( <Q :: ReadOnlyFetch as Fetch < ' w , ' s > >:: Item ) > ( & ' s self , f : FN ) {
457
+ pub fn for_each < ' this > (
458
+ & ' this self ,
459
+ f : impl FnMut ( <Q :: ReadOnlyFetch as Fetch < ' this , ' s > >:: Item ) ,
460
+ ) {
458
461
// SAFE: system runs without conflicts with other systems.
459
462
// same-system queries have runtime borrow checks when they conflict
460
463
unsafe {
461
- self . state
462
- . for_each_unchecked_manual :: < Q :: ReadOnlyFetch , FN > (
463
- self . world ,
464
- f,
465
- self . last_change_tick ,
466
- self . change_tick ,
467
- ) ;
464
+ self . state . for_each_unchecked_manual :: < Q :: ReadOnlyFetch , _ > (
465
+ self . world ,
466
+ f,
467
+ self . last_change_tick ,
468
+ self . change_tick ,
469
+ ) ;
468
470
} ;
469
471
}
470
472
@@ -524,17 +526,17 @@ where
524
526
///* `batch_size` - The number of batches to spawn
525
527
///* `f` - The function to run on each item in the query
526
528
#[ inline]
527
- pub fn par_for_each < FN : Fn ( < Q :: ReadOnlyFetch as Fetch < ' w , ' s > > :: Item ) + Send + Sync + Clone > (
528
- & ' s self ,
529
+ pub fn par_for_each < ' this > (
530
+ & ' this self ,
529
531
task_pool : & TaskPool ,
530
532
batch_size : usize ,
531
- f : FN ,
533
+ f : impl Fn ( < Q :: ReadOnlyFetch as Fetch < ' this , ' s > > :: Item ) + Send + Sync + Clone ,
532
534
) {
533
535
// SAFE: system runs without conflicts with other systems. same-system queries have runtime
534
536
// borrow checks when they conflict
535
537
unsafe {
536
538
self . state
537
- . par_for_each_unchecked_manual :: < Q :: ReadOnlyFetch , FN > (
539
+ . par_for_each_unchecked_manual :: < Q :: ReadOnlyFetch , _ > (
538
540
self . world ,
539
541
task_pool,
540
542
batch_size,
@@ -601,9 +603,9 @@ where
601
603
/// ```
602
604
#[ inline]
603
605
pub fn get (
604
- & ' s self ,
606
+ & self ,
605
607
entity : Entity ,
606
- ) -> Result < <Q :: ReadOnlyFetch as Fetch < ' w , ' s > >:: Item , QueryEntityError > {
608
+ ) -> Result < <Q :: ReadOnlyFetch as Fetch < ' _ , ' s > >:: Item , QueryEntityError > {
607
609
// SAFE: system runs without conflicts with other systems.
608
610
// same-system queries have runtime borrow checks when they conflict
609
611
unsafe {
@@ -834,7 +836,7 @@ where
834
836
/// Panics if the number of query results is not exactly one. Use
835
837
/// [`get_single`](Self::get_single) to return a `Result` instead of panicking.
836
838
#[ track_caller]
837
- pub fn single ( & ' s self ) -> <Q :: ReadOnlyFetch as Fetch < ' w , ' s > >:: Item {
839
+ pub fn single ( & self ) -> <Q :: ReadOnlyFetch as Fetch < ' _ , ' s > >:: Item {
838
840
self . get_single ( ) . unwrap ( )
839
841
}
840
842
@@ -870,8 +872,8 @@ where
870
872
/// # bevy_ecs::system::assert_is_system(player_scoring_system);
871
873
/// ```
872
874
pub fn get_single (
873
- & ' s self ,
874
- ) -> Result < <Q :: ReadOnlyFetch as Fetch < ' w , ' s > >:: Item , QuerySingleError > {
875
+ & self ,
876
+ ) -> Result < <Q :: ReadOnlyFetch as Fetch < ' _ , ' s > >:: Item , QuerySingleError > {
875
877
let mut query = self . iter ( ) ;
876
878
let first = query. next ( ) ;
877
879
let extra = query. next ( ) . is_some ( ) ;
@@ -1038,3 +1040,90 @@ pub enum QuerySingleError {
1038
1040
#[ error( "Multiple entities fit the query {0}!" ) ]
1039
1041
MultipleEntities ( & ' static str ) ,
1040
1042
}
1043
+
1044
+ impl < ' w , ' s , Q : WorldQuery , F : WorldQuery > Query < ' w , ' s , Q , F >
1045
+ where
1046
+ F :: Fetch : FilterFetch ,
1047
+ Q :: Fetch : ReadOnlyFetch ,
1048
+ {
1049
+ /// Returns the query result for the given [`Entity`], with the actual "inner" world lifetime.
1050
+ ///
1051
+ /// In case of a nonexisting entity or mismatched component, a [`QueryEntityError`] is
1052
+ /// returned instead.
1053
+ ///
1054
+ /// This can only return immutable data (mutable data will be cast to an immutable form).
1055
+ /// See [`get_mut`](Self::get_mut) for queries that contain at least one mutable component.
1056
+ ///
1057
+ /// # Example
1058
+ ///
1059
+ /// Here, `get` is used to retrieve the exact query result of the entity specified by the
1060
+ /// `SelectedCharacter` resource.
1061
+ ///
1062
+ /// ```
1063
+ /// # use bevy_ecs::prelude::*;
1064
+ /// #
1065
+ /// # struct SelectedCharacter { entity: Entity }
1066
+ /// # #[derive(Component)]
1067
+ /// # struct Character { name: String }
1068
+ /// #
1069
+ /// fn print_selected_character_name_system(
1070
+ /// query: Query<&Character>,
1071
+ /// selection: Res<SelectedCharacter>
1072
+ /// )
1073
+ /// {
1074
+ /// if let Ok(selected_character) = query.get(selection.entity) {
1075
+ /// println!("{}", selected_character.name);
1076
+ /// }
1077
+ /// }
1078
+ /// # bevy_ecs::system::assert_is_system(print_selected_character_name_system);
1079
+ /// ```
1080
+ #[ inline]
1081
+ pub fn get_inner (
1082
+ & ' s self ,
1083
+ entity : Entity ,
1084
+ ) -> Result < <Q :: ReadOnlyFetch as Fetch < ' w , ' s > >:: Item , QueryEntityError > {
1085
+ // SAFE: system runs without conflicts with other systems.
1086
+ // same-system queries have runtime borrow checks when they conflict
1087
+ unsafe {
1088
+ self . state . get_unchecked_manual :: < Q :: ReadOnlyFetch > (
1089
+ self . world ,
1090
+ entity,
1091
+ self . last_change_tick ,
1092
+ self . change_tick ,
1093
+ )
1094
+ }
1095
+ }
1096
+
1097
+ /// Returns an [`Iterator`] over the query results, with the actual "inner" world lifetime.
1098
+ ///
1099
+ /// This can only return immutable data (mutable data will be cast to an immutable form).
1100
+ /// See [`Self::iter_mut`] for queries that contain at least one mutable component.
1101
+ ///
1102
+ /// # Example
1103
+ ///
1104
+ /// Here, the `report_names_system` iterates over the `Player` component of every entity
1105
+ /// that contains it:
1106
+ ///
1107
+ /// ```
1108
+ /// # use bevy_ecs::prelude::*;
1109
+ /// #
1110
+ /// # #[derive(Component)]
1111
+ /// # struct Player { name: String }
1112
+ /// #
1113
+ /// fn report_names_system(query: Query<&Player>) {
1114
+ /// for player in query.iter() {
1115
+ /// println!("Say hello to {}!", player.name);
1116
+ /// }
1117
+ /// }
1118
+ /// # bevy_ecs::system::assert_is_system(report_names_system);
1119
+ /// ```
1120
+ #[ inline]
1121
+ pub fn iter_inner ( & ' s self ) -> QueryIter < ' w , ' s , Q , Q :: ReadOnlyFetch , F > {
1122
+ // SAFE: system runs without conflicts with other systems.
1123
+ // same-system queries have runtime borrow checks when they conflict
1124
+ unsafe {
1125
+ self . state
1126
+ . iter_unchecked_manual ( self . world , self . last_change_tick , self . change_tick )
1127
+ }
1128
+ }
1129
+ }
0 commit comments