macro_rules! impl_property {
(
$property:ident,
$entity:ident
$(, compute_derived_fn = $compute_derived_fn:expr)?
$(, default_const = $default_const:expr)?
$(, persist = $persist:tt)?
$(, collect_deps_fn = $collect_deps_fn:expr)?
$(, display_impl = $display_impl:expr)?
$(, ctor_registration = $ctor_registration:expr)?
) => { ... };
(
@with_option_display_default
$property:ident,
$entity:ident
$(, compute_derived_fn = $compute_derived_fn:expr)?
$(, default_const = $default_const:expr)?
$(, persist = $persist:tt)?
$(, collect_deps_fn = $collect_deps_fn:expr)?
$(, display_impl = $display_impl:expr)?
$(, ctor_registration = $ctor_registration:expr)?
) => { ... };
(
@multi_property
$property:ident,
$entity:ident,
( $($dependency:ident),+ )
$(, compute_derived_fn = $compute_derived_fn:expr)?
$(, default_const = $default_const:expr)?
$(, collect_deps_fn = $collect_deps_fn:expr)?
$(, display_impl = $display_impl: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 ; ) => { ... };
(@select_persistence $compute_derived_fn:expr ; ) => { ... };
(@select_persistence ; false) => { ... };
(@select_persistence ; ) => { ... };
(@select_persistence ; $persist:tt) => { ... };
(@unwrap_or $value:expr, $_default:expr) => { ... };
(@unwrap_or, $default:expr) => { ... };
(@replace_with_unit $_tt:tt) => { ... };
(@count_tts $($tt:tt),* $(,)?) => { ... };
(
@__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
$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
$query_parts_type:ty,
$value_from_query_parts_fn:expr,
$query_parts_for_value_fn:expr,
$type_id_fn:expr, // Code that returns the logical type ID for this property
$display_impl:expr, // A function that takes a value and returns a string representation of 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
$persistence:expr, // How population persistence treats this property
) => { ... };
}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,
Hash,
Clone,
Copy,
serde::Serialize,
serde::Deserialize,
)]
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 order listed here; each optional one may be omitted.
$property: The identifier for the type implementingProperty.$entity: The entity type this property is associated with.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.persist = false— Skip in population persistence; otherwise saving fails if the type lacks Serde.collect_deps_fn = <expr>— Function used to collect property dependencies; defaults to an empty implementation. Used internally for derived properties and multi-properties.display_impl = <expr>— Function converting the property value to a string; defaults to|v| format!("{v:?}").ctor_registration = <expr>— Code run in thectorfor property registration. Used internally.
§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.
§Persistence
#[derive(Debug, PartialEq, Clone, Copy)]
struct Nickname(&'static str);
impl_property!(Nickname, Person, default_const = Nickname(""), persist = false);Only false is accepted.