Restore separate methods for World::get_many_entities
#18234
+443
−766
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Objective
Reduce the complexity of the
World::get_entity
API by removing theF: WorldEntityFetch
type parameter and restoring separateWorld::get_many_entities
methods.It would be good for consistency to have
Query::get
andWorld::get_entity
work the same way, but per discussion on #18036 (comment),WorldEntityFetch
was too much complexity forQuery::get
. Instead, let's removeWorldEntityFetch
and makeWorld::get_entity
work likeQuery::get
.This undoes #15614 (although I did not attempt to implement it as a revert). A few things have changed since then that makes the duplication less of a problem than it was when
WorldEntityFetch
was introduced:One change is that Bevy has improved handling for
Result
s, so we no longer expect to offer panicking versions of all APIs, and we do not need to restore the panickingmany_entities
andmany_entities_mut
.Another change is the introduction of
EntitySet
and related traits in #16547. This lets us generalize over types that hold sets of uniqueEntity
s, so we do not need a separate method for every such type.Solution
Remove
F: WorldEntityFetch
parameter fromWorld::get_entity()
and related methods, and have it only acceptEntity
.Introduce new methods to handle the various "many" cases:
get_many_entities
takes an array and returns an array ofEntityRef
. These are read-only, so duplicates are okay.iter_entities
takes an iterator and returns an iterator ofEntityRef
. These are read-only, so duplicates are okay.get_many_entities_mut
takes an array and returns an array ofEntityMut
. It performs the O(N^2) duplicate check. This is acceptable because we expect N to be small for arrays.get_many_entities_unique_mut
takes aUniqueEntityArray
and returns an array ofEntityMut
.UniqueEntityArray
ensures there are no duplicates.iter_entities_mut
takes anEntitySet
and returns an iterator ofEntityMut
.EntitySet
ensures there are no duplicates.Also rename the existing
iter_entities(_mut)
methods toiter_all_entities(_mut)
, to free up the name.Migration Guide
World::entity()
,World::entity_mut()
,World::get_entity()
, andWorld::get_entity_mut()
now only take a singleEntity
instead of being generic and accepting collections. If you were passing a collection, you will need to call a different method.If you were using a panicking variant, first replace it with
get_entity(e).unwrap()
orget_entity_mut(e).unwrap()
.Then, replace:
World::get_entity::<[Entity; N]>
->World::get_many_entities()
World::get_entity_mut::<[Entity; N]>
->World::get_many_entities_mut()
World::get_entity::<&[Entity]>
->World::iter_entities().collect::<Vec<_>>()
World::get_entity_mut::<&[Entity]>
-> This has no direct equivalent, as it performed an O(N^2) check that can be expensive for large slices. First convert the collection to anEntitySet
, such asEntityHashSet
, then callWorld::iter_entities(e)
.World::get_entity::<&EntityHashSet>
->World::iter_entities(e).map(|e| (e.id(), e)).collect::<EntityHashMap<_>>()
World::get_entity_mut::<&EntityHashSet>
->World::iter_entities_mut(e).map(|e| (e.id(), e)).collect::<EntityHashMap<_>>()
In addition,
World::iter_entities(_mut)
has been renamediter_all_entities(_mut)
to make the name available for these new methods. If you were callingiter_entities
oriter_entities_mut
, the functionality is still available under the new name.