如何为单元格<mytype>实现Hash以将其放入集合中?

时间:2017-05-18 14:18:26

标签: rust hashset

作为我的第一个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

0 个答案:

没有答案