impl ReadOnlyFetch for Or, FetchOr, and FetchMutated (#763)

This commit is contained in:
memoryruins 2020-11-01 19:51:51 -05:00 committed by GitHub
parent 5cd67f7867
commit f81208adaa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 0 deletions

View file

@ -263,6 +263,9 @@ macro_rules! impl_or_query {
true $( && $T.should_skip(n) )+
}
}
unsafe impl<$( $T: ReadOnlyFetch ),+> ReadOnlyFetch for Or<($( $T ),+)> {}
unsafe impl<$( $T: ReadOnlyFetch ),+> ReadOnlyFetch for FetchOr<($( $T ),+)> {}
};
}
@ -320,6 +323,7 @@ impl<'a, T: Component> Query for Mutated<'a, T> {
#[doc(hidden)]
pub struct FetchMutated<T>(NonNull<T>, NonNull<bool>);
unsafe impl<T> ReadOnlyFetch for FetchMutated<T> {}
impl<'a, T: Component> Fetch<'a> for FetchMutated<T> {
type Item = Mutated<'a, T>;

View file

@ -489,6 +489,43 @@ mod tests {
assert!(*resources.get::<bool>().unwrap(), "system ran");
}
#[test]
fn or_query_set_system() {
// Regression test for issue #762
use crate::{Added, Changed, Mutated, Or};
fn query_system(
mut ran: ResMut<bool>,
set: QuerySet<(
Query<Or<(Changed<A>, Changed<B>)>>,
Query<Or<(Added<A>, Added<B>)>>,
Query<Or<(Mutated<A>, Mutated<B>)>>,
)>,
) {
let changed = set.q0().iter().count();
let added = set.q1().iter().count();
let mutated = set.q2().iter().count();
assert_eq!(changed, 1);
assert_eq!(added, 1);
assert_eq!(mutated, 0);
*ran = true;
}
let mut world = World::default();
let mut resources = Resources::default();
resources.insert(false);
world.spawn((A, B));
let mut schedule = Schedule::default();
schedule.add_stage("update");
schedule.add_system_to_stage("update", query_system.system());
schedule.run(&mut world, &mut resources);
assert!(*resources.get::<bool>().unwrap(), "system ran");
}
#[test]
fn changed_resource_system() {
fn incr_e_on_flip(_run_on_flip: ChangedRes<bool>, mut i: Mut<i32>) {