Attribute Macro dynec::comp

source ·
#[comp]
Expand description

Derives a comp::Simple or comp::Isotope implementation for the applied type. This macro does not modify the input other than stripping attributes.

This macro calls EntityRef implicitly. Fields that reference entities should be annotated with #[entity].

§Options

Options are applied behind the attribute name in the form #[system(...)]. Multiple options are separated by commas.

§of = $ty

Implements the applied type as a component of the archetype $ty. Can be applied multiple times in the same attribute.

§isotope = $ty

Indicates that the applied type is an isotope component with discriminant of type $ty. Indicates that the type is an isotope component (with discriminant type $ty) instead of a simple component.

§required

Indicates that the component must be present for an entity of its archetype any time as long as the entity is created andnot destroyed.

This argument is exclusive with isotope, because isotopes are always unset for an unknown discriminant.

§finalizer

Indicates that the component is a finalizer.

§init

Provides an initializer for the component that gets called when the entity was created without this component. This initializer should be either a closure with explicit parameter types, or a function reference with arity in the form path/arity (e.g. count/1).

For isotope components, the initializer should return an iterator of (C::Discrim, C) tuples, which is similar to the iterator from a HashMap when values of C are indexed by the discriminant.

§storage

Specify the storage type for the component. The argument should be a path that specifies the target type. If the all segments of the path does not have type parameters, it is automatically filled with <Arch::RawEntity, Self>, which is the format automatically compatible with all default storage types.

§Example

use dynec::comp;

dynec::archetype!(Foo; Bar);

#[comp(of = Foo, of = Bar, init = || Qux(1), finalizer)]
struct Qux(i32);

static_assertions::assert_impl_all!(Qux: comp::Simple<Foo>, comp::Simple<Bar>);
assert!(matches!(<Qux as comp::SimpleOrIsotope<Foo>>::PRESENCE, comp::Presence::Optional));
assert!(<Qux as comp::Simple<Bar>>::IS_FINALIZER);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, dynec::Discrim)]
#[dynec(map = discrim::SortedVecMap)]
struct Id(usize);

#[comp(of = Foo, isotope = Id)]
struct Corge(i32);

impl Corge {
    fn make() -> [(Id, Self); 2] { [
        (Id(3), Self(7)),
        (Id(13), Self(17)),
    ] }
}