Skip to content

Commit

Permalink
Call macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceeri committed Jun 18, 2017
1 parent 20915c0 commit 993ba09
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ branches:
script:
- cargo build --all --verbose
- cargo test --all --verbose
- cargo build -all --verbose --features serialize
- cargo build --all --verbose --features serialize
- cargo test --all --verbose --features serialize
- if [ "$TRAVIS_RUST_VERSION" == "nightly" ]; then
cargo bench --all --verbose --no-run;
Expand Down
11 changes: 6 additions & 5 deletions specs/examples/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
use serde::de::DeserializeSeed;
use serde_json::{Serializer, from_str as json_from_str};
use specs::{Component, DispatcherBuilder, Entities, Join, PackedData, System, VecStorage,
World, WriteStorage, WorldSerializer, WorldDeserializer};
World, WorldDeserializer, WorldSerializer, WriteStorage};

#[derive(Debug, Serialize, Deserialize)]
struct CompSerialize {
Expand Down Expand Up @@ -95,10 +95,10 @@ fn main() {
}

struct RemovalSystem;
impl<'a, C> System<'a, C> for RemovalSystem {
impl<'a> System<'a> for RemovalSystem {
type SystemData = RemovalData<'a>;

fn work(&mut self, mut data: RemovalData, _: C) {
fn run(&mut self, mut data: RemovalData) {
// Remove all components
for (entity, _) in (&*data.entities, &data.comp_serial.check()).join() {
data.comp_serial.remove(entity);
Expand Down Expand Up @@ -169,7 +169,8 @@ fn main() {
.add(RemovalSystem, "removal", &[])
.build();

dispatcher.dispatch(&mut world.res, ());
dispatcher.dispatch(&mut world.res);
world.maintain();
}

{
Expand All @@ -180,7 +181,7 @@ fn main() {

{
let entity_list: Vec<_> = {
let entities = world.read_resource::<specs::Entities>();
let entities = world.read_resource::<specs::EntitiesRes>();
entities.join().collect()
};

Expand Down
76 changes: 76 additions & 0 deletions specs/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,79 @@ impl Split for () {
fn next() -> bool { false }
}

#[macro_export]
macro_rules! call {
// Top level calls
( component: $group:ty =>
fn $method:ident
[ $( $before:ty ),* ] in [ $( $after:ty ),* ]
( $( $args:expr ),* )
) => {
call!(
@Iter $group: Locals =>
fn $method
[ $( $before ),* ] in [ $( $after ),* ]
( $( $args ),* )
);
};
( subgroup: $group:ty =>
fn $method:ident
[ $( $before:ty ),* ] in [ $( $after:ty ),* ]
( $( $args:expr ),* )
) => {
call!(
@Iter $group: Subgroups =>
fn $method
[ $( $before ),* ] in [ $( $after ),* ]
( $( $args ),* )
);
};

// Helper methods

// Break macro recursion.
(@Iter [ $( $group:tt )* ] [ ] =>
fn $method:ident
[ $( $before:ty ),* ] in [ $( $after:ty ),* ]
( $( $args:expr ),* )
) => { };

// Iterate over components through associated type recursion.
(@Iter [ $( $group:tt )* ] [ A $( $token:tt )* ] =>
fn $method:ident
[ $( $before:ty ),* ] in [ $( $after:ty ),* ]
( $( $args:expr ),* )
) => {
$method::<$( $before, )* <$( $group )* as Split>::This $( , $after )*>( $( $args ),* );

if <$( $group )* as Split>::next() {
call!(
@Iter [ <$( $group )* as Split>::Next ] [ $( $token )* ] =>
fn $method
[ $( $before ),* ] in [ $( $after ),* ]
( $( $args ),* )
);
}
};

// Entrance to iteration.
(@Iter $group:ty: $at:ident =>
fn $method:ident
[ $( $before:ty ),* ] in [ $( $after:ty ),* ]
( $( $args:expr ),* )
) => {
call!(
@Iter [ <$group as DeconstructedGroup>::$at ] [
// Requires tokens for recursion breaking.
// Each row has 32 tokens, totalling to 128 currently.
A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
] =>
fn $method
[ $( $before ),* ] in [ $( $after ),* ]
( $( $args ),* )
);
};
}
1 change: 1 addition & 0 deletions specs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ pub type Entities<'a> = Fetch<'a, EntitiesRes>;
/// An index is basically the id of an `Entity`.
pub type Index = u32;

#[macro_export]
mod group;
mod join;
mod storage;
Expand Down
82 changes: 3 additions & 79 deletions specs_derive/examples/derive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#![recursion_limit="256"]

#[macro_use]
extern crate specs;
#[macro_use]
extern crate specs_derive;
Expand All @@ -13,81 +14,6 @@ extern crate serde_derive;
#[cfg(feature="serialize")]
extern crate serde_json;

/*
macro_rules! call {
// Top level calls
( local: $group:ty =>
fn $method:ident
[ $( $before:ty ),* ] in [ $( $after:ty ),* ]
( $( $args:expr ),* )
) => {
call!(
@Iter $group: Locals =>
fn $method
[ $( $before ),* ] in [ $( $after ),* ]
( $( $args ),* )
);
};
( subgroups: $group:ty =>
fn $method:ident
[ $( $before:ty ),* ] in [ $( $after:ty ),* ]
( $( $args:expr ),* )
) => {
call!(
@Iter $group: Subgroups =>
fn $method
[ $( $before ),* ] in [ $( $after ),* ]
( $( $args ),* )
);
};
// Helper methods
(@Iter [ $( $group:tt )* ] [ A $( $token:tt )* ] =>
fn $method:ident
[ $( $before:ty ),* ] in [ $( $after:ty ),* ]
( $( $args:expr ),* )
) => {
$method::<$( $before, )* <$( $group )* as Split>::This $( , $after )*>( $( $args ),* );
if <$( $group )* as Split>::next() {
call!(
@Iter [ <$( $group )* as Split>::Next ] [ $( $token )* ] =>
fn $method
[ $( $before ),* ] in [ $( $after ),* ]
( $( $args ),* )
);
}
};
// Break macro recursion
(@Iter [ $( $group:tt )* ] [ ] =>
fn $method:ident
[ $( $before:ty ),* ] in [ $( $after:ty ),* ]
( $( $args:expr ),* )
) => { };
(@Iter $group:ty: $at:ident =>
fn $method:ident
[ $( $before:ty ),* ] in [ $( $after:ty ),* ]
( $( $args:expr ),* )
) => {
call!(
@Iter [ <$group as DeconstructedGroup>::$at ] [
// Requires tokens for recursion breaking.
// Each row has 32 tokens, totalling to 128 currently.
A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
] =>
fn $method
[ $( $before ),* ] in [ $( $after ),* ]
( $( $args ),* )
);
};
}
*/

#[cfg(feature="serialize")]
fn main() {
use specs::{Component, ComponentGroup, DeconstructedGroup, DispatcherBuilder, Entities, EntitiesRes, Join, SerializeGroup, System, ReadStorage, Split, VecStorage, WriteStorage, World, WorldDeserializer, WorldSerializer};
Expand Down Expand Up @@ -253,15 +179,13 @@ fn main() {
3
}

/*
let s = "Something";
call!(local: SomeGroup =>
call!(component: SomeGroup =>
fn call_method [ ] in [ ] (s)
);
call!(local: SomeGroup =>
call!(component: SomeGroup =>
fn call_other [ ] in [ ] (s)
);
*/
}

#[cfg(not(feature="serialize"))]
Expand Down

0 comments on commit 993ba09

Please sign in to comment.