cranelift_isle/
stablemapset.rs

1//! Implementations of hashmap and hashset that asvoid observing non-determinism
2//! in iteration order. In a separate module so the compiler can prevent access to the internal
3//! implementation details.
4
5use std::collections::hash_map::Entry;
6use std::collections::{HashMap, HashSet};
7use std::hash::Hash;
8use std::ops::Index;
9
10/// A wrapper around a [HashSet] which prevents accidentally observing the non-deterministic
11/// iteration order.
12#[derive(Clone, Debug, Default)]
13pub struct StableSet<T>(HashSet<T>);
14
15impl<T> StableSet<T> {
16    pub(crate) fn new() -> Self {
17        StableSet(HashSet::new())
18    }
19}
20
21impl<T: Hash + Eq> StableSet<T> {
22    /// Adds a value to the set. Returns whether the value was newly inserted.
23    pub fn insert(&mut self, val: T) -> bool {
24        self.0.insert(val)
25    }
26
27    /// Returns true if the set contains a value.
28    pub fn contains(&self, val: &T) -> bool {
29        self.0.contains(val)
30    }
31}
32
33/// A wrapper around a [HashMap] which prevents accidentally observing the non-deterministic
34/// iteration order.
35#[derive(Clone, Debug)]
36pub struct StableMap<K, V>(HashMap<K, V>);
37
38impl<K, V> StableMap<K, V> {
39    pub(crate) fn new() -> Self {
40        StableMap(HashMap::new())
41    }
42
43    pub(crate) fn len(&self) -> usize {
44        self.0.len()
45    }
46}
47
48// NOTE: Can't auto-derive this
49impl<K, V> Default for StableMap<K, V> {
50    fn default() -> Self {
51        StableMap(HashMap::new())
52    }
53}
54
55impl<K: Hash + Eq, V> StableMap<K, V> {
56    pub(crate) fn insert(&mut self, k: K, v: V) -> Option<V> {
57        self.0.insert(k, v)
58    }
59
60    pub(crate) fn contains_key(&self, k: &K) -> bool {
61        self.0.contains_key(k)
62    }
63
64    pub(crate) fn get(&self, k: &K) -> Option<&V> {
65        self.0.get(k)
66    }
67
68    pub(crate) fn entry(&mut self, k: K) -> Entry<K, V> {
69        self.0.entry(k)
70    }
71}
72
73impl<K: Hash + Eq, V> Index<&K> for StableMap<K, V> {
74    type Output = V;
75
76    fn index(&self, index: &K) -> &Self::Output {
77        self.0.index(index)
78    }
79}
80
81impl<K, V> From<HashMap<K, V>> for StableMap<K, V> {
82    fn from(map: HashMap<K, V>) -> Self {
83        StableMap(map)
84    }
85}
86
87impl<K: Hash + Eq, V> FromIterator<(K, V)> for StableMap<K, V> {
88    fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
89        StableMap(HashMap::from_iter(iter))
90    }
91}