impl_property

Macro impl_property 

Source
macro_rules! impl_property {
    (
        $property:ident,
        $entity:ident
        $(, compute_derived_fn = $compute_derived_fn:expr)?
        $(, default_const = $default_const:expr)?
        $(, display_impl = $display_impl:expr)?
        $(, canonical_value = $canonical_value:ty)?
        $(, make_canonical = $make_canonical:expr)?
        $(, make_uncanonical = $make_uncanonical:expr)?
        $(, index_id_fn = $index_id_fn:expr)?
        $(, collect_deps_fn = $collect_deps_fn:expr)?
        $(, ctor_registration = $ctor_registration:expr)?
    ) => { ... };
    (@assert_not_both $compute_derived_fn:expr ; $default_const:expr) => { ... };
    (@assert_not_both $compute_derived_fn:expr ; ) => { ... };
    (@assert_not_both ; $default_const:expr) => { ... };
    (@assert_not_both ; ) => { ... };
    (@select_initialization_kind $compute_derived_fn:expr ; $default_const:expr) => { ... };
    (@select_initialization_kind $compute_derived_fn:expr ; ) => { ... };
    (@select_initialization_kind ; $default_const:expr) => { ... };
    (@select_initialization_kind ; ) => { ... };
    (@unwrap_or $value:expr, $_default:expr) => { ... };
    (@unwrap_or, $default:expr) => { ... };
    (@unwrap_or_ty $ty:ty, $_default:ty) => { ... };
    (@unwrap_or_ty, $default:ty) => { ... };
    (
        @__impl_property_common
        $property:ident,           // The name of the type we are implementing `Property` for
        $entity:ident,             // The entity type this property is associated with
        $canonical_value:ty,       // If the type stored in the index is different from Self, the name of that type
        $initialization_kind:expr, // The kind of initialization this property has (implicit selection)
        $compute_derived_fn:expr,  // If the property is derived, the function that computes the value
        $default_const:expr,       // If the property has a constant default initial value, the default value
        $make_canonical:expr,      // A function that takes a value and returns a canonical value
        $make_uncanonical:expr,    // A function that takes a canonical value and returns a value
        $display_impl:expr,        // A function that takes a canonical value and returns a string representation of this property
        $index_id_fn:expr,         // Code that returns the unique index for this property
        $collect_deps_fn:expr,     // If the property is derived, the function that computes the value
        $ctor_registration:expr,   // Code that runs in a ctor for property registration
    ) => { ... };
}
Expand description

Implements the Property trait for the given property type and entity.

Use this macro when you want to implement the Property<E: Entity> trait for a type you have declared yourself. You might want to declare your own property type yourself instead of using the define_property! macro if

  • you want a visibility other than pub
  • you want to derive additional traits
  • your type definition requires attribute proc-macros or other special syntax (for example, deriving Default on an enum requires an attribute on one of the variants)

Example:

In this example, in addition to the set of derives required for all property types, we also derive the Default trait for an enum type, which requires the proc-macro attribute #[default] on one of the variants.

#[derive(Default, Debug, PartialEq, Eq, Clone, Copy, Serialize)]
pub enum InfectionStatus {
    #[default]
    Susceptible,
    Infectious,
    Recovered,
}
// We also specify that this property is assigned a default value for new entities if a value isn't provided.
// Here we have it coincide with `Default::default()`, but this isn't required.
impl_property!(InfectionStatus, Person, default_const = InfectionStatus::Susceptible);

§Parameters

Parameters must be given in the correct order.

  • $property: The identifier for the type implementing Property.
  • $entity: The entity type this property is associated with.
  • Optional parameters (each may be omitted; defaults will be used):
    • compute_derived_fn = <expr> — Function used to compute derived properties. Use define_derived_property! or impl_derived_property! instead of using this option directly.
    • default_const = <expr> — Constant default value if the property has one; implies a non-derived property.
    • display_impl = <expr> — Function converting the property value to a string; defaults to |v| format!("{v:?}").
    • canonical_value = <type> — If the type stored in the index differs from the property’s value type; defaults to Self. If this option is supplied, you will also want to supply make_canonical and make_uncanonical.
    • make_canonical = <expr> — Function converting from Self to CanonicalValue; defaults to std::convert::identity.
    • make_uncanonical = <expr> — Function converting from CanonicalValue to Self; defaults to std::convert::identity.
  • Optional parameters that should generally be left alone, used internally to implement derived properties and multi-properties:
    • index_id_fn = <expr> — Function used to initialize the property index id; defaults to Self::id().
    • collect_deps_fn = <expr> — Function used to collect property dependencies; defaults to an empty implementation.
    • ctor_registration = <expr> — Code run in the ctor for property registration.

§Semantics

  • If compute_derived_fn is provided, the property is derived. In this case, default_const must be absent, and calling Property::default_const() results in a panic. Use define_derived_property! or impl_derived_property! instead of using this option directly.
  • If default_const is provided, the property is a non-derived constant property. In this case, compute_derived_fn must be absent, and calling Property::compute_derived() results in a panic.
  • If neither is provided, the property is non-derived and required/explicit; both Property::default_const() and Property::compute_derived() panic.
  • If both are provided, a compile-time error is emitted.