Skip to content

Mutable NestedQuery with wrapper that erases QueryData type - #25809

Open
chescock wants to merge 8 commits into
bevyengine:mainfrom
chescock:mutable-nested-query-erased-query-state
Open

chescock wants to merge 8 commits into
bevyengine:mainfrom
chescock:mutable-nested-query-erased-query-state

Conversation

@chescock

Copy link
Copy Markdown
Contributor

Objective

Support mutable queries using NestedQuery.

The reason this was not supported in the initial version is that we want NestedQuery<D, F>::ReadOnly == NestedQuery<D::ReadOnly, F>, but that doesn't satisfy the ReadOnly::State == State constraint since QueryState<D::ReadOnly, F> != QueryState<D, F>.

Solution

Store QueryState<D::ReadOnly, F> in the State. Since D::ReadOnly::ReadOnly == D::ReadOnly, that ensures the states match.

In order to simplify the safety proofs and ensure we always convert it back to the appropriate type, introduce an ErasedQueryState wrapper type that prevents accidental access to the inner QueryState<D::ReadOnly, F>.

Adopted and modified from #25642. See also #25652 for context.

@chescock chescock added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events D-Unsafe Touches with unsafe code in some way S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 16, 2026
@github-project-automation github-project-automation Bot moved this to Needs SME Triage in ECS Sep 16, 2026
@Zeophlite Zeophlite added the S-Merge-Conflicts Merge conflicts :( Add this label on top of other S- labels. label Sep 19, 2026
…d-query-erased-query-state

# Conflicts:
#	crates/bevy_ecs/src/query/fetch.rs
@chescock chescock removed the S-Merge-Conflicts Merge conflicts :( Add this label on top of other S- labels. label Sep 23, 2026

@hymm hymm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code looks good, but can we add some tests for the new functionality

Show examples of mutable queries, and document how to set `ReadOnly` safely.
@chescock

Copy link
Copy Markdown
Contributor Author

code looks good, but can we add some tests for the new functionality

What sort of tests do you have in mind? Note that I did extend the doc tests! There's an example of Query<NestedQuery<&mut A>> that shows that iter() and get_mut() work but iter_mut() doesn't, and the Parent<D, F> example now uses QueryData instead of ReadOnlyQueryData.

... Oh, the actual usage for Parent is still &Data, though. Let me use &mut Data there, too, and add some docs for how to set type ReadOnly.

This branch has not been deployed

No deployments
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-Feature A new feature, making something new possible D-Unsafe Touches with unsafe code in some way S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

Status: Needs SME Triage

Development

Successfully merging this pull request may close these issues.

4 participants