shape/
hashing.rs

1use std::hash::Hash;
2use std::hash::Hasher;
3
4use super::ShapeCase;
5
6/// Because [`ShapeCase`] uses [`indexmap::IndexMap`] and [`indexmap::IndexSet`]
7/// for its Object, One, and All variants, the default derived Hash trait
8/// implementation does not work. Instead, we hash those cases manually, using a
9/// commutative operation to combine the order-insensitive hashes of the
10/// elements.
11impl Hash for ShapeCase {
12    fn hash<H: Hasher>(&self, state: &mut H) {
13        match self {
14            Self::Bool(value) => ("::Bool", value).hash(state),
15            Self::String(value) => ("::String", value).hash(state),
16            Self::Int(value) => ("::Int", value).hash(state),
17            Self::Float => "::Float".hash(state),
18            Self::Null => "::Null".hash(state),
19            Self::Array { prefix, tail } => ("::Array", prefix, tail).hash(state),
20            Self::Object { fields, rest } => {
21                // Hash each (key, value) pair and then combine the hashes with
22                // a commutative operation like XOR (without sorting the keys).
23                let fields_hash = fields.iter().fold(0, |acc, (key, value)| {
24                    let mut fields_hasher = std::collections::hash_map::DefaultHasher::new();
25                    key.hash(&mut fields_hasher);
26                    value.hash(&mut fields_hasher);
27                    // TODO Is XOR the best operation here?
28                    acc ^ fields_hasher.finish()
29                });
30                ("::Object", fields_hash, rest).hash(state);
31            }
32            Self::One(shapes) => {
33                let xor_hash = shapes.iter().fold(0, |acc, shape| {
34                    let mut shape_hasher = std::collections::hash_map::DefaultHasher::new();
35                    shape.hash(&mut shape_hasher);
36                    acc ^ shape_hasher.finish()
37                });
38                ("::One", xor_hash).hash(state);
39            }
40            Self::All(shapes) => {
41                let xor_hash = shapes.iter().fold(0, |acc, shape| {
42                    let mut shape_hasher = std::collections::hash_map::DefaultHasher::new();
43                    shape.hash(&mut shape_hasher);
44                    acc ^ shape_hasher.finish()
45                });
46                ("::All", xor_hash).hash(state);
47            }
48            // Ignore the WeakScope in order to maintain a stable hash
49            // before/after Namespace finalization.
50            Self::Name(name, _weak) => ("::Name", name).hash(state),
51            Self::Unknown => "::Unknown".hash(state),
52            Self::None => "::None".hash(state),
53        }
54    }
55}