作为我的第一个Rust项目,我正在开发一个简单的图形库。我希望我的图根能够引用所有通过引用计数进行内存管理的节点,但以下内容不能编译:
pub struct Graph {
nodes: HashSet<Rc<Cell<Node>>>
}
impl Hash for Cell<Node> {
fn hash<H: Hasher>(&self, state: &mut H) {
let pointer = &self as *const isize;
pointer.hash(state);
}
}
pub struct Relationship {
source: Rc<Cell<Node>>,
target: Rc<Cell<Node>>,
}
pub struct Node {
labels: Vec<Rc<String>>,
attributes: Vec<Rc<String>>,
relationships: Vec<Rc<Relationship>>
}
出现以下错误:
error[E0117]: only traits defined in the current crate can be implemented
for arbitrary types
--> src\graph/mod.rs:12:1
|
12 | impl Hash for Cell<Node> {
| _^ starting here...
13 | | fn hash<H: Hasher>(&self, state: &mut H) {
14 | |
15 | | }
16 | | }
| |_^ ...ending here: impl doesn't use types inside crate
|
= note: the impl does not reference any types defined in this crate
= note: define and implement a trait or new type instead