Allow iterating over with EntityRef over the entire World (#6843)

# Objective
Partially addresses #5504. Allow users to get an `Iterator<Item = EntityRef<'a>>` over all entities in the `World`.

## Solution
Change `World::iter_entities` to return an iterator of `EntityRef` instead of `Entity`.

Not sure how to tackle making an `Iterator<Item = EntityMut<'_>>` without being horribly unsound. Might need to wait for `LendingIterator` to stabilize so we can ensure only one of them is valid at a given time.

---

## Changelog
Changed: `World::iter_entities` now returns an iterator of `EntityRef` instead of `Entity`.
This commit is contained in:
James Liu 2022-12-05 22:35:02 +00:00
parent 05b498a224
commit e8c0df9e1e
2 changed files with 17 additions and 8 deletions

View file

@ -14,7 +14,7 @@ use crate::{
component::{ component::{
Component, ComponentDescriptor, ComponentId, ComponentInfo, Components, TickCells, Component, ComponentDescriptor, ComponentId, ComponentInfo, Components, TickCells,
}, },
entity::{AllocAtWithoutReplacement, Entities, Entity}, entity::{AllocAtWithoutReplacement, Entities, Entity, EntityLocation},
query::{QueryState, ReadOnlyWorldQuery, WorldQuery}, query::{QueryState, ReadOnlyWorldQuery, WorldQuery},
storage::{ResourceData, SparseSet, Storages}, storage::{ResourceData, SparseSet, Storages},
system::Resource, system::Resource,
@ -323,11 +323,20 @@ impl World {
/// ///
/// This is useful in contexts where you only have read-only access to the [`World`]. /// This is useful in contexts where you only have read-only access to the [`World`].
#[inline] #[inline]
pub fn iter_entities(&self) -> impl Iterator<Item = Entity> + '_ { pub fn iter_entities(&self) -> impl Iterator<Item = EntityRef<'_>> + '_ {
self.archetypes self.archetypes.iter().flat_map(|archetype| {
.iter() archetype
.flat_map(|archetype| archetype.entities().iter()) .entities()
.map(|archetype_entity| archetype_entity.entity()) .iter()
.enumerate()
.map(|(index, archetype_entity)| {
let location = EntityLocation {
archetype_id: archetype.id(),
index,
};
EntityRef::new(self, archetype_entity.entity(), location)
})
})
} }
/// Retrieves an [`EntityMut`] that exposes read and write operations for the given `entity`. /// Retrieves an [`EntityMut`] that exposes read and write operations for the given `entity`.
@ -1870,7 +1879,7 @@ mod tests {
let iterate_and_count_entities = |world: &World, entity_counters: &mut HashMap<_, _>| { let iterate_and_count_entities = |world: &World, entity_counters: &mut HashMap<_, _>| {
entity_counters.clear(); entity_counters.clear();
for entity in world.iter_entities() { for entity in world.iter_entities() {
let counter = entity_counters.entry(entity).or_insert(0); let counter = entity_counters.entry(entity.id()).or_insert(0);
*counter += 1; *counter += 1;
} }
}; };

View file

@ -46,7 +46,7 @@ impl DynamicScene {
let mut builder = let mut builder =
DynamicSceneBuilder::from_world_with_type_registry(world, type_registry.clone()); DynamicSceneBuilder::from_world_with_type_registry(world, type_registry.clone());
builder.extract_entities(world.iter_entities()); builder.extract_entities(world.iter_entities().map(|entity| entity.id()));
builder.build() builder.build()
} }