Skip to content

Safe method for multiple mutable references to components from Query #2042

@engiwengi

Description

@engiwengi

What problem does this solve or what need does it fill?

It is sometimes useful/easier to have mutable access to the same component of many different entities at once.

What solution would you like?

Two new methods for Query, a default and one for a component. Example:

fn get_many_mut<'a, Q: WorldQuery, F: WorldQuery>(
    entities: impl IntoIterator<Item = Entity>,
    query: &'a mut Query<Q, F>,
) -> Result<Vec<<Q::Fetch as Fetch<'a>>::Item>, MultiEntityQueryErr>
where
    F::Fetch: FilterFetch,
{
    let mut unique = HashSet::default();
    let mut iter = entities.into_iter();
    if iter.by_ref().all(|x| unique.insert(x)) {
        let mut result = Vec::with_capacity(unique.len());

        for entity in iter {
            match unsafe { query.get_unchecked(entity) } {
                Ok(item) => {
                    result.push(item);
                }
                Err(err) => {
                    return Err(MultiEntityQueryErr::QueryEntityError(err));
                }
            }
        }

        Ok(result)
    } else {
        Err(MultiEntityQueryErr::DuplicateEntities)
    }
}

Just a quick implementation I've already written, not necessarily the best. Originally I used const generic arrays to avoid heap allocation too - but depends on use case.

What alternative(s) have you considered?

Have users implement it themselves using the existing unsafe Query methods, like above, and worry their use of unsafe will cause issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possibleS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions