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
12 changes: 12 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,18 @@ mod tests {
assert_eq!(conflicts, vec![b_id, d_id]);
}

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

let mut world = World::default();
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
11 changes: 11 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,17 @@ where
>())),
}
}

/// Returns true if this query contains no elements.
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 iter = unsafe {
self.state
.iter_unchecked_manual(self.world, self.last_change_tick, self.change_tick)
};
iter.peekable().peek().is_none()
NathanSWard marked this conversation as resolved.
Show resolved Hide resolved
}
}

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