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

Convert queries to a more general form #10037

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 14 additions & 0 deletions crates/bevy_ecs/macros/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,26 @@ pub fn derive_world_query_impl(input: TokenStream) -> TokenStream {
)*
}

fn optional_access(
state: &Self::State,
_access: &mut #path::query::Access<#path::component::ComponentId>,
parent_is_optional: bool,
) {
#( <#field_types>::optional_access(&state.#named_field_idents, _access, parent_is_optional); )*
}

fn init_state(world: &mut #path::world::World) -> #state_struct_name #user_ty_generics {
#state_struct_name {
#(#named_field_idents: <#field_types>::init_state(world),)*
}
}

fn get_state(components: &#path::component::Components) -> Option<#state_struct_name #user_ty_generics> {
Some(#state_struct_name {
#(#named_field_idents: <#field_types>::get_state(components)?,)*
})
}

fn matches_component_set(state: &Self::State, _set_contains_id: &impl Fn(#path::component::ComponentId) -> bool) -> bool {
true #(&& <#field_types>::matches_component_set(&state.#named_field_idents, _set_contains_id))*
}
Expand Down
105 changes: 105 additions & 0 deletions crates/bevy_ecs/src/query/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,79 @@ impl<T: SparseSetIndex> Access<T> {
&& other.writes.is_disjoint(&self.reads_and_writes)
}

/// [`Access`] is subset of `other` access.
pub fn is_subset(&self, other: &Access<T>) -> bool {
if self.writes_all {
return other.writes_all;
}

if other.writes_all {
return true;
}

if self.reads_all {
return other.reads_all || other.writes_all;
}

if other.reads_all {
return !self.has_any_write();
}

let reads = self
.reads_and_writes
.difference(&self.writes)
.collect::<FixedBitSet>();

reads.is_subset(&other.reads_and_writes) && self.writes.is_subset(&other.writes)
}

/// Modify `self` with the intersection with `other`
pub fn intersect(&mut self, other: &Access<T>) {
if self.writes_all {
if other.writes_all {
// we intersect everything so we're done.
return;
}

self.writes_all = false;

if other.reads_all {
self.reads_all = true;
return;
}

self.reads_all = false;

self.reads_and_writes = other.reads_and_writes.clone();
self.writes = other.writes.clone();
return;
}

if self.reads_all {
if other.writes_all || other.reads_all {
return;
}

self.reads_and_writes = other.reads_and_writes.clone();
self.writes.clear();
return;
}

if other.writes_all {
// reads_and_writes and writes are already correct
return;
}

if other.reads_all {
self.writes.clear();
return;
}

self.reads_and_writes
.intersect_with(&other.reads_and_writes);
self.writes.intersect_with(&other.writes);
}

/// Returns a vector of elements that the access and `other` cannot access at the same time.
pub fn get_conflicts(&self, other: &Access<T>) -> Vec<T> {
let mut conflicts = FixedBitSet::default();
Expand Down Expand Up @@ -243,6 +316,17 @@ impl<T: SparseSetIndex> Access<T> {
pub fn writes(&self) -> impl Iterator<Item = T> + '_ {
self.writes.ones().map(T::get_sparse_set_index)
}

/// Returns `true` if the difference of `self` with `other` is empty
pub fn read_and_writes_difference_is_empty(&self, other: &Access<T>) -> bool {
if self.reads_all || self.writes_all {
return other.reads_all || other.writes_all;
}
self.reads_and_writes
.difference(&other.reads_and_writes)
.count()
== 0
}
}

/// An [`Access`] that has been filtered to include and exclude certain combinations of elements.
Expand Down Expand Up @@ -374,6 +458,27 @@ impl<T: SparseSetIndex> FilteredAccess<T> {
})
}

/// `other` contains all the same access as `self`. This does not take into account the `filter_sets`.
pub fn is_subset(&self, other: &FilteredAccess<T>) -> bool {
self.access.is_subset(&other.access)
}

/// Returns `true` if optional access has not changed.
pub fn is_optional_compatible(
&self,
mut original_optional: Access<T>,
new_optional: &Access<T>,
) -> bool {
if original_optional.has_any_read() {
original_optional.intersect(&self.access);
if !original_optional.read_and_writes_difference_is_empty(new_optional) {
return false;
}
}

true
}

/// Returns a vector of elements that this and `other` cannot access at the same time.
pub fn get_conflicts(&self, other: &FilteredAccess<T>) -> Vec<T> {
if !self.is_compatible(other) {
Expand Down
Loading