bevyengine / bevyengine/bevy

Allow query transmutation with immutable query references

Open
#14,429 5 comments 0 reactions 0 assignees View on GitHub
A-ECS C-Usability D-Complex S-Needs-Design
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

Consider the following example:

```rust
#[derive(Component)]
struct A { }
#[derive(Component)]
struct B { }

fn system1(
my_query: Query<(&A, &B)>
) {
fn immutable_helper(subquery: &Query<(&A)>) {
for x in subquery.iter() {
// bla bla
}
}
let lens_a = my_query.transmute_lens::<(&A)>();
immutable_helper(&lens_a.query())
}

```

Why does using `transmute_lens()` and `query()` require me to have mutable reference to the query if the query itself is not mutating anything? This prevents me from using transmute lens if the destination of the transmute result accepts only immutable query references. Is there a way around this without forcing the user to perform painful refactorings for all helper functions to accept mutable references of the query, assuming that is even always possible?

Going down the path of definitions from `transmute_lens`, it would also seem that final transmute function call [takes immutable reference anyway](https://github.com/bevyengine/bevy/blob/08d3497d87f02005603116866ec6730fb05a7445/crates/bevy_ecs/src/query/state.rs#L488), so maybe this feature could be implemented without much hassle?

Contributor guide

Open the contributing guide

Research direction

Start at transmute_lens and the linked implementation in crates/bevy_ecs/src/query/state.rs, then trace how query() enforces a mutable reference. Determine whether the transmutation can support immutable query references without breaking existing borrowing guarantees. Done means the example can pass the transmuted query to immutable_helper without refactoring it to accept a mutable reference.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.