forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows conversion of mutable queries to immutable queries (bevyengine…
…#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`.
- Loading branch information
1 parent
e4c6e77
commit 617d10f
Showing
4 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
crates/bevy_ecs_compile_fail_tests/tests/ui/query_to_readonly.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
use bevy_ecs::prelude::*; | ||
|
||
#[derive(Component, Debug)] | ||
struct Foo; | ||
|
||
fn for_loops(mut query: Query<&mut Foo>) { | ||
// this should fail to compile | ||
for _ in query.iter_mut() { | ||
for _ in query.to_readonly().iter() {} | ||
} | ||
|
||
// this should fail to compile | ||
for _ in query.to_readonly().iter() { | ||
for _ in query.iter_mut() {} | ||
} | ||
|
||
// this should *not* fail to compile | ||
for _ in query.to_readonly().iter() { | ||
for _ in query.to_readonly().iter() {} | ||
} | ||
|
||
// this should *not* fail to compile | ||
for _ in query.to_readonly().iter() { | ||
for _ in query.iter() {} | ||
} | ||
|
||
// this should *not* fail to compile | ||
for _ in query.iter() { | ||
for _ in query.to_readonly().iter() {} | ||
} | ||
} | ||
|
||
fn single_mut_query(mut query: Query<&mut Foo>) { | ||
// this should fail to compile | ||
{ | ||
let mut mut_foo = query.single_mut(); | ||
|
||
// This solves "temporary value dropped while borrowed" | ||
let readonly_query = query.to_readonly(); | ||
|
||
let ref_foo = readonly_query.single(); | ||
|
||
*mut_foo = Foo; | ||
|
||
println!("{ref_foo:?}"); | ||
} | ||
|
||
// this should fail to compile | ||
{ | ||
// This solves "temporary value dropped while borrowed" | ||
let readonly_query = query.to_readonly(); | ||
|
||
let ref_foo = readonly_query.single(); | ||
|
||
let mut mut_foo = query.single_mut(); | ||
|
||
println!("{ref_foo:?}"); | ||
|
||
*mut_foo = Foo; | ||
} | ||
|
||
// this should *not* fail to compile | ||
{ | ||
// This solves "temporary value dropped while borrowed" | ||
let readonly_query = query.to_readonly(); | ||
|
||
let readonly_foo = readonly_query.single(); | ||
|
||
let query_foo = query.single(); | ||
|
||
println!("{readonly_foo:?}, {query_foo:?}"); | ||
} | ||
} | ||
|
||
fn main() {} |
45 changes: 45 additions & 0 deletions
45
crates/bevy_ecs_compile_fail_tests/tests/ui/query_to_readonly.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
error[E0502]: cannot borrow `query` as immutable because it is also borrowed as mutable | ||
--> tests/ui/query_to_readonly.rs:9:18 | ||
| | ||
8 | for _ in query.iter_mut() { | ||
| ---------------- | ||
| | | ||
| mutable borrow occurs here | ||
| mutable borrow later used here | ||
9 | for _ in query.to_readonly().iter() {} | ||
| ^^^^^^^^^^^^^^^^^^^ immutable borrow occurs here | ||
|
||
error[E0502]: cannot borrow `query` as mutable because it is also borrowed as immutable | ||
--> tests/ui/query_to_readonly.rs:14:18 | ||
| | ||
13 | for _ in query.to_readonly().iter() { | ||
| -------------------------- | ||
| | | ||
| immutable borrow occurs here | ||
| immutable borrow later used here | ||
14 | for _ in query.iter_mut() {} | ||
| ^^^^^^^^^^^^^^^^ mutable borrow occurs here | ||
|
||
error[E0502]: cannot borrow `query` as immutable because it is also borrowed as mutable | ||
--> tests/ui/query_to_readonly.rs:39:30 | ||
| | ||
36 | let mut mut_foo = query.single_mut(); | ||
| ------------------ mutable borrow occurs here | ||
... | ||
39 | let readonly_query = query.to_readonly(); | ||
| ^^^^^^^^^^^^^^^^^^^ immutable borrow occurs here | ||
... | ||
43 | *mut_foo = Foo; | ||
| ------- mutable borrow later used here | ||
|
||
error[E0502]: cannot borrow `query` as mutable because it is also borrowed as immutable | ||
--> tests/ui/query_to_readonly.rs:55:27 | ||
| | ||
51 | let readonly_query = query.to_readonly(); | ||
| ------------------- immutable borrow occurs here | ||
... | ||
55 | let mut mut_foo = query.single_mut(); | ||
| ^^^^^^^^^^^^^^^^^^ mutable borrow occurs here | ||
56 | | ||
57 | println!("{ref_foo:?}"); | ||
| ------- immutable borrow later used here |