Skip to content

Commit

Permalink
Fix Added behaviour for QueryOne get. (#543)
Browse files Browse the repository at this point in the history
Query unchanged as impacts performances.
Added tests in bevy_ecs/hecs
  • Loading branch information
BorisBoutillier authored Oct 5, 2020
1 parent 9a4167e commit 1bdb9d3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
12 changes: 10 additions & 2 deletions crates/bevy_ecs/hecs/src/query_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ impl<'a, Q: Query> QueryOne<'a, Q> {
pub fn get(&mut self) -> Option<<Q::Fetch as Fetch<'_>>::Item> {
unsafe {
let mut fetch = Q::Fetch::get(self.archetype, self.index)?;
Some(fetch.next())
if fetch.should_skip() {
None
} else {
Some(fetch.next())
}
}
}

Expand Down Expand Up @@ -104,7 +108,11 @@ where
{
unsafe {
let mut fetch = Q::Fetch::get(self.archetype, self.index)?;
Some(fetch.next())
if fetch.should_skip() {
None
} else {
Some(fetch.next())
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions crates/bevy_ecs/hecs/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,35 @@ fn remove_tracking() {
"world clears result in 'removed component' states"
);
}

#[test]
fn added_tracking() {
let mut world = World::new();
let a = world.spawn((123,));

assert_eq!(world.query::<&i32>().iter().count(), 1);
assert_eq!(world.query::<Added<i32>>().iter().count(), 1);
assert_eq!(world.query_mut::<&i32>().iter().count(), 1);
assert_eq!(world.query_mut::<Added<i32>>().iter().count(), 1);
assert!(world.query_one::<&i32>(a).unwrap().get().is_some());
assert!(world.query_one::<Added<i32>>(a).unwrap().get().is_some());
assert!(world.query_one_mut::<&i32>(a).unwrap().get().is_some());
assert!(world
.query_one_mut::<Added<i32>>(a)
.unwrap()
.get()
.is_some());

world.clear_trackers();

assert_eq!(world.query::<&i32>().iter().count(), 1);
assert_eq!(world.query::<Added<i32>>().iter().count(), 0);
assert_eq!(world.query_mut::<&i32>().iter().count(), 1);
assert_eq!(world.query_mut::<Added<i32>>().iter().count(), 0);
assert!(world.query_one_mut::<&i32>(a).unwrap().get().is_some());
assert!(world
.query_one_mut::<Added<i32>>(a)
.unwrap()
.get()
.is_none());
}

0 comments on commit 1bdb9d3

Please sign in to comment.