Skip to main content

Entity

Trait Entity 

Source
pub trait Entity: Send + Sync {
    type Id: EntityId;

    // Required method
    fn id(&self) -> &Self::Id;

    // Provided method
    fn is_same_as(&self, other: &Self) -> bool { ... }
}
Expand description

A domain object whose equality is defined by its identity, not its attributes - the same conceptual entity can change all of its other fields over time and still be “the same” entity.

This trait deliberately does not require PartialEq/Eq: nothing stops an implementor from also deriving PartialEq, but a derived PartialEq compares every field structurally, which is almost never the identity comparison a DDD Entity is supposed to have. Use Entity::is_same_as wherever “is this the same entity” is the question being asked; if you do implement PartialEq/Eq for an Entity, implement it in terms of is_same_as rather than deriving it, so == and is_same_as cannot silently disagree.

Required Associated Types§

Source

type Id: EntityId

This entity’s identity type.

Required Methods§

Source

fn id(&self) -> &Self::Id

This entity’s identity.

Provided Methods§

Source

fn is_same_as(&self, other: &Self) -> bool

Identity comparison: true iff self and other are the same entity, regardless of whether their other fields currently match.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§