Path: blob/main/crates/environ/src/collections/entity_set.rs
3061 views
use cranelift_entity::{EntityRef, Keys, SetIter};1use wasmtime_core::error::OutOfMemory;23/// Like `cranelift_entity::EntitySet` but enforces fallible allocation for all4/// methods that allocate.5#[derive(Debug, Default)]6pub struct EntitySet<K>7where8K: EntityRef,9{10inner: cranelift_entity::EntitySet<K>,11}1213impl<K> EntitySet<K>14where15K: EntityRef,16{17/// Create a new empty set.18pub fn new() -> Self {19EntitySet {20inner: Default::default(),21}22}2324/// Creates a new empty set with the specified capacity.25pub fn with_capacity(capacity: usize) -> Result<Self, OutOfMemory> {26let mut set = Self::new();27set.inner.try_ensure_capacity(capacity)?;28Ok(set)29}3031/// Ensure that there is enough capacity to hold `capacity` total elements.32pub fn ensure_capacity(&mut self, capacity: usize) -> Result<(), OutOfMemory> {33self.inner.try_ensure_capacity(capacity)34}3536/// Is this set completely empty?37pub fn is_empty(&self) -> bool {38self.inner.is_empty()39}4041/// Get the element at `k` if it exists.42pub fn contains(&self, k: K) -> bool {43self.inner.contains(k)44}4546/// Remove all entries from this set.47pub fn clear(&mut self) {48self.inner.clear();49}5051/// Iterate over all the keys up to the maximum in this set.52///53/// This will yield intermediate keys on the way up to the max key, even if54/// they are not contained within the set.55pub fn keys(&self) -> Keys<K> {56self.inner.keys()57}5859/// Iterate over the elements of this set.60pub fn iter(&self) -> SetIter<'_, K> {61self.inner.iter()62}6364/// Insert the element at `k`.65///66/// Returns `true` if `k` was not present in the set, i.e. this is a67/// newly-added element. Returns `false` otherwise.68pub fn insert(&mut self, k: K) -> Result<bool, OutOfMemory> {69self.inner.try_ensure_capacity(k.index() + 1)?;70Ok(self.inner.insert(k))71}7273/// Remove `k` from this bitset.74///75/// Returns whether `k` was previously in this set or not.76pub fn remove(&mut self, k: K) -> bool {77self.inner.remove(k)78}7980/// Removes and returns the highest-index entity from the set if it exists.81pub fn pop(&mut self) -> Option<K> {82self.inner.pop()83}84}858687