Skip to main content

entity_id

Attribute Macro entity_id 

Source
#[entity_id]
Expand description

Turn a single-field tuple struct into a fully-featured entity-ID newtype.

Applied to struct Foo(Inner); (exactly one unnamed field), this macro generates the standard derive set plus the conversions and helpers users expect from a strongly-typed identifier:

  • Default derives: Default, Clone, Copy, serde::Serialize, serde::Deserialize, PartialEq, Eq, Hash, and (by default) Debug. User-supplied derives are merged in.
  • pub fn new(value: Inner) -> Self
  • impl FromStr (forwarding to the inner type’s FromStr).
  • impl Display (forwarding to the inner type’s Display).
  • impl AsRef<Inner> and impl AsMut<Inner>.
  • impl From<Inner>/impl From<&Inner> and the symmetric impl From<Self>/impl From<&Self> for Inner conversions.

§Attribute arguments

#[entity_id(debug = <bool>)]

  • debug — disable the auto-derived Debug (defaults to true) so callers can hand-write a redacting or otherwise customised impl.

§Important

Because the macro derives Copy, the inner type must implement Copy (e.g. Uuid, u64, i64). Wrappers around String are not supported through this macro.

§Examples

use eventide_macros::entity_id;
use uuid::Uuid;

#[entity_id]
struct OrderId(Uuid);

let id = OrderId::new(Uuid::new_v4());
let copy = id; // `Copy` works.
let _ = format!("{} / {:?}", id, id); // `Display` and `Debug` both work.

Custom Debug impl:

use eventide_macros::entity_id;
use uuid::Uuid;

#[entity_id(debug = false)]
struct ProfileId(Uuid);

impl std::fmt::Debug for ProfileId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ProfileId(<redacted>)")
    }
}