Skip to main content

wasmtime_environ/collections/
secondary_map.rs

1use crate::{collections::Vec, error::OutOfMemory};
2use core::{fmt, ops::Index};
3use cranelift_entity::{EntityRef, SecondaryMap as Inner};
4use serde::ser::SerializeSeq;
5
6/// Like [`cranelift_entity::SecondaryMap`] but all allocation is fallible.
7pub struct SecondaryMap<K, V>
8where
9    K: EntityRef,
10    V: Clone,
11{
12    inner: Inner<K, V>,
13}
14
15impl<K, V> fmt::Debug for SecondaryMap<K, V>
16where
17    K: EntityRef + fmt::Debug,
18    V: fmt::Debug + Clone,
19{
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        fmt::Debug::fmt(&self.inner, f)
22    }
23}
24
25impl<K, V> SecondaryMap<K, V>
26where
27    K: EntityRef,
28    V: Clone,
29{
30    /// Same as [`cranelift_entity::SecondaryMap::new`].
31    pub fn new() -> Self
32    where
33        V: Default,
34    {
35        Self {
36            inner: Inner::new(),
37        }
38    }
39
40    /// Same as [`cranelift_entity::SecondaryMap::try_with_capacity`].
41    pub fn with_capacity(capacity: usize) -> Result<Self, OutOfMemory>
42    where
43        V: Default,
44    {
45        Ok(Self {
46            inner: Inner::try_with_capacity(capacity)?,
47        })
48    }
49
50    /// Same as [`cranelift_entity::SecondaryMap::with_default`].
51    pub fn with_default(default: V) -> Self {
52        Self {
53            inner: Inner::with_default(default),
54        }
55    }
56
57    /// Same as [`cranelift_entity::SecondaryMap::capacity`].
58    pub fn capacity(&self) -> usize {
59        self.inner.capacity()
60    }
61
62    /// Same as [`cranelift_entity::SecondaryMap::get`].
63    pub fn get(&self, k: K) -> Option<&V> {
64        self.inner.get(k)
65    }
66
67    /// Same as [`cranelift_entity::SecondaryMap::get_mut`].
68    pub fn get_mut(&mut self, k: K) -> Option<&mut V> {
69        self.inner.get_mut(k)
70    }
71
72    /// Same as [`cranelift_entity::SecondaryMap::try_insert`].
73    pub fn insert(&mut self, k: K, v: V) -> Result<Option<V>, OutOfMemory> {
74        self.inner.try_insert(k, v)
75    }
76
77    /// Same as [`cranelift_entity::SecondaryMap::remove`].
78    pub fn remove(&mut self, k: K) -> Option<V> {
79        self.inner.remove(k)
80    }
81
82    /// Same as [`cranelift_entity::SecondaryMap::is_empty`].
83    pub fn is_empty(&self) -> bool {
84        self.inner.is_empty()
85    }
86
87    /// Same as [`cranelift_entity::SecondaryMap::clear`].
88    pub fn clear(&mut self) {
89        self.inner.clear()
90    }
91
92    /// Same as [`cranelift_entity::SecondaryMap::iter`].
93    pub fn iter(&self) -> cranelift_entity::Iter<'_, K, V> {
94        self.inner.iter()
95    }
96
97    /// Same as [`cranelift_entity::SecondaryMap::iter_mut`].
98    pub fn iter_mut(&mut self) -> cranelift_entity::IterMut<'_, K, V> {
99        self.inner.iter_mut()
100    }
101
102    /// Same as [`cranelift_entity::SecondaryMap::keys`].
103    pub fn keys(&self) -> cranelift_entity::Keys<K> {
104        self.inner.keys()
105    }
106
107    /// Same as [`cranelift_entity::SecondaryMap::values`].
108    pub fn values(&self) -> core::slice::Iter<'_, V> {
109        self.inner.values()
110    }
111
112    /// Same as [`cranelift_entity::SecondaryMap::values_mut`].
113    pub fn values_mut(&mut self) -> core::slice::IterMut<'_, V> {
114        self.inner.values_mut()
115    }
116
117    /// Resize the map to have `n` entries by adding default entries as needed.
118    pub fn resize(&mut self, n: usize) -> Result<(), OutOfMemory> {
119        self.inner.try_resize(n)
120    }
121}
122
123impl<K, V> Default for SecondaryMap<K, V>
124where
125    K: EntityRef,
126    V: Clone + Default,
127{
128    fn default() -> SecondaryMap<K, V> {
129        SecondaryMap::new()
130    }
131}
132
133// NB: no `IndexMut` implementation because it requires allocation but the trait
134// doesn't allow for fallibility.
135impl<K, V> Index<K> for SecondaryMap<K, V>
136where
137    K: EntityRef,
138    V: Clone,
139{
140    type Output = V;
141
142    fn index(&self, k: K) -> &V {
143        &self.inner[k]
144    }
145}
146
147impl<K, V> From<Vec<V>> for SecondaryMap<K, V>
148where
149    K: EntityRef,
150    V: Clone + Default,
151{
152    fn from(values: Vec<V>) -> Self {
153        let values: alloc::vec::Vec<V> = values.into();
154        let inner = Inner::from(values);
155        Self { inner }
156    }
157}
158
159impl<K, V> serde::ser::Serialize for SecondaryMap<K, V>
160where
161    K: EntityRef,
162    V: Clone + serde::ser::Serialize,
163{
164    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
165    where
166        S: serde::Serializer,
167    {
168        let mut seq = serializer.serialize_seq(Some(self.capacity()))?;
169        for elem in self.values() {
170            seq.serialize_element(elem)?;
171        }
172        seq.end()
173    }
174}
175
176impl<'de, K, V> serde::de::Deserialize<'de> for SecondaryMap<K, V>
177where
178    K: EntityRef,
179    V: Clone + Default + serde::de::Deserialize<'de>,
180{
181    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
182    where
183        D: serde::Deserializer<'de>,
184    {
185        let values: Vec<V> = serde::de::Deserialize::deserialize(deserializer)?;
186        Ok(Self::from(values))
187    }
188}