serde_json::value::RawValue - hashing problem

Hello.
How to enable hashing &RawValue?

My code:

The problem is with reference hashing. But it works out of the box:

use std::collections::HashMap;

let mut map: HashMap<&str, i32> = HashMap::new();
map.insert("test", 231);
println!("{:?}", map);

Please, help.

The serde_json crate does not specify how RawValue is hashed.

How to implement it? I would like to clone the content with the reference.

You can implement the Hash trait manually on Variable by following the example on Hash. One challenge is that two different json strings with different whitespaces would have different hashes despite representing the same json.

impl<'a> Hash for Variable<'a> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
        self.custom.get().hash(state);
    }
}

Ok. I solved it with your help.

Solution:

impl<'a> Hash for Variable<'a> {
        fn hash<H: Hasher>(&self, state: &mut H) {
            self.id.hash(state);
            self.custom.get().hash(state);
        }
}

impl<'a> PartialEq for Variable<'a> {
        fn eq(&self, other: &Self) -> bool {
            self.id == other.id &&  self.custom.get() == other.custom.get()
        }
}

impl<'a> Eq for Variable<'a> {}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.