serde_json::value::RawValue - hashing problem

Hello.
How to enable hashing &RawValue?

My code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7c4b7b42e3b79dfee3663b3cf5e9b198

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:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=82d9c879cb0d209f852d4a2581936e1c

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.