From 1ad81a6fe33b23bd3cf4cc93fdbad2819837e3f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20R=C3=B6nnberg?= Date: Thu, 2 Nov 2017 21:48:46 +0100 Subject: [PATCH] feat(world): Adding trait for registering components/resources in the World --- src/world/mod.rs | 14 ++++++++++++++ src/world/tests.rs | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/world/mod.rs b/src/world/mod.rs index 3ee6e1e92..5890992e3 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -501,6 +501,14 @@ impl World { } } } + + /// Adds the given bundle of resources/components. + pub fn add_bundle(&mut self, bundle: B) + where + B: Bundle, + { + bundle.add_to_world(self); + } } unsafe impl Send for World {} @@ -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); +} diff --git a/src/world/tests.rs b/src/world/tests.rs index b2b21972b..34e16d705 100644 --- a/src/world/tests.rs +++ b/src/world/tests.rs @@ -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::().v); +}