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

Make QueryState::transmute&co validate the world of the &Components used #14631

Merged
merged 4 commits into from
Aug 5, 2024

Conversation

SkiFire13
Copy link
Contributor

@SkiFire13 SkiFire13 commented Aug 5, 2024

Objective

Solution

  • Make QueryState::transmute, QueryState::transmute_filtered, QueryState::join and QueryState::join_filtered take a impl Into<UnsafeWorldCell> instead of a &Components and validate their WorldId

Migration Guide

  • QueryState::transmute, QueryState::transmute_filtered, QueryState::join and QueryState::join_filtered now take a impl Into<UnsafeWorldCell> instead of a &Components

@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use P-Unsound A bug that results in undefined compiler behavior labels Aug 5, 2024
@alice-i-cecile alice-i-cecile added this to the 0.15 milestone Aug 5, 2024
@alice-i-cecile alice-i-cecile added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Aug 5, 2024
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Nice clean design, good docs and a regression test! Can't ask for more.

@alice-i-cecile alice-i-cecile added the S-Needs-Review Needs reviewer attention (from anyone!) to move forward label Aug 5, 2024
Copy link
Member

@cart cart left a comment

Choose a reason for hiding this comment

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

I think this design does work nicely, but I can't help but think that metadata() is just an arbitrary (and redundant) slice of UnsafeWorldCell. Rather than introduce a new name / concept, I'm thinking we should just add the missing method to UnsafeWorldCell and use that instead.

As a proposal, I've pushed that change so we can discuss. Somewhat controversially, it includes From<&World> for UnsafeWorldCell and changed the parameter in the relevant functions to accept Into<UnsafeWorldCell>, which allows us to just pass &World into them. Im pretty sure not including From<&World> impls for UnsafeWorldCell was an intentional design decision to make the conversion explicit, but given that it is completely safe to create an UnsafeWorldCell, and given that it is increasingly used in our APIs, I think adding this impl is both acceptable and desirable.

@alice-i-cecile
Copy link
Member

given that it is completely safe to create an UnsafeWorldCell, and given that it is increasingly used in our APIs, I think adding this impl is both acceptable and desirable.

Agreed here.

Rather than introduce a new name / concept, I'm thinking we should just add the missing method to UnsafeWorldCell and use that instead.

I think this is a reasonable proposal: it's nice to keep things simple.

@SkiFire13
Copy link
Contributor Author

My initial motivation for making it a separate struct was that I wanted it to be as easy to obtain and use as &Components, &Entities and similar. On the other hand UnsafeWorldCell sounds scary and doesn't implement SystemParam either, making it more difficult to obtain (you don't always have a &World available). In hindsight however this is required only when interfacing with QueryState (Query will use its internal UnsafeWorldCell), so maybe this isn't really needed?

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

I like this new diff better :) @SkiFire13 if you're on board with this new plan / diff, let me know and I'll count it as a second approval.

@SkiFire13
Copy link
Contributor Author

I like this new diff better :) @SkiFire13 if you're on board with this new plan / diff, let me know and I'll count it as a second approval.

Looks good to me.

@SkiFire13 SkiFire13 changed the title Add WorldMetadata and replace QueryState::transmute's &Components parameter with it Make QueryState::transmute&co validate the world of the &Components used Aug 5, 2024
@SkiFire13
Copy link
Contributor Author

Updated the first comment and title to reflect cart's changes

@cart cart added this pull request to the merge queue Aug 5, 2024
Merged via the queue into bevyengine:main with commit 68ec6f4 Aug 5, 2024
26 checks passed
@SkiFire13 SkiFire13 deleted the world-metadata branch August 6, 2024 06:15
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-Bug An unexpected or incorrect behavior C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide P-Unsound A bug that results in undefined compiler behavior S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Query::transmute/Query::transmute_filtered accept any &Components parameter and this is unsound
3 participants