cranelift_isle/
stablemapset.rs1use std::collections::hash_map::Entry;
6use std::collections::{HashMap, HashSet};
7use std::hash::Hash;
8use std::ops::Index;
9
10#[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 pub fn insert(&mut self, val: T) -> bool {
24 self.0.insert(val)
25 }
26
27 pub fn contains(&self, val: &T) -> bool {
29 self.0.contains(val)
30 }
31}
32
33#[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
48impl<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}