use std::fmt::Debug;
use std::hash::Hash;
pub trait Entity {
type Id: Debug + Clone + PartialEq + Eq + Hash + Send + Sync;
fn id(&self) -> Self::Id;
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, Clone)]
struct User {
id: u64,
name: String,
}
impl Entity for User {
type Id = u64;
fn id(&self) -> Self::Id {
self.id
}
}
#[test]
fn entity_trait_compiles_with_simple_struct() {
let user = User {
id: 1,
name: "Alice".to_string(),
};
let id: <User as Entity>::Id = user.id();
assert_eq!(id, 1);
assert_eq!(user.name, "Alice");
}
}