Path: blob/main/crates/environ/src/collections/hash_set.rs
3073 views
use crate::error::OutOfMemory;1use core::{2borrow::Borrow,3fmt,4hash::{BuildHasher, Hash},5};67#[cfg(feature = "std")]8use std::{collections::hash_set as inner, hash::RandomState as DefaultHashBuilder};910#[cfg(not(feature = "std"))]11use hashbrown::{DefaultHashBuilder, hash_set as inner};1213/// A wrapper type around [`hashbrown::hash_set::HashSet`] that only exposes14/// fallible allocation.15pub struct HashSet<T, S = DefaultHashBuilder> {16inner: inner::HashSet<T, S>,17}1819impl<T, S> Default for HashSet<T, S>20where21S: Default,22{23fn default() -> Self {24Self {25inner: inner::HashSet::default(),26}27}28}2930impl<T, S> PartialEq for HashSet<T, S>31where32T: Eq + Hash,33S: BuildHasher,34{35fn eq(&self, other: &Self) -> bool {36self.inner.eq(&other.inner)37}38}3940impl<T, S> Eq for HashSet<T, S>41where42T: Eq + Hash,43S: BuildHasher,44{45}4647impl<T, S> fmt::Debug for HashSet<T, S>48where49T: fmt::Debug,50{51fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {52fmt::Debug::fmt(&self.inner, f)53}54}5556impl<T> HashSet<T, DefaultHashBuilder> {57/// Same as [`hashbrown::hash_set::HashSet::new`].58pub fn new() -> Self {59Self {60inner: inner::HashSet::new(),61}62}63}6465impl<T> HashSet<T, DefaultHashBuilder>66where67T: Eq + Hash,68{69/// Same as [`hashbrown::hash_set::HashSet::with_capacity`] but returns an70/// error on allocation failure.71pub fn with_capacity(capacity: usize) -> Result<Self, OutOfMemory> {72let mut set = Self::new();73set.reserve(capacity)?;74Ok(set)75}76}7778impl<T, S> HashSet<T, S> {79/// Same as [`hashbrown::hash_set::HashSet::with_hasher`].80pub const fn with_hasher(hasher: S) -> Self {81Self {82inner: inner::HashSet::with_hasher(hasher),83}84}8586/// Same as [`hashbrown::hash_set::HashSet::capacity`].87pub fn capacity(&self) -> usize {88self.inner.capacity()89}9091/// Same as [`hashbrown::hash_set::HashSet::iter`].92pub fn iter(&self) -> inner::Iter<'_, T> {93self.inner.iter()94}9596/// Same as [`hashbrown::hash_set::HashSet::len`].97pub fn len(&self) -> usize {98self.inner.len()99}100101/// Same as [`hashbrown::hash_set::HashSet::is_empty`].102pub fn is_empty(&self) -> bool {103self.inner.is_empty()104}105106/// Same as [`hashbrown::hash_set::HashSet::drain`].107pub fn drain(&mut self) -> inner::Drain<'_, T> {108self.inner.drain()109}110111/// Same as [`hashbrown::hash_set::HashSet::retain`].112pub fn retain<F>(&mut self, f: F)113where114F: FnMut(&T) -> bool,115{116self.inner.retain(f);117}118119/// Same as [`hashbrown::hash_set::HashSet::extract_if`].120pub fn extract_if<F>(&mut self, f: F) -> inner::ExtractIf<'_, T, F>121where122F: FnMut(&T) -> bool,123{124self.inner.extract_if(f)125}126127/// Same as [`hashbrown::hash_set::HashSet::clear`].128pub fn clear(&mut self) {129self.inner.clear();130}131}132133impl<T, S> HashSet<T, S>134where135T: Eq + Hash,136S: BuildHasher,137{138/// Same as [`hashbrown::hash_set::HashSet::with_capacity_and_hasher`] but139/// returns an error on allocation failure.140pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Result<Self, OutOfMemory> {141let mut map = Self::with_hasher(hasher);142map.reserve(capacity)?;143Ok(map)144}145146/// Same as [`hashbrown::hash_set::HashSet::reserve`] but returns an error147/// on allocation failure.148pub fn reserve(&mut self, additional: usize) -> Result<(), OutOfMemory> {149self.inner.try_reserve(additional).map_err(|_| {150OutOfMemory::new(151self.len()152.saturating_add(additional)153.saturating_mul(core::mem::size_of::<T>()),154)155})156}157158/// Same as [`hashbrown::hash_set::HashSet::contains`].159pub fn contains<Q>(&self, value: &Q) -> bool160where161Q: Hash + Eq + ?Sized,162T: Borrow<Q>,163{164self.inner.contains(value)165}166167/// Same as [`hashbrown::hash_set::HashSet::get`].168pub fn get<Q>(&self, value: &Q) -> Option<&T>169where170Q: Hash + Eq + ?Sized,171T: Borrow<Q>,172{173self.inner.get(value)174}175176/// Same as [`hashbrown::hash_set::HashSet::insert`] but returns an error on177/// allocation failure.178pub fn insert(&mut self, value: T) -> Result<bool, OutOfMemory> {179self.reserve(1)?;180Ok(self.inner.insert(value))181}182183/// Same as [`hashbrown::hash_set::HashSet::remove`].184pub fn remove<Q>(&mut self, value: &Q) -> bool185where186Q: Hash + Eq + ?Sized,187T: Borrow<Q>,188{189self.inner.remove(value)190}191192/// Same as [`hashbrown::hash_set::HashSet::take`].193pub fn take<Q>(&mut self, value: &Q) -> Option<T>194where195Q: Hash + Eq + ?Sized,196T: Borrow<Q>,197{198self.inner.take(value)199}200}201202203