Add num_entities() to World (#19780)

# Objective

There is a lot of `world.entities().len()`, especially in tests. In
tests, usually, the assumption is made that empty worlds do not contain
any entities. This is about to change (#19711), and as such all of these
tests are failing for that PR.

## Solution

`num_entities` is a convenience method that returns the number of
entities inside a world. It can later be adapted to exclude 'unexpected'
entities, associated with internal data structures such as Resources,
Queries, Systems. In general I argue for a separation of concepts where
`World` ignores internal entities in methods such as `iter_entities()`
and `clear_entities()`, that discussion is, however, separate from this
PR.

## Testing

I replaced most occurrences of `world.entities().len()` with
`world.num_entities()` and the tests passed.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
This commit is contained in:
Trashtalk217 2025-06-23 00:48:35 +02:00 committed by GitHub
parent c6ae964709
commit 6dbe3600ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 16 additions and 8 deletions

View File

@ -1582,7 +1582,7 @@ mod tests {
app.add_systems(EnterMainMenu, (foo, bar));
app.world_mut().run_schedule(EnterMainMenu);
assert_eq!(app.world().entities().len(), 2);
assert_eq!(app.world().num_entities(), 2);
}
#[test]

View File

@ -1637,7 +1637,7 @@ mod tests {
assert_eq!(q1.iter(&world).len(), 1);
assert_eq!(q2.iter(&world).len(), 1);
assert_eq!(world.entities().len(), 2);
assert_eq!(world.num_entities(), 2);
world.clear_entities();
@ -1652,7 +1652,7 @@ mod tests {
"world should not contain sparse set components"
);
assert_eq!(
world.entities().len(),
world.num_entities(),
0,
"world should not have any entities"
);

View File

@ -1315,7 +1315,7 @@ mod tests {
world.spawn(A).flush();
assert_eq!(vec!["add_2", "add_1"], world.resource::<Order>().0);
// Our A entity plus our two observers
assert_eq!(world.entities().len(), 3);
assert_eq!(world.num_entities(), 3);
}
#[test]

View File

@ -2342,7 +2342,7 @@ mod tests {
.spawn((W(1u32), W(2u64)))
.id();
command_queue.apply(&mut world);
assert_eq!(world.entities().len(), 1);
assert_eq!(world.num_entities(), 1);
let results = world
.query::<(&W<u32>, &W<u64>)>()
.iter(&world)

View File

@ -423,12 +423,12 @@ mod test {
let mut world = World::new();
queue.apply(&mut world);
assert_eq!(world.entities().len(), 2);
assert_eq!(world.num_entities(), 2);
// The previous call to `apply` cleared the queue.
// This call should do nothing.
queue.apply(&mut world);
assert_eq!(world.entities().len(), 2);
assert_eq!(world.num_entities(), 2);
}
#[expect(
@ -462,7 +462,7 @@ mod test {
queue.push(SpawnCommand);
queue.push(SpawnCommand);
queue.apply(&mut world);
assert_eq!(world.entities().len(), 3);
assert_eq!(world.num_entities(), 3);
}
#[test]

View File

@ -216,6 +216,14 @@ impl World {
&mut self.entities
}
/// Retrieves the number of [`Entities`] in the world.
///
/// This is helpful as a diagnostic, but it can also be used effectively in tests.
#[inline]
pub fn num_entities(&self) -> u32 {
self.entities.len()
}
/// Retrieves this world's [`Archetypes`] collection.
#[inline]
pub fn archetypes(&self) -> &Archetypes {