Path: blob/main/crates/environ/src/collections/primary_map.rs
3076 views
use core::{1fmt,2ops::{Index, IndexMut},3};4use cranelift_entity::EntityRef;5use wasmtime_core::error::OutOfMemory;67/// Like [`cranelift_entity::PrimaryMap`] but enforces fallible allocation for8/// all methods that allocate.9#[derive(Clone, Hash, PartialEq, Eq)]10pub struct PrimaryMap<K, V>11where12K: EntityRef,13{14inner: cranelift_entity::PrimaryMap<K, V>,15}1617impl<K, V> Default for PrimaryMap<K, V>18where19K: EntityRef,20{21fn default() -> Self {22Self {23inner: cranelift_entity::PrimaryMap::default(),24}25}26}2728impl<K, V> fmt::Debug for PrimaryMap<K, V>29where30K: EntityRef + fmt::Debug,31V: fmt::Debug,32{33fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {34fmt::Debug::fmt(&self.inner, f)35}36}3738impl<K, V> PrimaryMap<K, V>39where40K: EntityRef,41{42/// Same as [`cranelift_entity::PrimaryMap::new`].43pub fn new() -> Self {44Self {45inner: cranelift_entity::PrimaryMap::new(),46}47}4849/// Same as [`cranelift_entity::PrimaryMap::try_with_capacity`].50pub fn with_capacity(capacity: usize) -> Result<Self, OutOfMemory> {51let mut map = Self::new();52map.reserve(capacity)?;53Ok(map)54}5556/// Same as [`cranelift_entity::PrimaryMap::is_valid`].57pub fn is_valid(&self, k: K) -> bool {58self.inner.is_valid(k)59}6061/// Same as [`cranelift_entity::PrimaryMap::get`].62pub fn get(&self, k: K) -> Option<&V> {63self.inner.get(k)64}6566/// Same as [`cranelift_entity::PrimaryMap::get_range`].67pub fn get_range(&self, range: core::ops::Range<K>) -> Option<&[V]> {68self.inner.get_range(range)69}7071/// Same as [`cranelift_entity::PrimaryMap::get_mut`].72pub fn get_mut(&mut self, k: K) -> Option<&mut V> {73self.inner.get_mut(k)74}7576/// Same as [`cranelift_entity::PrimaryMap::is_empty`].77pub fn is_empty(&self) -> bool {78self.inner.is_empty()79}8081/// Same as [`cranelift_entity::PrimaryMap::len`].82pub fn len(&self) -> usize {83self.inner.len()84}8586/// Same as [`cranelift_entity::PrimaryMap::keys`].87pub fn keys(&self) -> cranelift_entity::Keys<K> {88self.inner.keys()89}9091/// Same as [`cranelift_entity::PrimaryMap::values`].92pub fn values(&self) -> core::slice::Iter<'_, V> {93self.inner.values()94}9596/// Same as [`cranelift_entity::PrimaryMap::values_mut`].97pub fn values_mut(&mut self) -> core::slice::IterMut<'_, V> {98self.inner.values_mut()99}100101/// Same as [`cranelift_entity::PrimaryMap::as_values_slice`].102pub fn as_values_slice(&self) -> &[V] {103self.inner.as_values_slice()104}105106/// Same as [`cranelift_entity::PrimaryMap::iter`].107pub fn iter(&self) -> cranelift_entity::Iter<'_, K, V> {108self.inner.iter()109}110111/// Same as [`cranelift_entity::PrimaryMap::iter_mut`].112pub fn iter_mut(&mut self) -> cranelift_entity::IterMut<'_, K, V> {113self.inner.iter_mut()114}115116/// Same as [`cranelift_entity::PrimaryMap::clear`].117pub fn clear(&mut self) {118self.inner.clear()119}120121/// Same as [`cranelift_entity::PrimaryMap::next_key`].122pub fn next_key(&self) -> K {123self.inner.next_key()124}125126/// Same as [`cranelift_entity::PrimaryMap::push`] but returns an error on127/// allocation failure.128pub fn push(&mut self, v: V) -> Result<K, OutOfMemory> {129self.reserve(1)?;130Ok(self.inner.push(v))131}132133/// Same as [`cranelift_entity::PrimaryMap::last`].134pub fn last(&self) -> Option<(K, &V)> {135self.inner.last()136}137138/// Same as [`cranelift_entity::PrimaryMap::last_mut`].139pub fn last_mut(&mut self) -> Option<(K, &mut V)> {140self.inner.last_mut()141}142143/// Same as [`cranelift_entity::PrimaryMap::try_reserve`].144pub fn reserve(&mut self, additional: usize) -> Result<(), OutOfMemory> {145self.inner.try_reserve(additional)146}147148/// Same as [`cranelift_entity::PrimaryMap::try_reserve_exact`].149pub fn reserve_exact(&mut self, additional: usize) -> Result<(), OutOfMemory> {150self.inner.try_reserve_exact(additional)151}152153/// Same as [`cranelift_entity::PrimaryMap::get_disjoint_mut`].154pub fn get_disjoint_mut<const N: usize>(155&mut self,156indices: [K; N],157) -> Result<[&mut V; N], core::slice::GetDisjointMutError> {158self.inner.get_disjoint_mut(indices)159}160161/// Same as [`cranelift_entity::PrimaryMap::binary_search_values_by_key`].162pub fn binary_search_values_by_key<'a, B, F>(&'a self, b: &B, f: F) -> Result<K, K>163where164F: FnMut(&'a V) -> B,165B: Ord,166{167self.inner.binary_search_values_by_key(b, f)168}169170/// Same as [`cranelift_entity::PrimaryMap::get_raw_mut`].171pub fn get_raw_mut(&mut self, k: K) -> Option<*mut V> {172self.inner.get_raw_mut(k)173}174}175176impl<K, V> Index<K> for PrimaryMap<K, V>177where178K: EntityRef,179{180type Output = V;181182fn index(&self, k: K) -> &V {183&self.inner[k]184}185}186187impl<K, V> IndexMut<K> for PrimaryMap<K, V>188where189K: EntityRef,190{191fn index_mut(&mut self, k: K) -> &mut V {192&mut self.inner[k]193}194}195196197