]> git.proxmox.com Git - rustc.git/blob - vendor/rustc-ap-rustc_data_structures/src/stable_set.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / vendor / rustc-ap-rustc_data_structures / src / stable_set.rs
1 pub use rustc_hash::FxHashSet;
2 use std::borrow::Borrow;
3 use std::fmt;
4 use std::hash::Hash;
5
6 /// A deterministic wrapper around FxHashSet that does not provide iteration support.
7 ///
8 /// It supports insert, remove, get functions from FxHashSet.
9 /// It also allows to convert hashset to a sorted vector with the method `into_sorted_vector()`.
10 #[derive(Clone)]
11 pub struct StableSet<T> {
12 base: FxHashSet<T>,
13 }
14
15 impl<T> Default for StableSet<T>
16 where
17 T: Eq + Hash,
18 {
19 fn default() -> StableSet<T> {
20 StableSet::new()
21 }
22 }
23
24 impl<T> fmt::Debug for StableSet<T>
25 where
26 T: Eq + Hash + fmt::Debug,
27 {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 write!(f, "{:?}", self.base)
30 }
31 }
32
33 impl<T> PartialEq<StableSet<T>> for StableSet<T>
34 where
35 T: Eq + Hash,
36 {
37 fn eq(&self, other: &StableSet<T>) -> bool {
38 self.base == other.base
39 }
40 }
41
42 impl<T> Eq for StableSet<T> where T: Eq + Hash {}
43
44 impl<T: Hash + Eq> StableSet<T> {
45 pub fn new() -> StableSet<T> {
46 StableSet { base: FxHashSet::default() }
47 }
48
49 pub fn into_sorted_vector(self) -> Vec<T>
50 where
51 T: Ord,
52 {
53 let mut vector = self.base.into_iter().collect::<Vec<_>>();
54 vector.sort_unstable();
55 vector
56 }
57
58 pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
59 where
60 T: Borrow<Q>,
61 Q: Hash + Eq,
62 {
63 self.base.get(value)
64 }
65
66 pub fn insert(&mut self, value: T) -> bool {
67 self.base.insert(value)
68 }
69
70 pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
71 where
72 T: Borrow<Q>,
73 Q: Hash + Eq,
74 {
75 self.base.remove(value)
76 }
77 }