Derive Macro dynec::EntityRef

source ·
#[derive(EntityRef)]
{
    // Attributes available to this derive:
    #[entity]
    #[not_entity]
}
Expand description

Derives a Referrer implementation for the type.

The generated implementation does not visit any fields by default. Add the #[entity] attribute to fields that implement crate::entity::Referrer, then the generated implementation will delegate to these fields.

This derive macro is automatically called in comp and global. It should only be called explicitly if the type is not a component or global, e.g. if it is a type included in a component field.

§Example

dynec::archetype!(Foo);

#[derive(dynec::EntityRef)]
struct Bar {
    #[entity]
    entity: dynec::Entity<Foo>,
}

A compile error would be triggered if a field is an entity reference but is not #[entity]:

dynec::archetype!(Foo);

#[derive(dynec::EntityRef)]
struct Bar {
    entity: dynec::Entity<Foo>,
}

The above code will fail to compile with an error that contains this_field_references_an_entity_so_it_should_have_the_entity_attribute.

In the case where a field references a type parameter, dynec cannot check whether it correctly does not implement Referrer. In that case, apply the #[not_entity] attribute to assert its safety:

#[derive(dynec::EntityRef)]
struct Bar<T: 'static> {
    #[not_entity]
    value: T,
}

It is the user’s responsibility not to set T as a Referrer implementation.

Note that this compile error is best-effort and not comprehensive — if the actual entity reference is hidden behind a complex type that does not implement Referrer, e.g. as an element in a tuple, this error will not happen, which would lead to a runtime panic instead during ref counting.