Skip to content

Add QueryState::contains, document complexity, and make as_nop pub(crate) #12776

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

Merged
merged 2 commits into from
Mar 29, 2024
Merged
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
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1654,7 +1654,7 @@ all_tuples!(impl_anytuple_fetch, 0, 15, F, S);
/// [`WorldQuery`] used to nullify queries by turning `Query<D>` into `Query<NopWorldQuery<D>>`
///
/// This will rarely be useful to consumers of `bevy_ecs`.
pub struct NopWorldQuery<D: QueryData>(PhantomData<D>);
pub(crate) struct NopWorldQuery<D: QueryData>(PhantomData<D>);

/// SAFETY:
/// `update_component_access` and `update_archetype_component_access` do nothing.
Expand Down
32 changes: 31 additions & 1 deletion crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
///
/// This doesn't use `NopWorldQuery` as it loses filter functionality, for example
/// `NopWorldQuery<Changed<T>>` is functionally equivalent to `With<T>`.
pub fn as_nop(&self) -> &QueryState<NopWorldQuery<D>, F> {
pub(crate) fn as_nop(&self) -> &QueryState<NopWorldQuery<D>, F> {
// SAFETY: `NopWorldQuery` doesn't have any accesses and defers to
// `D` for table/archetype matching
unsafe { self.as_transmuted_state::<NopWorldQuery<D>, F>() }
Expand Down Expand Up @@ -244,6 +244,24 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
}
}

/// 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
Expand Down Expand Up @@ -570,6 +588,8 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// 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,
Expand Down Expand Up @@ -642,6 +662,8 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
}

/// 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,
Expand Down Expand Up @@ -733,6 +755,8 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// 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,
Expand All @@ -753,6 +777,8 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {

/// 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
Expand All @@ -770,6 +796,8 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// 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
Expand Down Expand Up @@ -850,6 +878,8 @@ impl<D: QueryData, F: QueryFilter> QueryState<D, F> {
/// 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.
Expand Down
8 changes: 8 additions & 0 deletions crates/bevy_ecs/src/system/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
///
/// ```
Expand Down