pub use rustc_hash::FxHashSet; use std::borrow::Borrow; use std::fmt; use std::hash::Hash; /// A deterministic wrapper around FxHashSet that does not provide iteration support. /// /// It supports insert, remove, get functions from FxHashSet. /// It also allows to convert hashset to a sorted vector with the method `into_sorted_vector()`. #[derive(Clone)] pub struct StableSet { base: FxHashSet, } impl Default for StableSet where T: Eq + Hash, { fn default() -> StableSet { StableSet::new() } } impl fmt::Debug for StableSet where T: Eq + Hash + fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self.base) } } impl PartialEq> for StableSet where T: Eq + Hash, { fn eq(&self, other: &StableSet) -> bool { self.base == other.base } } impl Eq for StableSet where T: Eq + Hash {} impl StableSet { pub fn new() -> StableSet { StableSet { base: FxHashSet::default() } } pub fn into_sorted_vector(self) -> Vec where T: Ord, { let mut vector = self.base.into_iter().collect::>(); vector.sort_unstable(); vector } pub fn get(&self, value: &Q) -> Option<&T> where T: Borrow, Q: Hash + Eq, { self.base.get(value) } pub fn insert(&mut self, value: T) -> bool { self.base.insert(value) } pub fn remove(&mut self, value: &Q) -> bool where T: Borrow, Q: Hash + Eq, { self.base.remove(value) } }