use discriminant_hash_derive::DiscriminantHash; use std::{ collections::hash_map::DefaultHasher, hash::{Hash, Hasher}, }; #[derive(DiscriminantHash)] enum Abc<'a, T> { Simple, Lifetime(&'a str), HashNotImpl(Xyz), Generic(T), } #[allow(unused)] #[derive(Hash)] enum Pqr<'a> { Simple, Lifetime(&'a str), } // Xyz doesn't impl Hash struct Xyz; fn main() { assert_eq!(my_hash(Abc::Simple::), my_hash(Abc::Simple::)); assert_eq!( my_hash(Abc::Lifetime::("hello")), my_hash(Abc::Lifetime::("world")) ); assert_eq!( my_hash(Abc::HashNotImpl::(Xyz)), my_hash(Abc::HashNotImpl::(Xyz)) ); assert_eq!( my_hash(Abc::Generic::(4)), my_hash(Abc::Generic::(Xyz)) ); assert_ne!( my_hash(Abc::Simple::), my_hash(Abc::Lifetime::("abc")) ); /* This may be same depending on how Pqr is defined assert_eq!( my_hash(Abc::Simple::), my_hash(Pqr::Simple) ); */ } fn my_hash(obj: T) -> u64 where T: Hash, { let mut hasher = DefaultHasher::new(); obj.hash(&mut hasher); hasher.finish() }