1use std::fmt::Debug;
4use std::hash::Hash;
5
6pub trait Entity {
11 type Id: Debug + Clone + PartialEq + Eq + Hash + Send + Sync;
13
14 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 let id: <User as Entity>::Id = user.id();
44 assert_eq!(id, 1);
45 assert_eq!(user.name, "Alice");
46 }
47}