Skip to content

Commit

Permalink
Add QueryMap::contains to check for presence on an entity with fewer …
Browse files Browse the repository at this point in the history
…constraints.
  • Loading branch information
adamreichold committed Nov 11, 2023
1 parent 67e2e04 commit 9686da3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "rs-ecs"
description = "reasonably simple entity component system"
version = "0.7.5"
version = "0.7.6"
edition = "2018"
rust-version = "1.51"
authors = ["Adam Reichold <adam.reichold@t-online.de>"]
Expand Down
33 changes: 33 additions & 0 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,39 @@ impl<S> QueryMap<'_, S>
where
S: QuerySpec,
{
/// Check if an [Entity] is present in the map.
///
/// In contrast to [`get`][Self::get], this does not require a `S::Fetch: FetchShared` bound.
/// In contrast to [`get_mut`][Self::get_mut] however, a shared borrow is sufficient.
///
/// # Examples
///
/// ```
/// # use rs_ecs::*;
/// let mut world = World::new();
///
/// let entity1 = world.alloc();
/// world.insert(entity1, (42_i32, 1.0_f32));
///
/// let entity2 = world.alloc();
/// world.insert(entity2, (2.0_f32,));
///
/// let mut query = Query::<(&i32, Option<&f32>)>::new();
/// let mut query = query.borrow(&world);
/// let query = query.map();
///
/// assert!(query.contains(entity1));
/// assert!(!query.contains(entity2));
/// ```
pub fn contains(&self, ent: Entity) -> bool {
let meta = self.entities[ent.id as usize];
assert_eq!(ent.gen, meta.gen, "Entity is stale");

let ptr = unsafe { self.ptrs.get_unchecked(meta.ty as usize) };

ptr.is_some()
}

/// Access the queried components of the given [Entity]
///
/// Available only if the components do not include unique references.
Expand Down

0 comments on commit 9686da3

Please sign in to comment.