Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/environ/src/collections/primary_map.rs
3076 views
1
use core::{
2
fmt,
3
ops::{Index, IndexMut},
4
};
5
use cranelift_entity::EntityRef;
6
use wasmtime_core::error::OutOfMemory;
7
8
/// Like [`cranelift_entity::PrimaryMap`] but enforces fallible allocation for
9
/// all methods that allocate.
10
#[derive(Clone, Hash, PartialEq, Eq)]
11
pub struct PrimaryMap<K, V>
12
where
13
K: EntityRef,
14
{
15
inner: cranelift_entity::PrimaryMap<K, V>,
16
}
17
18
impl<K, V> Default for PrimaryMap<K, V>
19
where
20
K: EntityRef,
21
{
22
fn default() -> Self {
23
Self {
24
inner: cranelift_entity::PrimaryMap::default(),
25
}
26
}
27
}
28
29
impl<K, V> fmt::Debug for PrimaryMap<K, V>
30
where
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
39
impl<K, V> PrimaryMap<K, V>
40
where
41
K: EntityRef,
42
{
43
/// Same as [`cranelift_entity::PrimaryMap::new`].
44
pub fn new() -> Self {
45
Self {
46
inner: cranelift_entity::PrimaryMap::new(),
47
}
48
}
49
50
/// Same as [`cranelift_entity::PrimaryMap::try_with_capacity`].
51
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
/// Same as [`cranelift_entity::PrimaryMap::is_valid`].
58
pub fn is_valid(&self, k: K) -> bool {
59
self.inner.is_valid(k)
60
}
61
62
/// Same as [`cranelift_entity::PrimaryMap::get`].
63
pub fn get(&self, k: K) -> Option<&V> {
64
self.inner.get(k)
65
}
66
67
/// Same as [`cranelift_entity::PrimaryMap::get_range`].
68
pub fn get_range(&self, range: core::ops::Range<K>) -> Option<&[V]> {
69
self.inner.get_range(range)
70
}
71
72
/// Same as [`cranelift_entity::PrimaryMap::get_mut`].
73
pub fn get_mut(&mut self, k: K) -> Option<&mut V> {
74
self.inner.get_mut(k)
75
}
76
77
/// Same as [`cranelift_entity::PrimaryMap::is_empty`].
78
pub fn is_empty(&self) -> bool {
79
self.inner.is_empty()
80
}
81
82
/// Same as [`cranelift_entity::PrimaryMap::len`].
83
pub fn len(&self) -> usize {
84
self.inner.len()
85
}
86
87
/// Same as [`cranelift_entity::PrimaryMap::keys`].
88
pub fn keys(&self) -> cranelift_entity::Keys<K> {
89
self.inner.keys()
90
}
91
92
/// Same as [`cranelift_entity::PrimaryMap::values`].
93
pub fn values(&self) -> core::slice::Iter<'_, V> {
94
self.inner.values()
95
}
96
97
/// Same as [`cranelift_entity::PrimaryMap::values_mut`].
98
pub fn values_mut(&mut self) -> core::slice::IterMut<'_, V> {
99
self.inner.values_mut()
100
}
101
102
/// Same as [`cranelift_entity::PrimaryMap::as_values_slice`].
103
pub fn as_values_slice(&self) -> &[V] {
104
self.inner.as_values_slice()
105
}
106
107
/// Same as [`cranelift_entity::PrimaryMap::iter`].
108
pub fn iter(&self) -> cranelift_entity::Iter<'_, K, V> {
109
self.inner.iter()
110
}
111
112
/// Same as [`cranelift_entity::PrimaryMap::iter_mut`].
113
pub fn iter_mut(&mut self) -> cranelift_entity::IterMut<'_, K, V> {
114
self.inner.iter_mut()
115
}
116
117
/// Same as [`cranelift_entity::PrimaryMap::clear`].
118
pub fn clear(&mut self) {
119
self.inner.clear()
120
}
121
122
/// Same as [`cranelift_entity::PrimaryMap::next_key`].
123
pub fn next_key(&self) -> K {
124
self.inner.next_key()
125
}
126
127
/// Same as [`cranelift_entity::PrimaryMap::push`] but returns an error on
128
/// allocation failure.
129
pub fn push(&mut self, v: V) -> Result<K, OutOfMemory> {
130
self.reserve(1)?;
131
Ok(self.inner.push(v))
132
}
133
134
/// Same as [`cranelift_entity::PrimaryMap::last`].
135
pub fn last(&self) -> Option<(K, &V)> {
136
self.inner.last()
137
}
138
139
/// Same as [`cranelift_entity::PrimaryMap::last_mut`].
140
pub fn last_mut(&mut self) -> Option<(K, &mut V)> {
141
self.inner.last_mut()
142
}
143
144
/// Same as [`cranelift_entity::PrimaryMap::try_reserve`].
145
pub fn reserve(&mut self, additional: usize) -> Result<(), OutOfMemory> {
146
self.inner.try_reserve(additional)
147
}
148
149
/// Same as [`cranelift_entity::PrimaryMap::try_reserve_exact`].
150
pub fn reserve_exact(&mut self, additional: usize) -> Result<(), OutOfMemory> {
151
self.inner.try_reserve_exact(additional)
152
}
153
154
/// Same as [`cranelift_entity::PrimaryMap::get_disjoint_mut`].
155
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
/// Same as [`cranelift_entity::PrimaryMap::binary_search_values_by_key`].
163
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
/// Same as [`cranelift_entity::PrimaryMap::get_raw_mut`].
172
pub fn get_raw_mut(&mut self, k: K) -> Option<*mut V> {
173
self.inner.get_raw_mut(k)
174
}
175
}
176
177
impl<K, V> Index<K> for PrimaryMap<K, V>
178
where
179
K: EntityRef,
180
{
181
type Output = V;
182
183
fn index(&self, k: K) -> &V {
184
&self.inner[k]
185
}
186
}
187
188
impl<K, V> IndexMut<K> for PrimaryMap<K, V>
189
where
190
K: EntityRef,
191
{
192
fn index_mut(&mut self, k: K) -> &mut V {
193
&mut self.inner[k]
194
}
195
}
196
197