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
Defaulton 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 implementingProperty.$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. Usedefine_derived_property!orimpl_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 toSelf. If this option is supplied, you will also want to supplymake_canonicalandmake_uncanonical.make_canonical = <expr>— Function converting fromSelftoCanonicalValue; defaults tostd::convert::identity.make_uncanonical = <expr>— Function converting fromCanonicalValuetoSelf; defaults tostd::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 toSelf::id().collect_deps_fn = <expr>— Function used to collect property dependencies; defaults to an empty implementation.ctor_registration = <expr>— Code run in thectorfor property registration.
§Semantics
- If
compute_derived_fnis provided, the property is derived. In this case,default_constmust be absent, and callingProperty::default_const()results in a panic. Usedefine_derived_property!orimpl_derived_property!instead of using this option directly. - If
default_constis provided, the property is a non-derived constant property. In this case,compute_derived_fnmust be absent, and callingProperty::compute_derived()results in a panic. - If neither is provided, the property is non-derived and required/explicit; both
Property::default_const()andProperty::compute_derived()panic. - If both are provided, a compile-time error is emitted.