diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index 5c7d7b21f260d..6896e04e9ee15 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -1654,7 +1654,7 @@ all_tuples!(impl_anytuple_fetch, 0, 15, F, S); /// [`WorldQuery`] used to nullify queries by turning `Query` into `Query>` /// /// This will rarely be useful to consumers of `bevy_ecs`. -pub struct NopWorldQuery(PhantomData); +pub(crate) struct NopWorldQuery(PhantomData); /// SAFETY: /// `update_component_access` and `update_archetype_component_access` do nothing. diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index 57bad73e7b8a2..23c3b34914131 100644 --- a/crates/bevy_ecs/src/query/state.rs +++ b/crates/bevy_ecs/src/query/state.rs @@ -86,7 +86,7 @@ impl QueryState { /// /// This doesn't use `NopWorldQuery` as it loses filter functionality, for example /// `NopWorldQuery>` is functionally equivalent to `With`. - pub fn as_nop(&self) -> &QueryState, F> { + pub(crate) fn as_nop(&self) -> &QueryState, F> { // SAFETY: `NopWorldQuery` doesn't have any accesses and defers to // `D` for table/archetype matching unsafe { self.as_transmuted_state::, F>() } @@ -244,6 +244,24 @@ impl QueryState { } } + /// Returns `true` if the given [`Entity`] matches the query. + /// + /// This is always guaranteed to run in `O(1)` time. + #[inline] + pub fn contains(&self, entity: Entity, world: &World, last_run: Tick, this_run: Tick) -> bool { + // SAFETY: NopFetch does not access any members while &self ensures no one has exclusive access + unsafe { + self.as_nop() + .get_unchecked_manual( + world.as_unsafe_world_cell_readonly(), + entity, + last_run, + this_run, + ) + .is_ok() + } + } + /// Checks if the query is empty for the given [`UnsafeWorldCell`]. /// /// # Safety @@ -570,6 +588,8 @@ impl QueryState { /// Gets the query result for the given [`World`] and [`Entity`]. /// /// This can only be called for read-only queries, see [`Self::get_mut`] for write-queries. + /// + /// This is always guaranteed to run in `O(1)` time. #[inline] pub fn get<'w>( &mut self, @@ -642,6 +662,8 @@ impl QueryState { } /// Gets the query result for the given [`World`] and [`Entity`]. + /// + /// This is always guaranteed to run in `O(1)` time. #[inline] pub fn get_mut<'w>( &mut self, @@ -733,6 +755,8 @@ impl QueryState { /// access to `self`. /// /// This can only be called for read-only queries, see [`Self::get_mut`] for mutable queries. + /// + /// This is always guaranteed to run in `O(1)` time. #[inline] pub fn get_manual<'w>( &self, @@ -753,6 +777,8 @@ impl QueryState { /// Gets the query result for the given [`World`] and [`Entity`]. /// + /// This is always guaranteed to run in `O(1)` time. + /// /// # Safety /// /// This does not check for mutable query correctness. To be safe, make sure mutable queries @@ -770,6 +796,8 @@ impl QueryState { /// Gets the query result for the given [`World`] and [`Entity`], where the last change and /// the current change tick are given. /// + /// This is always guaranteed to run in `O(1)` time. + /// /// # Safety /// /// This does not check for mutable query correctness. To be safe, make sure mutable queries @@ -850,6 +878,8 @@ impl QueryState { /// Gets the query results for the given [`World`] and array of [`Entity`], where the last change and /// the current change tick are given. /// + /// This is always guaranteed to run in `O(1)` time. + /// /// # Safety /// /// This does not check for unique access to subsets of the entity-component data. diff --git a/crates/bevy_ecs/src/system/query.rs b/crates/bevy_ecs/src/system/query.rs index 961c30c94d3c4..3693334d9627c 100644 --- a/crates/bevy_ecs/src/system/query.rs +++ b/crates/bevy_ecs/src/system/query.rs @@ -813,6 +813,8 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// /// In case of a nonexisting entity or mismatched component, a [`QueryEntityError`] is returned instead. /// + /// This is always guaranteed to run in `O(1)` time. + /// /// # Example /// /// Here, `get` is used to retrieve the exact query item of the entity specified by the `SelectedCharacter` resource. @@ -931,6 +933,8 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// /// In case of a nonexisting entity or mismatched component, a [`QueryEntityError`] is returned instead. /// + /// This is always guaranteed to run in `O(1)` time. + /// /// # Example /// /// Here, `get_mut` is used to retrieve the exact query item of the entity specified by the `PoisonedCharacter` resource. @@ -1045,6 +1049,8 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// /// In case of a nonexisting entity or mismatched component, a [`QueryEntityError`] is returned instead. /// + /// This is always guaranteed to run in `O(1)` time. + /// /// # Safety /// /// This function makes it possible to violate Rust's aliasing guarantees. @@ -1248,6 +1254,8 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// Returns `true` if the given [`Entity`] matches the query. /// + /// This is always guaranteed to run in `O(1)` time. + /// /// # Example /// /// ```