Hi,
Is there a way to do a Query on a immutable World? If yes, how?
I will illustrate with an example below.
This is a test I would like to be able to write:
fn test_empty_app_has_no_players() {
let app = App::new();
assert_eq!(count_n_players(&app), 0);
}
The idea of count_n_players is to count the number of times a (marker) component is present. Because we only read (i.e. do not modify the App), we can write let app (instead of let mut app).
Writing this test, however, fails when implementing count_n_players.
Below is an implementation that I wish I could write, that uses a Query on a (non-mut) World:
// Does not compile, as `query` expects a mutable World
fn count_n_players(app: &App) -> usize {
let query = app.world().query::<&Player>();
return query.iter(app.world()).len();
}
(there may be other implementations possible that do not use a query and I will give one below. My question, however, is about using queries on an immutable World)
However, a Query always needs a mutable World, hence an implementation that works is:
// Does not modify the App, I promise!
fn count_n_players(app: &mut App) -> usize {
let mut query = app.world_mut().query::<&Player>();
return query.iter(app.world_mut()).len();
}