Skip to content

Commit

Permalink
impl ReadOnlyFetch for Or, FetchOr, and FetchMutated
Browse files Browse the repository at this point in the history
  • Loading branch information
memoryruins committed Nov 1, 2020
1 parent 9cc6368 commit 06da948
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/bevy_ecs/hecs/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),+)> {}
};
}

Expand Down Expand Up @@ -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>;
Expand Down
37 changes: 37 additions & 0 deletions crates/bevy_ecs/src/system/into_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>) {
Expand Down

0 comments on commit 06da948

Please sign in to comment.