If we want to define two different classes, user and product, and we want to make sure that we cannot compare their ids directly, we might try #[derive(Debug, Default, PartialEq, Eq)] pub struct Identifier<T> { inner: u64, } #[derive(Debug, Default, PartialEq, Eq)] pub struct User { id: Identifier<Self>, } #[derive(Debug, Default, PartialEq, Eq)] pub struct Product { id: Identifier<Self>, } #[cfg(test)] mod tests { use super::*; #[test] fn id_should_not_be_the_same() { let user = User::default(); let product = Product::default(); assert_ne!(user.id, product.id); assert_eq!(user.id.inner, product.id.inner); } }
Rust generics
Rust generics
Rust generics
If we want to define two different classes, user and product, and we want to make sure that we cannot compare their ids directly, we might try #[derive(Debug, Default, PartialEq, Eq)] pub struct Identifier<T> { inner: u64, } #[derive(Debug, Default, PartialEq, Eq)] pub struct User { id: Identifier<Self>, } #[derive(Debug, Default, PartialEq, Eq)] pub struct Product { id: Identifier<Self>, } #[cfg(test)] mod tests { use super::*; #[test] fn id_should_not_be_the_same() { let user = User::default(); let product = Product::default(); assert_ne!(user.id, product.id); assert_eq!(user.id.inner, product.id.inner); } }