Skip to content

Added Query::for_each_combinations(_mut), Updated Query::for_each(_mut) #9546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
65 changes: 63 additions & 2 deletions crates/bevy_ecs/src/system/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
/// - [`for_each_mut`](Self::for_each_mut) to operate on mutable query items.
/// - [`iter`](Self::iter) for the iterator based alternative.
#[inline]
pub fn for_each<'this>(&'this self, f: impl FnMut(ROQueryItem<'this, Q>)) {
pub fn for_each(&self, f: impl for<'item> FnMut(ROQueryItem<'item, Q>)) {
// SAFETY:
// - `self.world` has permission to access the required components.
// - The query is read-only, so it can be aliased even if it was originally mutable.
Expand Down Expand Up @@ -771,14 +771,75 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> Query<'w, 's, Q, F> {
/// - [`for_each`](Self::for_each) to operate on read-only query items.
/// - [`iter_mut`](Self::iter_mut) for the iterator based alternative.
#[inline]
pub fn for_each_mut<'a>(&'a mut self, f: impl FnMut(Q::Item<'a>)) {
pub fn for_each_mut(&mut self, f: impl for<'item> FnMut(Q::Item<'item>)) {
// SAFETY: `self.world` has permission to access the required components.
unsafe {
self.state
.for_each_unchecked_manual(self.world, f, self.last_run, self.this_run);
};
}

/// Runs `f` on each read-only combination of query items of length `K`.
///
/// # Example
///
/// ```
/// # use bevy_ecs::prelude::*;
/// # #[derive(Component)]
/// # struct ComponentA;
/// #
/// fn some_system(query: Query<&ComponentA>) {
/// query.for_each_combinations(|[a1, a2]| {
/// // ...
/// });
/// }
/// ```
///
/// # See also
///
/// - [`iter_combinations`](Self::iter_combinations) for query item combinations.
/// - [`for_each`](Self::for_each) to operate on query items.
/// - [`iter`](Self::iter) for the iterator based alternative.
#[inline]
pub fn for_each_combinations<const K: usize>(
&self,
f: impl for<'item> FnMut([<<Q as WorldQuery>::ReadOnly as WorldQuery>::Item<'item>; K]),
) {
self.iter_combinations().for_each(f);
}

/// Runs `f` on each mutable combination of query items of length `K`.
///
/// # Example
///
/// ```
/// # use bevy_ecs::prelude::*;
/// # #[derive(Component)]
/// # struct ComponentA;
/// #
/// fn some_system(mut query: Query<&mut ComponentA>) {
/// query.for_each_combinations_mut(|[mut a1, mut a2]| {
/// // ...
/// });
/// }
/// ```
///
/// # See also
///
/// - [`iter_combinations_mut`](Self::iter_combinations_mut) for mutable query item combinations.
/// - [`for_each_mut`](Self::for_each_mut) to operate on mutable query items.
/// - [`iter`](Self::iter) for the iterator based alternative.
#[inline]
pub fn for_each_combinations_mut<const K: usize>(
&mut self,
mut f: impl for<'item> FnMut([Q::Item<'item>; K]),
) {
let mut combinations = self.iter_combinations_mut();
while let Some(combination) = combinations.fetch_next() {
f(combination);
}
}

/// Returns a parallel iterator over the query results for the given [`World`].
///
/// This can only be called for read-only queries, see [`par_iter_mut`] for write-queries.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
error[E0521]: borrowed data escapes outside of closure
--> tests/ui/query_lifetime_safety.rs:78:35
|
76 | let mut opt_data: Option<&Foo> = None;
| ------------ `opt_data` declared here, outside of the closure body
77 | let mut opt_data_2: Option<Mut<Foo>> = None;
78 | query.for_each(|data| opt_data = Some(data));
| ---- ^^^^^^^^^^^^^^^^^^^^^ `data` escapes the closure body here
| |
| `data` is a reference that is only valid in the closure body

error[E0521]: borrowed data escapes outside of closure
--> tests/ui/query_lifetime_safety.rs:79:39
|
77 | let mut opt_data_2: Option<Mut<Foo>> = None;
| -------------- `opt_data_2` declared here, outside of the closure body
78 | query.for_each(|data| opt_data = Some(data));
79 | query.for_each_mut(|data| opt_data_2 = Some(data));
| ---- ^^^^^^^^^^^^^^^^^^^^^^^ `data` escapes the closure body here
| |
| `data` is a reference that is only valid in the closure body

error[E0521]: borrowed data escapes outside of closure
--> tests/ui/query_lifetime_safety.rs:86:39
|
84 | let mut opt_data_2: Option<Mut<Foo>> = None;
| -------------- `opt_data_2` declared here, outside of the closure body
85 | let mut opt_data: Option<&Foo> = None;
86 | query.for_each_mut(|data| opt_data_2 = Some(data));
| ---- ^^^^^^^^^^^^^^^^^^^^^^^ `data` escapes the closure body here
| |
| `data` is a reference that is only valid in the closure body

error[E0521]: borrowed data escapes outside of closure
--> tests/ui/query_lifetime_safety.rs:87:35
|
85 | let mut opt_data: Option<&Foo> = None;
| ------------ `opt_data` declared here, outside of the closure body
86 | query.for_each_mut(|data| opt_data_2 = Some(data));
87 | query.for_each(|data| opt_data = Some(data));
| ---- ^^^^^^^^^^^^^^^^^^^^^ `data` escapes the closure body here
| |
| `data` is a reference that is only valid in the closure body

error[E0502]: cannot borrow `query` as mutable because it is also borrowed as immutable
--> tests/ui/query_lifetime_safety.rs:17:39
|
Expand Down Expand Up @@ -97,23 +141,3 @@ error[E0502]: cannot borrow `query` as immutable because it is also borrowed as
| ^^^^^^^^^^^^ immutable borrow occurs here
72 | assert_eq!(data, &mut *data2); // oops UB
| ----- mutable borrow later used here

error[E0502]: cannot borrow `query` as mutable because it is also borrowed as immutable
--> tests/ui/query_lifetime_safety.rs:79:13
|
78 | query.for_each(|data| opt_data = Some(data));
| -------------------------------------------- immutable borrow occurs here
79 | query.for_each_mut(|data| opt_data_2 = Some(data));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
80 | assert_eq!(opt_data.unwrap(), &mut *opt_data_2.unwrap()); // oops UB
| -------- immutable borrow later used here

error[E0502]: cannot borrow `query` as immutable because it is also borrowed as mutable
--> tests/ui/query_lifetime_safety.rs:87:13
|
86 | query.for_each_mut(|data| opt_data_2 = Some(data));
| -------------------------------------------------- mutable borrow occurs here
87 | query.for_each(|data| opt_data = Some(data));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ immutable borrow occurs here
88 | assert_eq!(opt_data.unwrap(), &mut *opt_data_2.unwrap()); // oops UB
| ---------- mutable borrow later used here