Skip to content

Commit be19c69

Browse files
committed
Add missing ReadOnly = Self bound (#5462)
# Objective `ReadOnlyWorldQuery` should have required `Self::ReadOnly = Self` so that calling `.iter()` on a readonly query is equivelent to calling `iter_mut()`. ## Solution add `ReadOnly = Self` to the definition of `ReadOnlyWorldQuery` --- ## Changelog ReadOnlyWorldQuery's `ReadOnly` assoc type is now always equal to `Self` ## Migration Guide Make `Self::ReadOnly = Self` hold
1 parent 3561b38 commit be19c69

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

crates/bevy_ecs/src/query/fetch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ use std::{cell::UnsafeCell, marker::PhantomData};
168168
/// ```
169169
///
170170
/// **Note:** if you omit the `mutable` attribute for a query that doesn't implement
171-
/// `ReadOnlyFetch`, compilation will fail. We insert static checks as in the example above for
171+
/// [`ReadOnlyWorldQuery`], compilation will fail. We insert static checks as in the example above for
172172
/// every query component and a nested query.
173173
/// (The checks neither affect the runtime, nor pollute your local namespace.)
174174
///
@@ -333,7 +333,7 @@ pub unsafe trait WorldQuery: for<'w> WorldQueryGats<'w, _State = Self::State> {
333333
/// # Safety
334334
///
335335
/// This must only be implemented for read-only [`WorldQuery`]'s.
336-
pub unsafe trait ReadOnlyWorldQuery: WorldQuery {}
336+
pub unsafe trait ReadOnlyWorldQuery: WorldQuery<ReadOnly = Self> {}
337337

338338
/// The [`Fetch`] of a [`WorldQuery`], which declares which data it needs access to
339339
pub type QueryFetch<'w, Q> = <Q as WorldQueryGats<'w>>::Fetch;

crates/bevy_ecs/src/query/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ where
306306

307307
#[inline]
308308
fn next(&mut self) -> Option<Self::Item> {
309-
// Safety: it is safe to alias for ReadOnlyFetch
309+
// Safety: it is safe to alias for ReadOnlyWorldQuery
310310
unsafe { QueryCombinationIter::fetch_next_aliased_unchecked(self) }
311311
}
312312

crates/bevy_ecs/src/system/query.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ use std::{any::TypeId, borrow::Borrow, fmt::Debug};
5454
/// # bevy_ecs::system::assert_is_system(system);
5555
/// ```
5656
///
57+
/// You can use the [`ReadOnlyWorldQuery`] trait to abstract over read only query generics:
58+
/// ```
59+
/// # use bevy_ecs::system::Query;
60+
/// # use bevy_ecs::query::{QueryItem, ReadOnlyWorldQuery};
61+
/// fn system<Q: ReadOnlyWorldQuery>(
62+
/// query: Query<Q>,
63+
/// ) {
64+
/// let _: Option<QueryItem<Q>> = query.iter().next();
65+
/// }
66+
/// ```
67+
///
5768
/// ## Mutable component access
5869
///
5970
/// The following example is similar to the previous one, with the exception of `ComponentA`

0 commit comments

Comments
 (0)