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

Clarify a comment in Option WorldQuery impl #9749

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,10 +1218,15 @@ unsafe impl<T: WorldQuery> WorldQuery for Option<T> {
}

fn update_component_access(state: &T::State, access: &mut FilteredAccess<ComponentId>) {
// We don't want to add the `with`/`without` of `T` as `Option<T>` will match things regardless of
// `T`'s filters. for example `Query<(Option<&U>, &mut V)>` will match every entity with a `V` component
// regardless of whether it has a `U` component. If we don't do this the query will not conflict with
// `Query<&mut V, Without<U>>` which would be unsound.
// FilteredAccess::add_[write,read] adds the component to the `with` filter.
// Those methods are called on `access` in `T::update_component_access`.
// But in `Option<T>`, we specifically don't filter on `T`,
// since `(Option<T>, &OtherComponent)` should be a valid item, even
// if `Option<T>` is `None`.
//
// We pass a clone of the `FilteredAccess` to `T`, and only update the `Access`
// of `access` using `extend_access` so that we can apply `T`'s component_access
// without updating the `with` filters of `access`.
let mut intermediate = access.clone();
T::update_component_access(state, &mut intermediate);
access.extend_access(&intermediate);
Expand Down