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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - [ecs] implement is_empty for queries #2271

Closed
wants to merge 15 commits into from
15 changes: 15 additions & 0 deletions crates/bevy_ecs/src/system/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,21 @@ mod tests {
assert_eq!(conflicts, vec![b_id, d_id]);
}

#[test]
fn query_is_empty() {
fn sys(empty: Query<&A>, not_empty: Query<&B>) {
assert!(empty.is_empty());
assert!(!not_empty.is_empty());
}

let mut world = World::default();
world.spawn().insert(B);

let mut sys = sys.system();
sys.initialize(&mut world);
sys.run((), &mut world);
}

#[test]
#[allow(clippy::too_many_arguments)]
fn can_have_16_parameters() {
Expand Down
12 changes: 12 additions & 0 deletions crates/bevy_ecs/src/system/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,18 @@ where
>())),
}
}

/// Returns true if this query contains no elements.
#[inline]
pub fn is_empty(&self) -> bool {
NathanSWard marked this conversation as resolved.
Show resolved Hide resolved
// SAFE: system runs without conflicts with other systems.
// same-system queries have runtime borrow checks when they conflict
let mut iter = unsafe {
self.state
.iter_unchecked_manual(self.world, self.last_change_tick, self.change_tick)
};
iter.next().is_none()
}
}

/// An error that occurs when retrieving a specific [`Entity`]'s component from a [`Query`]
Expand Down