-
Notifications
You must be signed in to change notification settings - Fork 101
Adds an example of using the new columnar API to clone world. #138
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
Open
AngelOfSol
wants to merge
1
commit into
Ralith:master
Choose a base branch
from
AngelOfSol:clone-example
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,142 @@ | ||||||
| use core::any::TypeId; | ||||||
| use std::ops::{Deref, DerefMut}; | ||||||
|
|
||||||
| use hecs::{Archetype, ColumnBatchBuilder, ColumnBatchType, Component, World}; | ||||||
|
|
||||||
| fn main() { | ||||||
| let mut world = CloneableWorld { | ||||||
| inner: World::new(), | ||||||
| clone_registry: CloneRegistry::default() | ||||||
| .register::<String>() | ||||||
| .register::<u32>(), | ||||||
| }; | ||||||
|
|
||||||
| world.spawn((4u32,)); | ||||||
| world.spawn((8u32, "test".to_string())); | ||||||
| world.spawn(("test".to_string(),)); | ||||||
|
|
||||||
| let cloned = world.clone(); | ||||||
|
|
||||||
| assert_eq!( | ||||||
| world.query::<&u32>().iter().count(), | ||||||
| cloned.query::<&u32>().iter().count() | ||||||
| ); | ||||||
| assert_eq!( | ||||||
| world.query::<&String>().iter().count(), | ||||||
| cloned.query::<&String>().iter().count() | ||||||
| ); | ||||||
|
|
||||||
| for (left, right) in world | ||||||
| .query::<&u32>() | ||||||
| .iter() | ||||||
| .zip(cloned.query::<&u32>().iter()) | ||||||
| { | ||||||
| assert_eq!(left, right); | ||||||
| } | ||||||
|
|
||||||
| for (left, right) in world | ||||||
| .query::<&String>() | ||||||
| .iter() | ||||||
| .zip(cloned.query::<&String>().iter()) | ||||||
| { | ||||||
| assert_eq!(left, right); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// An opaque registry that holds data that helps a World clone itself. | ||||||
| #[derive(Clone, Default)] | ||||||
| struct CloneRegistry(Vec<CloneEntry>); | ||||||
|
|
||||||
| impl CloneRegistry { | ||||||
| /// Registers `T` with the registry, enabling `T` to be cloned in any | ||||||
| /// archetypes that contain it. | ||||||
| pub fn register<T: Clone + Component>(mut self) -> Self { | ||||||
| if !self.0.iter().any(|item| item.type_id == TypeId::of::<T>()) { | ||||||
| self.0.push(register::<T>()); | ||||||
| } | ||||||
| self | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[derive(Clone)] | ||||||
| struct CloneEntry { | ||||||
| type_id: TypeId, | ||||||
| add_type: fn(&mut ColumnBatchType) -> (), | ||||||
| add_values: fn(&mut ColumnBatchBuilder, &Archetype) -> (), | ||||||
| } | ||||||
| fn register<T: Component + Clone>() -> CloneEntry { | ||||||
| CloneEntry { | ||||||
| type_id: TypeId::of::<T>(), | ||||||
| add_type: |batch_type| { | ||||||
| batch_type.add::<T>(); | ||||||
| }, | ||||||
| add_values: |batch, arch| { | ||||||
| let mut writer = batch.writer::<T>().unwrap(); | ||||||
| for item in arch.get::<T>().unwrap().iter() { | ||||||
| if writer.push(item.clone()).is_err() { | ||||||
| panic!() | ||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| } | ||||||
| }, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| pub struct CloneableWorld { | ||||||
| inner: World, | ||||||
| clone_registry: CloneRegistry, | ||||||
| } | ||||||
|
|
||||||
| impl Deref for CloneableWorld { | ||||||
| type Target = World; | ||||||
| fn deref(&self) -> &Self::Target { | ||||||
| &self.inner | ||||||
| } | ||||||
| } | ||||||
| impl DerefMut for CloneableWorld { | ||||||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||||||
| &mut self.inner | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| impl Clone for CloneableWorld { | ||||||
| fn clone(&self) -> Self { | ||||||
| let mut new_world = Self { | ||||||
| inner: World::new(), | ||||||
| clone_registry: self.clone_registry.clone(), | ||||||
| }; | ||||||
|
|
||||||
| for archetype in self.archetypes() { | ||||||
| debug_assert!(archetype.component_types().all(|item| self | ||||||
| .clone_registry | ||||||
| .0 | ||||||
| .iter() | ||||||
| .any(|register| register.type_id == item))); | ||||||
| let mut types = ColumnBatchType::new(); | ||||||
| for entry in self | ||||||
| .clone_registry | ||||||
| .0 | ||||||
| .iter() | ||||||
| .filter(|item| archetype.has_dynamic(item.type_id)) | ||||||
| { | ||||||
| (entry.add_type)(&mut types); | ||||||
| } | ||||||
| let mut batch = types.into_batch(archetype.len()); | ||||||
| for entry in self | ||||||
| .clone_registry | ||||||
| .0 | ||||||
| .iter() | ||||||
| .filter(|item| archetype.has_dynamic(item.type_id)) | ||||||
| { | ||||||
| (entry.add_values)(&mut batch, archetype); | ||||||
| } | ||||||
| let entities: Box<[_]> = archetype | ||||||
| .ids() | ||||||
| .iter() | ||||||
| .map(|id| unsafe { self.find_entity_from_id(*id) }) | ||||||
| .collect(); | ||||||
| new_world.spawn_column_batch_at(&entities, batch.build().unwrap()); | ||||||
| } | ||||||
|
|
||||||
| new_world | ||||||
| } | ||||||
| } | ||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should include module-level doc comments that explain what this demonstrates, why it's useful, why special effort is needed (there's no static guarantee that all components are
Clone) and what the caveats are (unregistered components aren't cloned).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, I'll get on (I've not written an example before)