在Rust中,我希望将枚举视为相等,但仍然能够通过指针区分不同的实例。这是一个玩具示例:
use self::Piece::*;
use std::collections::HashMap;
#[derive(Eq, PartialEq)]
enum Piece {
Rook,
Knight,
}
fn main() {
let mut positions: HashMap<&Piece, (u8, u8)> = HashMap::new();
let left_rook = Rook;
let right_rook = Rook;
positions.insert(&left_rook, (0, 0));
positions.insert(&right_rook, (0, 7));
}
但是,编译器要我在Hash
上定义Piece
:
error[E0277]: the trait bound `Piece: std::hash::Hash` is not satisfied
--> src/main.rs:11:52
|
11 | let mut positions: HashMap<&Piece, (u8, u8)> = HashMap::new();
| ^^^^^^^^^^^^ the trait `std::hash::Hash` is not implemented for `Piece`
|
= note: required because of the requirements on the impl of `std::hash::Hash` for `&Piece`
= note: required by `<std::collections::HashMap<K, V>>::new`
error[E0599]: no method named `insert` found for type `std::collections::HashMap<&Piece, (u8, u8)>` in the current scope
--> src/main.rs:15:15
|
15 | positions.insert(&left_rook, (0, 0));
| ^^^^^^
|
= note: the method `insert` exists but the following trait bounds were not satisfied:
`&Piece : std::hash::Hash`
error[E0599]: no method named `insert` found for type `std::collections::HashMap<&Piece, (u8, u8)>` in the current scope
--> src/main.rs:16:15
|
16 | positions.insert(&right_rook, (0, 7));
| ^^^^^^
|
= note: the method `insert` exists but the following trait bounds were not satisfied:
`&Piece : std::hash::Hash`
我希望在我的枚举上定义相等,以便一个Rook
等于另一个Rook
。但是,我希望能够区分positions
哈希映射中的不同Hash
个实例。
我该怎么做?我不想在Piece
上定义{{1}},但肯定已经在指针上定义了哈希?
答案 0 :(得分:7)
原始指针(*const T
,*mut T
)和引用(&T
,{{之间存在差异在Rust中1}})你有一个参考。
&mut T
is defined作为委托给引用项的哈希的引用:
Hash
但是,你想要的是defined for raw pointers:
impl<T: ?Sized + Hash> Hash for &T {
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state);
}
}
这有效:
impl<T: ?Sized> Hash for *const T {
fn hash<H: Hasher>(&self, state: &mut H) {
if mem::size_of::<Self>() == mem::size_of::<usize>() {
// Thin pointer
state.write_usize(*self as *const () as usize);
} else {
// Fat pointer
let (a, b) = unsafe {
*(self as *const Self as *const (usize, usize))
};
state.write_usize(a);
state.write_usize(b);
}
}
}
但是,在这里使用引用或原始指针充其量只是最好的。
如果使用引用,一旦移动了已插入的值,编译器将阻止您使用hashmap,因为引用将不再有效。
如果您使用原始指针,编译器不会阻止您,但是您将有悬空指针,这可能导致内存不安全。
在您的情况下,我认为我会尝试重新构建代码,以便在内存地址之外的某个部分是唯一的。也许只是一些递增的数字:
let mut positions = HashMap::new();
positions.insert(&left_rook as *const Piece, (0, 0));
positions.insert(&right_rook as *const Piece, (0, 7));
如果这似乎不可能,你可以随时positions.insert((left_rook, 0), (0, 0));
positions.insert((right_rook, 1), (0, 7));
给它一个稳定的内存地址。后一种解决方案更类似于Java这样的语言,默认情况下所有内容都是堆分配的。
我宁愿将
Box
包装在另一个与&'a T
具有相同身份语义的结构中,而不是将其删除。
您可以创建一个处理引用相等性的结构:
*const T
然后使用:
#[derive(Debug, Eq)]
struct RefEquality<'a, T>(&'a T);
impl<'a, T> std::hash::Hash for RefEquality<'a, T> {
fn hash<H>(&self, state: &mut H)
where
H: std::hash::Hasher,
{
(self.0 as *const T).hash(state)
}
}
impl<'a, 'b, T> PartialEq<RefEquality<'b, T>> for RefEquality<'a, T> {
fn eq(&self, other: &RefEquality<'b, T>) -> bool {
self.0 as *const T == other.0 as *const T
}
}