在 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));
}
但是,编译器要我在Piece
上定义Hash
:
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
与另一个相等。但是,我希望能够在我的 positions
HashMap 中区分不同的 Rook
实例。
我该怎么做?我不想在 Piece
上定义 Hash
,但肯定已经在指针上定义了散列?
最佳答案
原始指针(*const T
、*mut T
)和引用(&T
, &mut T
) 在 Rust 中。你有一个引用。
哈希
is defined对于委托(delegate)给所引用项目的哈希的引用:
impl<T: ?Sized + Hash> Hash for &T {
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state);
}
}
然而,它是defined for raw pointers如你所愿:
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);
}
}
}
这行得通:
let mut positions = HashMap::new();
positions.insert(&left_rook as *const Piece, (0, 0));
positions.insert(&right_rook as *const Piece, (0, 7));
但是,在这里使用引用或原始指针充其量是不确定的。
如果您使用引用,一旦您移动了您插入的值,编译器将阻止您使用 hashmap,因为引用将不再有效。
如果您使用原始指针,编译器不会阻止您,但您将拥有可能导致内存不安全的悬垂指针。
在您的情况下,我想我会尝试重构代码,以便一段在内存地址之外是唯一的。也许只是一些递增的数字:
positions.insert((left_rook, 0), (0, 0));
positions.insert((right_rook, 1), (0, 7));
如果这看起来不可能,您总是可以Box
给它一个稳定的内存地址。后一种解决方案更类似于 Java 等语言,默认情况下所有内容都是堆分配的。
I'd rather wrap a
&'a T
in another struct that has the same identity semantics as*const T
than have the lifetime erased
您可以创建一个结构来处理引用相等性:
#[derive(Debug)]
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
}
}
impl<'a, T> Eq for RefEquality<'a, T> {}
然后使用它:
positions.insert(RefEquality(&left_rook), (0, 0));
positions.insert(RefEquality(&right_rook), (0, 7));
关于rust - 如何使指针可散列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33847537/