Skip to main content

aro_core/
entity.rs

1//! Entity marker trait for domain types.
2
3use std::fmt::Debug;
4use std::hash::Hash;
5
6/// Marker trait for domain entities managed by a repository.
7///
8/// Implementors must specify an `Id` type used to uniquely identify each entity
9/// and provide an accessor to retrieve it.
10pub trait Entity {
11    /// The type used to uniquely identify this entity.
12    type Id: Debug + Clone + PartialEq + Eq + Hash + Send + Sync;
13
14    /// Returns the unique identifier of this entity.
15    fn id(&self) -> Self::Id;
16}
17
18#[cfg(test)]
19mod tests {
20    use super::*;
21
22    #[derive(Debug, Clone)]
23    struct User {
24        id: u64,
25        name: String,
26    }
27
28    impl Entity for User {
29        type Id = u64;
30
31        fn id(&self) -> Self::Id {
32            self.id
33        }
34    }
35
36    #[test]
37    fn entity_trait_compiles_with_simple_struct() {
38        let user = User {
39            id: 1,
40            name: "Alice".to_string(),
41        };
42        // Verify the entity can be used with its Id type
43        let id: <User as Entity>::Id = user.id();
44        assert_eq!(id, 1);
45        assert_eq!(user.name, "Alice");
46    }
47}