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

Add trait for easy registration with the World #296

Merged
merged 1 commit into from
Nov 7, 2017
Merged
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
14 changes: 14 additions & 0 deletions src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,14 @@ impl World {
}
}
}

/// Adds the given bundle of resources/components.
pub fn add_bundle<B>(&mut self, bundle: B)
where
B: Bundle,
{
bundle.add_to_world(self);
}
}

unsafe impl Send for World {}
Expand Down Expand Up @@ -529,3 +537,9 @@ impl Default for World {
}
}
}

/// Trait used to bundle up resources/components for easy registration with `World`.
pub trait Bundle {
/// Add resources/components to `world`.
fn add_to_world(self, world: &mut World);
}
20 changes: 20 additions & 0 deletions src/world/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,23 @@ fn delete_twice() {
world.delete_entity(e).unwrap();
assert!(world.entities().delete(e).is_err());
}

#[test]
fn test_bundle() {
let mut world = World::new();

pub struct SomeResource {
pub v: u32,
}

pub struct TestBundle;

impl Bundle for TestBundle {
fn add_to_world(self, world: &mut World) {
world.add_resource(SomeResource { v: 12 });
}
}

world.add_bundle(TestBundle);
assert_eq!(12, world.read_resource::<SomeResource>().v);
}