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.
alice
April 21, 2020, 11:06am
2
The serde_json
crate does not specify how RawValue
is hashed.
alice:
RawValue
How to implement it? I would like to clone the content with the reference.
alice
April 21, 2020, 11:12am
4
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> {}
system
Closed
July 20, 2020, 11:29am
7
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.