Skip to content

Commit

Permalink
feat(world): Adding trait for registering components/resources in the…
Browse files Browse the repository at this point in the history
… World
  • Loading branch information
Rhuagh committed Nov 2, 2017
1 parent 474f769 commit 1ad81a6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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);
}

0 comments on commit 1ad81a6

Please sign in to comment.