Skip to content

Populated (query) system param#15488

Merged
alice-i-cecile merged 13 commits intobevyengine:mainfrom
MiniaczQ:query-non-empty
Sep 30, 2024
Merged

Populated (query) system param#15488
alice-i-cecile merged 13 commits intobevyengine:mainfrom
MiniaczQ:query-non-empty

Conversation

@MiniaczQ
Copy link
Copy Markdown
Contributor

@MiniaczQ MiniaczQ commented Sep 28, 2024

Objective

Add a Populated system parameter that acts like Query, but prevents system from running if there are no matching entities.

Fixes: #15302

Solution

Implement the system param which newtypes the Query.
The only change is new validation, which fails if query is empty.

The new system param is used in fallible_params example.

Testing

Ran fallible_params example.

@alice-i-cecile alice-i-cecile added this to the 0.15 milestone Sep 28, 2024
@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events S-Blocked This cannot move forward until something else changes X-Uncontroversial This work is generally agreed upon labels Sep 28, 2024
@MiniaczQ MiniaczQ added D-Unsafe Touches with unsafe code in some way M-Release-Note Work that should be called out in the blog due to impact labels Sep 28, 2024
@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Blocked This cannot move forward until something else changes labels Sep 28, 2024
@MiniaczQ MiniaczQ marked this pull request as ready for review September 28, 2024 20:02
@MiniaczQ MiniaczQ added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Sep 28, 2024
@BenjaminBrienen
Copy link
Copy Markdown
Contributor

It looks fine but this is too much unsafe for me to have any idea if it is correct

archetype: &Archetype,
system_meta: &mut SystemMeta,
) {
// SAFETY: Delegate to existing `SystemParam` implementations.
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.

SAFETY: Delegate to existing ``Query`` implementation.?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But then the doc comment says exactly what's written
The point is, we use a different SystemParam implementation which satisfies the same safety restrictions

world: UnsafeWorldCell<'w>,
change_tick: Tick,
) -> Self::Item<'w, 's> {
// SAFETY: Delegate to existing `SystemParam` implementations.
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.

Same here

state.validate_world(world.id());
// SAFETY:
// - We have read-only access to the components accessed by query.
// - The world has been validated.
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.

Might be clearer to say something like, The world matches the one used to create ``state``. (Just means the reader doesn't need to look at validate_world to know what it does)

Copy link
Copy Markdown
Contributor Author

@MiniaczQ MiniaczQ Sep 29, 2024

Choose a reason for hiding this comment

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

it's a common term in ECS safety comments,
I'm pretty sure all of the safety comments here are copy-pasted from other implementations

let query = unsafe {
Query::<'world, 'state, D, F>::get_param(state, system_meta, world, change_tick)
};
QueryNonEmpty(query)
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.

Is the plan for QueryNonEmpty to have some of its own methods or could it just return Query as its Item as its logic is just during validation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's just a newtyped Query, we can modify it in the future to be it's own thing which provides non-empty-based methods

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But I'd expect this to also require extracting traits for shared methods, bit too much work for this PR and not the focus

@MiniaczQ MiniaczQ changed the title QueryNonEmpty system param Populated (query) system param Sep 29, 2024
@MiniaczQ MiniaczQ requested a review from chescock September 29, 2024 18:41
@notmd
Copy link
Copy Markdown
Contributor

notmd commented Sep 29, 2024

How about Exists? That sound clearer for non native people like me.

@MiniaczQ
Copy link
Copy Markdown
Contributor Author

MiniaczQ commented Sep 29, 2024

How about Exists? That sound clearer for non native people like me.

I'm afraid that has different meaning from intended

Copy link
Copy Markdown
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.

Lovely; nice and simple (you know, as far as manual SystemParam impls go) and I really like the end user API. Good docs too!

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Sep 29, 2024
MiniaczQ and others added 2 commits September 30, 2024 01:01
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Sep 30, 2024
Merged via the queue into bevyengine:main with commit fc93e13 Sep 30, 2024
robtfm pushed a commit to robtfm/bevy that referenced this pull request Oct 4, 2024
# Objective

Add a `Populated` system parameter that acts like `Query`, but prevents
system from running if there are no matching entities.

Fixes: bevyengine#15302

## Solution

Implement the system param which newtypes the `Query`.
The only change is new validation, which fails if query is empty.

The new system param is used in `fallible_params` example.

## Testing

Ran `fallible_params` example.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
@alice-i-cecile
Copy link
Copy Markdown
Member

Thank you to everyone involved with the authoring or reviewing of this PR! This work is relatively important and needs release notes! Head over to bevyengine/bevy-website#1706 if you'd like to help out.

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 M-Release-Note Work that should be called out in the blog due to impact S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce Populated system param

7 participants