Fix Added behaviour for QueryOne get. (#543)

Query unchanged as impacts performances.
Added tests in bevy_ecs/hecs
This commit is contained in:
Boutillier 2020-10-05 19:38:12 +02:00 committed by GitHub
parent 9a4167ef7f
commit 1bdb9d3b00
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 2 deletions

View file

@ -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())
}
}
}
@ -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())
}
}
}

View file

@ -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());
}