Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model world borrow in Query more accurately #5588

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions crates/bevy_ecs/src/query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,18 @@ impl<Q: WorldQuery, F: ReadOnlyWorldQuery> QueryState<Q, F> {
}

/// Checks if the query is empty for the given [`World`], where the last change and current tick are given.
///
/// This function will only access `world` in ways that are compliant with the access of `Q::ReadOnly` and
/// `F::ReadOnly` (though may not use those `WorldQuery`'s specifically).
#[inline]
pub fn is_empty(&self, world: &World, last_change_tick: u32, change_tick: u32) -> bool {
// SAFETY: NopFetch does not access any members while &self ensures no one has exclusive access
// SAFETY: `&World` ensures we have readonly access to the whole world. A `NopWorldQuery` does not
// access anything so it is trivially true that it cannot violate any aliasing guarantees. We convert
// the `F` world query to its `ReadOnly` which is guaranteed by the `ReadOnlyWorldQuery` trait to not
// access the world mutably.
unsafe {
self.as_nop()
self.as_readonly()
.as_nop()
.iter_unchecked_manual(world, last_change_tick, change_tick)
.next()
.is_none()
Expand Down
13 changes: 11 additions & 2 deletions crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,8 +1209,17 @@ mod tests {
let mut world1 = World::new();
let world2 = World::new();
let qstate = world1.query::<()>();
// SAFETY: doesnt access anything
let query = unsafe { Query::new(&world2, &qstate, 0, 0, false) };
// SAFETY: query state was made with the same QF as this query has
let query = unsafe {
Query::new(
// SAFETY: doesnt have any access
super::query_world_borrows::QueryLockBorrows::new(&world2),
&qstate,
0,
0,
false,
)
};
query.iter();
}
}