Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast query of mutable components to query of immutable #4606

Closed
michalmoc opened this issue Apr 26, 2022 · 1 comment
Closed

Cast query of mutable components to query of immutable #4606

michalmoc opened this issue Apr 26, 2022 · 1 comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use

Comments

@michalmoc
Copy link

michalmoc commented Apr 26, 2022

Because of Rust borrow checker I'm usually forced to pass around entities and queries. But there seems to be a problem when I've got a mutable query but it suffices to have immutable one, like

fn some_function(entities: &Vec<Entity>, query: &Query<&SomeComponent>){...}

fn some_system(some_entities: Res<Vec<Entity>>, query: Query<&mut SomeComponent>)
{
    for mut c in query.iter_mut() {...};

    some_function(some_entities, query);
}

As far as I can see there is no way to cast the query although it seems reasonable to do so. I believe there should exist some implementation of AsRef trait or perhaps some new trait like CanQuery<ComponentType>.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Apr 26, 2022
@alice-i-cecile
Copy link
Member

I agree here. I'd really like to see a unified API for casting mutable ECS data into its immutable form, in order to resolve this sort of challenge. You see this with e.g. ResMut and NonSendMut as well.

@bors bors bot closed this as completed in 959f3b1 Jul 20, 2022
inodentry pushed a commit to IyesGames/bevy that referenced this issue Aug 8, 2022
…#5376)

# Objective

- Allows conversion of mutable queries to immutable queries.
- Fixes bevyengine#4606

## Solution

- Add `to_readonly` method on `Query`, which uses `QueryState::as_readonly`
- `AsRef` is not feasible because creation of new queries is needed.

---

## Changelog

### Added

- Allows conversion of mutable queries to immutable queries using `Query::to_readonly`.
james7132 pushed a commit to james7132/bevy that referenced this issue Oct 28, 2022
…#5376)

# Objective

- Allows conversion of mutable queries to immutable queries.
- Fixes bevyengine#4606

## Solution

- Add `to_readonly` method on `Query`, which uses `QueryState::as_readonly`
- `AsRef` is not feasible because creation of new queries is needed.

---

## Changelog

### Added

- Allows conversion of mutable queries to immutable queries using `Query::to_readonly`.
ItsDoot pushed a commit to ItsDoot/bevy that referenced this issue Feb 1, 2023
…#5376)

# Objective

- Allows conversion of mutable queries to immutable queries.
- Fixes bevyengine#4606

## Solution

- Add `to_readonly` method on `Query`, which uses `QueryState::as_readonly`
- `AsRef` is not feasible because creation of new queries is needed.

---

## Changelog

### Added

- Allows conversion of mutable queries to immutable queries using `Query::to_readonly`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants