Skip to content

Commit

Permalink
Introduce bundle_satisfies_query (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanbox-irl authored Dec 4, 2023
1 parent b55697d commit 428885e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ use core::{fmt, mem};
use crate::archetype::TypeInfo;
use crate::Component;

/// Checks if a query is satisfied by a bundle. This is primarily useful for unit tests.
pub fn bundle_satisfies_query<B: Bundle, Q: crate::Query>() -> bool {
use crate::Fetch;

let arch = B::with_static_type_info(|info| crate::Archetype::new(info.into()));
Q::Fetch::access(&arch).is_some()
}

/// Checks if a query is satisfied by a dynamic bundle. For static bundles, see [bundle_satisfies_query].
/// This is primarily useful for unit tests.
pub fn dynamic_bundle_satisfies_query<B: DynamicBundle, Q: crate::Query>(b: &B) -> bool {
use crate::Fetch;

let arch = crate::Archetype::new(b.type_info());
Q::Fetch::access(&arch).is_some()
}

/// A dynamically typed collection of components
///
/// Bundles composed of exactly the same types are semantically equivalent, regardless of order. The
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ mod world;

pub use archetype::{Archetype, ArchetypeColumn, ArchetypeColumnMut};
pub use batch::{BatchIncomplete, BatchWriter, ColumnBatch, ColumnBatchBuilder, ColumnBatchType};
pub use bundle::{Bundle, DynamicBundle, DynamicBundleClone, MissingComponent};
pub use bundle::{
bundle_satisfies_query, dynamic_bundle_satisfies_query, Bundle, DynamicBundle,
DynamicBundleClone, MissingComponent,
};
pub use command_buffer::CommandBuffer;
pub use entities::{Entity, NoSuchEntity};
pub use entity_builder::{BuiltEntity, BuiltEntityClone, EntityBuilder, EntityBuilderClone};
Expand Down

0 comments on commit 428885e

Please sign in to comment.