wasmtime_environ/collections/
primary_map.rs1use core::{
2 fmt,
3 ops::{Index, IndexMut},
4};
5use cranelift_entity::EntityRef;
6use wasmtime_core::error::OutOfMemory;
7
8#[derive(Clone, Hash, PartialEq, Eq)]
11pub struct PrimaryMap<K, V>
12where
13 K: EntityRef,
14{
15 inner: cranelift_entity::PrimaryMap<K, V>,
16}
17
18impl<K, V> Default for PrimaryMap<K, V>
19where
20 K: EntityRef,
21{
22 fn default() -> Self {
23 Self {
24 inner: cranelift_entity::PrimaryMap::default(),
25 }
26 }
27}
28
29impl<K, V> fmt::Debug for PrimaryMap<K, V>
30where
31 K: EntityRef + fmt::Debug,
32 V: fmt::Debug,
33{
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 fmt::Debug::fmt(&self.inner, f)
36 }
37}
38
39impl<K, V> PrimaryMap<K, V>
40where
41 K: EntityRef,
42{
43 pub fn new() -> Self {
45 Self {
46 inner: cranelift_entity::PrimaryMap::new(),
47 }
48 }
49
50 pub fn with_capacity(capacity: usize) -> Result<Self, OutOfMemory> {
52 let mut map = Self::new();
53 map.reserve(capacity)?;
54 Ok(map)
55 }
56
57 pub fn is_valid(&self, k: K) -> bool {
59 self.inner.is_valid(k)
60 }
61
62 pub fn get(&self, k: K) -> Option<&V> {
64 self.inner.get(k)
65 }
66
67 pub fn get_range(&self, range: core::ops::Range<K>) -> Option<&[V]> {
69 self.inner.get_range(range)
70 }
71
72 pub fn get_mut(&mut self, k: K) -> Option<&mut V> {
74 self.inner.get_mut(k)
75 }
76
77 pub fn is_empty(&self) -> bool {
79 self.inner.is_empty()
80 }
81
82 pub fn len(&self) -> usize {
84 self.inner.len()
85 }
86
87 pub fn keys(&self) -> cranelift_entity::Keys<K> {
89 self.inner.keys()
90 }
91
92 pub fn values(&self) -> core::slice::Iter<'_, V> {
94 self.inner.values()
95 }
96
97 pub fn values_mut(&mut self) -> core::slice::IterMut<'_, V> {
99 self.inner.values_mut()
100 }
101
102 pub fn as_values_slice(&self) -> &[V] {
104 self.inner.as_values_slice()
105 }
106
107 pub fn iter(&self) -> cranelift_entity::Iter<'_, K, V> {
109 self.inner.iter()
110 }
111
112 pub fn iter_mut(&mut self) -> cranelift_entity::IterMut<'_, K, V> {
114 self.inner.iter_mut()
115 }
116
117 pub fn clear(&mut self) {
119 self.inner.clear()
120 }
121
122 pub fn next_key(&self) -> K {
124 self.inner.next_key()
125 }
126
127 pub fn push(&mut self, v: V) -> Result<K, OutOfMemory> {
130 self.reserve(1)?;
131 Ok(self.inner.push(v))
132 }
133
134 pub fn last(&self) -> Option<(K, &V)> {
136 self.inner.last()
137 }
138
139 pub fn last_mut(&mut self) -> Option<(K, &mut V)> {
141 self.inner.last_mut()
142 }
143
144 pub fn reserve(&mut self, additional: usize) -> Result<(), OutOfMemory> {
146 self.inner.try_reserve(additional)
147 }
148
149 pub fn reserve_exact(&mut self, additional: usize) -> Result<(), OutOfMemory> {
151 self.inner.try_reserve_exact(additional)
152 }
153
154 pub fn get_disjoint_mut<const N: usize>(
156 &mut self,
157 indices: [K; N],
158 ) -> Result<[&mut V; N], core::slice::GetDisjointMutError> {
159 self.inner.get_disjoint_mut(indices)
160 }
161
162 pub fn binary_search_values_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<K, K>
164 where
165 F: FnMut(&'a V) -> B,
166 B: Ord,
167 {
168 self.inner.binary_search_values_by_key(b, f)
169 }
170
171 pub fn get_raw_mut(&mut self, k: K) -> Option<*mut V> {
173 self.inner.get_raw_mut(k)
174 }
175}
176
177impl<K, V> Index<K> for PrimaryMap<K, V>
178where
179 K: EntityRef,
180{
181 type Output = V;
182
183 fn index(&self, k: K) -> &V {
184 &self.inner[k]
185 }
186}
187
188impl<K, V> IndexMut<K> for PrimaryMap<K, V>
189where
190 K: EntityRef,
191{
192 fn index_mut(&mut self, k: K) -> &mut V {
193 &mut self.inner[k]
194 }
195}