I want to get an ability to create hashset of an enum
. As I understand, all I need to do, is to implement PartialEq
, Eq
and Hash
traits for my enums. But the problem is that the enum is declared in some other crate. And because of that implemenation is impossilbe. So, I decided to create a struct, which contains a key from the enum, and make it hashable instead.
use rdev::{Key}; // needed enum
pub struct MyKey(Key); // my implementation which supposed to be hashable
All I need is to implement the traits, but there's a problem
impl Hash for MyKey {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_u16(self.0 as u16);
// ^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
}
}
impl PartialEq for MyKey {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Eq for MyKey {}
I can't get the point of this warning. If I declare some other enum, as u16
will work just fine. But with outer enum it does not.
Could you, please, explain why? Or maybe you can give me more beautiful way to get an ability for creating HashSet of enum's keys.