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

Populated (query) system param #15488

Merged
merged 13 commits into from
Sep 30, 2024
Merged

Conversation

MiniaczQ
Copy link
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-Needs-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
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
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
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
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
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
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
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
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
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
crates/bevy_ecs/src/system/system_param.rs Outdated Show resolved Hide resolved
crates/bevy_ecs/src/system/system_param.rs Outdated Show resolved Hide resolved
crates/bevy_ecs/src/system/query.rs Show resolved Hide resolved
crates/bevy_ecs/src/system/query.rs Show resolved Hide resolved
@notmd
Copy link
Contributor

notmd commented Sep 29, 2024

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

@MiniaczQ
Copy link
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
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
@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
26 checks passed
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 <[email protected]>
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-Needs-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