Change World::inspect_entity to return an Iterator instead of Vec (#13934)

# Objective

Fixes #13933.

## Solution

Changed the return type.

## Testing

Fixed and reused the pre-existing tests for `inspect_entity`.

---

## Migration Guide

- `World::inspect_entity` now returns an `Iterator` instead of a `Vec`.
If you need a `Vec`, immediately collect the iterator:
`world.inspect_entity(entity).collect<Vec<_>>()`
This commit is contained in:
Christian Hughes 2024-06-19 16:06:35 -05:00 committed by GitHub
parent e34ecf2f86
commit ee2487a6e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 10 deletions

View File

@ -1321,7 +1321,6 @@ fn insert_resource<R: Resource>(resource: R) -> impl Command {
fn log_components(entity: Entity, world: &mut World) { fn log_components(entity: Entity, world: &mut World) {
let debug_infos: Vec<_> = world let debug_infos: Vec<_> = world
.inspect_entity(entity) .inspect_entity(entity)
.into_iter()
.map(|component_info| component_info.name()) .map(|component_info| component_info.name())
.collect(); .collect();
info!("Entity {:?}: {:?}", entity, debug_infos); info!("Entity {:?}: {:?}", entity, debug_infos);

View File

@ -479,7 +479,7 @@ impl World {
/// Returns the components of an [`Entity`] through [`ComponentInfo`]. /// Returns the components of an [`Entity`] through [`ComponentInfo`].
#[inline] #[inline]
pub fn inspect_entity(&self, entity: Entity) -> Vec<&ComponentInfo> { pub fn inspect_entity(&self, entity: Entity) -> impl Iterator<Item = &ComponentInfo> {
let entity_location = self let entity_location = self
.entities() .entities()
.get(entity) .get(entity)
@ -498,7 +498,6 @@ impl World {
archetype archetype
.components() .components()
.filter_map(|id| self.components().get_info(id)) .filter_map(|id| self.components().get_info(id))
.collect()
} }
/// Returns an [`EntityWorldMut`] for the given `entity` (if it exists) or spawns one if it doesn't exist. /// Returns an [`EntityWorldMut`] for the given `entity` (if it exists) or spawns one if it doesn't exist.
@ -3162,31 +3161,31 @@ mod tests {
let bar_id = TypeId::of::<Bar>(); let bar_id = TypeId::of::<Bar>();
let baz_id = TypeId::of::<Baz>(); let baz_id = TypeId::of::<Baz>();
assert_eq!( assert_eq!(
to_type_ids(world.inspect_entity(ent0)), to_type_ids(world.inspect_entity(ent0).collect()),
[Some(foo_id), Some(bar_id), Some(baz_id)].into() [Some(foo_id), Some(bar_id), Some(baz_id)].into()
); );
assert_eq!( assert_eq!(
to_type_ids(world.inspect_entity(ent1)), to_type_ids(world.inspect_entity(ent1).collect()),
[Some(foo_id), Some(bar_id)].into() [Some(foo_id), Some(bar_id)].into()
); );
assert_eq!( assert_eq!(
to_type_ids(world.inspect_entity(ent2)), to_type_ids(world.inspect_entity(ent2).collect()),
[Some(bar_id), Some(baz_id)].into() [Some(bar_id), Some(baz_id)].into()
); );
assert_eq!( assert_eq!(
to_type_ids(world.inspect_entity(ent3)), to_type_ids(world.inspect_entity(ent3).collect()),
[Some(foo_id), Some(baz_id)].into() [Some(foo_id), Some(baz_id)].into()
); );
assert_eq!( assert_eq!(
to_type_ids(world.inspect_entity(ent4)), to_type_ids(world.inspect_entity(ent4).collect()),
[Some(foo_id)].into() [Some(foo_id)].into()
); );
assert_eq!( assert_eq!(
to_type_ids(world.inspect_entity(ent5)), to_type_ids(world.inspect_entity(ent5).collect()),
[Some(bar_id)].into() [Some(bar_id)].into()
); );
assert_eq!( assert_eq!(
to_type_ids(world.inspect_entity(ent6)), to_type_ids(world.inspect_entity(ent6).collect()),
[Some(baz_id)].into() [Some(baz_id)].into()
); );
} }