Skip to main content

define_property

Macro define_property 

Source
macro_rules! define_property {
    (
        struct $name:ident ( $visibility:vis Option<$inner_ty:ty> ),
        $entity:ident,
        impl_eq_hash = $impl_eq_hash:ident
        $(, $($extra:tt)*)?
    ) => { ... };
    (
        struct $name:ident ( $visibility:vis Option<$inner_ty:ty> ),
        $entity:ident
        $(, $($extra:tt)*)?
    ) => { ... };
    (
        struct $name:ident ( $($visibility:vis $field_ty:ty),* $(,)? ),
        $entity:ident,
        impl_eq_hash = $impl_eq_hash:ident
        $(, $($extra:tt)*)?
    ) => { ... };
    (
        struct $name:ident ( $($visibility:vis $field_ty:ty),* $(,)? ),
        $entity:ident
        $(, $($extra:tt)*)?
    ) => { ... };
    (
        struct $name:ident { $($visibility:vis $field_name:ident : $field_ty:ty),* $(,)? },
        $entity:ident,
        impl_eq_hash = $impl_eq_hash:ident
        $(, $($extra:tt)*)?
    ) => { ... };
    (
        struct $name:ident { $($visibility:vis $field_name:ident : $field_ty:ty),* $(,)? },
        $entity:ident
        $(, $($extra:tt)*)?
    ) => { ... };
    (
        enum $name:ident {
            $($variant:ident),* $(,)?
        },
        $entity:ident,
        impl_eq_hash = $impl_eq_hash:ident
        $(, $($extra:tt)*)?
    ) => { ... };
    (
        enum $name:ident {
            $($variant:ident),* $(,)?
        },
        $entity:ident
        $(, $($extra:tt)*)?
    ) => { ... };
    (@apply_property_decoration , $item:item, $name:ident) => { ... };
    (@apply_property_decoration Eq, $item:item, $name:ident) => { ... };
    (@apply_property_decoration Hash, $item:item, $name:ident) => { ... };
    (@apply_property_decoration both, $item:item, $name:ident) => { ... };
    (@apply_property_decoration neither, $item:item, $name:ident) => { ... };
    (@apply_property_decoration $mode:ident, $item:item, $name:ident) => { ... };
}
Expand description

Defines a struct or enum with a standard set of derives and automatically invokes impl_property! for it. This macro provides a concise shorthand for defining simple property types that follow the same derive and implementation pattern.

The macro supports the following forms:

§1. Tuple Structs

define_property!(struct Age(u8), Person);

Expands to:

#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub struct Age(u8);
impl_property!(Age, Person);

You can define multiple tuple fields:

define_property!(struct Location(City, State), Person);

§2. Named-field Structs

define_property!(struct Coordinates { x: i32, y: i32 }, Person);

Expands to:

#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub struct Coordinates { x: i32, y: i32 }
impl_property!(Coordinates, Person);

§3. Enums

define_property!(
    enum InfectionStatus {
        Susceptible,
        Infectious,
        Recovered,
    },
    Person
);

Expands to:

#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub enum InfectionStatus {
    Susceptible,
    Infectious,
    Recovered,
}
impl_property!(InfectionStatus, Person);

§Notes

  • By default, the generated type derives Debug, PartialEq, Eq, Hash, Clone, and Copy.
  • Use the optional default_const = <default_value> argument to define a compile-time constant default for the property.
  • Use impl_eq_hash = Eq, Hash, both, or neither as the first optional argument to suppress the default Eq/Hash derives and switch to generated or user-supplied implementations.
  • Remaining optional arguments follow the same ordering as impl_property!.
  • If you need a more complex type definition (e.g., generics, attributes, or non-Copy fields), define the type manually and then call impl_property! directly.