Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LayoutFilter for Box<dyn LayoutFilter + Send> #290

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/internals/query/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ pub trait LayoutFilter {
fn matches_layout(&self, components: &[ComponentTypeId]) -> FilterResult;
}

impl LayoutFilter for Box<dyn LayoutFilter + Send> {
fn matches_layout(&self, components: &[ComponentTypeId]) -> FilterResult {
self.as_ref().matches_layout(components)
}
}

/// A filter which selects based upon the data available in the archetype.
pub trait DynamicFilter: Default + Send + Sync {
/// Prepares the filter to run.
Expand Down
45 changes: 45 additions & 0 deletions src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,51 @@
//! .deserialize(json)
//! .unwrap();
//! ```
//!
//! # `Box<dyn LayoutFilter + Send>` as a layout filter
//! [`World::as_serializable()`](super::World::as_serializable) accepts an implementor of the
//! [`LayoutFilter`](super::query::LayoutFilter) trait that is, in particular, also implemented
//! for `Box<dyn LayoutFilter + Send>`. This allows to store filter objects of different
//! types inside [`Box`](std::boxed::Box)es in some collection and process them all together:
//! ```
//! # use legion::*;
//! # use legion::query::LayoutFilter;
//! # use legion::serialize::Canon;
//! # let world = World::default();
//! # #[derive(serde::Serialize, serde::Deserialize)]
//! # struct Name;
//! # #[derive(serde::Serialize, serde::Deserialize)]
//! # struct Position;
//! # #[derive(serde::Serialize, serde::Deserialize)]
//! # struct Velocity;
//! // setup world serializer
//! let mut registry = Registry::<String>::default();
//! registry.register::<Name>("name".to_string());
//! registry.register::<Position>("position".to_string());
//! registry.register::<Velocity>("velocity".to_string());
//! let entity_serializer = Canon::default();
//!
//! // store serialization requests with different entity filters together
//! let serialize_requests: Vec<(&str, Box<dyn LayoutFilter + Send>)> = vec![
//! ("all entities", Box::new(any())),
//! ("having names", Box::new(component::<Name>())),
//! ("without velocity", Box::new(!component::<Velocity>())),
//! ];
//!
//! // process all requests
//! for (title, filter) in serialize_requests {
//! let json =
//! serde_json::to_value(&world.as_serializable(filter, &registry, &entity_serializer))
//! .unwrap();
//! println!("{}: {:#}", title, json);
//! }
//! ```
//!
//! ## Why also `Send`?
//! [`Send`](std::marker::Send) requirement is needed to allow to store filters in
//! [`Resources`](super::Resources) accessed by non thread-local systems. This allows e.g.
//! to implement serialization requests sending from different systems and then processing them
//! all together outside the [`Schedule`](super::Schedule) execution.

#[cfg(feature = "type-uuid")]
pub use crate::internals::serialize::SerializableTypeUuid;
Expand Down