Macro dynec::comps

source ·
comps!() { /* proc-macro */ }
Expand description

Creates a map of components for a given archetype.

§Syntax

The macro starts with the archetype, followed by =>, then a comma-delimited list of simple and isotope components.

§Simple components

Simple components can be passed in the list directly.

If it is not known whether a component should be added to the list at compile time, start with @?, followed by a value of type Option<Simple>, e.g. @?flag.then_with(|| value).

§Isotope components

For each isotope component, start with a @, followed by a tuple of type (Discrim, Isotope), e.g. @(discrim, value).

Since there can be multiple isotope components for the same entity, an iterator of isotope tuples is also allowed. Start with @?, followed by a value that implements IntoIterator<Item = (Discrim, Isotope)>. HashMap<Discrim, Isotope> and BTreeMap<Discrim, Isotope> satisfy this requirement automatically.

§Example

dynec::archetype!(Foo);
let empty = dynec::comps![Foo =>];
assert_eq!(empty.simple_len(), 0);
assert_eq!(empty.isotope_type_count(), 0);

#[dynec::comp(of = Foo)]
struct Comp1;
#[dynec::comp(of = Foo)]
struct Comp2(i32);
#[dynec::comp(of = Foo)]
struct Comp3 { value: i32 }
#[dynec::comp(of = Foo)]
struct Comp4 { value: i32 }

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, dynec::Discrim)]
struct MyDiscrim(usize);

#[dynec::comp(of = Foo, isotope = MyDiscrim)]
struct Iso(&'static str);

#[dynec::comp(of = Foo, isotope = MyDiscrim)]
struct Carbon { neutrons: i32 };

let mut hashed = std::collections::HashMap::new();
hashed.insert(MyDiscrim(10), Carbon { neutrons: 4 });
hashed.insert(MyDiscrim(11), Carbon { neutrons: 5 });
hashed.insert(MyDiscrim(12), Carbon { neutrons: 6 });

let map = dynec::comps![Foo =>
    Comp1,
    Comp2(2),
    ?Some(Comp3{ value: 3 }),
    ?None::<Comp4>,
    @(MyDiscrim(4), Iso("xxx")),
    @?hashed,
];
assert_eq!(map.simple_len(), 3);
assert_eq!(map.isotope_type_count(), 2);