@@ -3,6 +3,8 @@ use std::borrow::Borrow;
3
3
use std:: fmt;
4
4
use std:: hash:: Hash ;
5
5
6
+ use crate :: stable_hasher:: { stable_hash_reduce, HashStable , StableHasher , ToStableHashKey } ;
7
+
6
8
/// A deterministic wrapper around FxHashSet that does not provide iteration support.
7
9
///
8
10
/// It supports insert, remove, get functions from FxHashSet.
@@ -16,6 +18,7 @@ impl<T> Default for StableSet<T>
16
18
where
17
19
T : Eq + Hash ,
18
20
{
21
+ #[ inline]
19
22
fn default ( ) -> StableSet < T > {
20
23
StableSet :: new ( )
21
24
}
@@ -25,6 +28,7 @@ impl<T> fmt::Debug for StableSet<T>
25
28
where
26
29
T : Eq + Hash + fmt:: Debug ,
27
30
{
31
+ #[ inline]
28
32
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
29
33
write ! ( f, "{:?}" , self . base)
30
34
}
@@ -34,6 +38,7 @@ impl<T> PartialEq<StableSet<T>> for StableSet<T>
34
38
where
35
39
T : Eq + Hash ,
36
40
{
41
+ #[ inline]
37
42
fn eq ( & self , other : & StableSet < T > ) -> bool {
38
43
self . base == other. base
39
44
}
@@ -42,19 +47,22 @@ where
42
47
impl < T > Eq for StableSet < T > where T : Eq + Hash { }
43
48
44
49
impl < T : Hash + Eq > StableSet < T > {
50
+ #[ inline]
45
51
pub fn new ( ) -> StableSet < T > {
46
52
StableSet { base : FxHashSet :: default ( ) }
47
53
}
48
54
49
- pub fn into_sorted_vector ( self ) -> Vec < T >
55
+ #[ inline]
56
+ pub fn into_sorted_vector < HCX > ( self , hcx : & HCX ) -> Vec < T >
50
57
where
51
- T : Ord ,
58
+ T : ToStableHashKey < HCX > ,
52
59
{
53
60
let mut vector = self . base . into_iter ( ) . collect :: < Vec < _ > > ( ) ;
54
- vector. sort_unstable ( ) ;
61
+ vector. sort_by_cached_key ( |x| x . to_stable_hash_key ( hcx ) ) ;
55
62
vector
56
63
}
57
64
65
+ #[ inline]
58
66
pub fn get < Q : ?Sized > ( & self , value : & Q ) -> Option < & T >
59
67
where
60
68
T : Borrow < Q > ,
@@ -63,15 +71,35 @@ impl<T: Hash + Eq> StableSet<T> {
63
71
self . base . get ( value)
64
72
}
65
73
74
+ #[ inline]
66
75
pub fn insert ( & mut self , value : T ) -> bool {
67
76
self . base . insert ( value)
68
77
}
69
78
79
+ #[ inline]
70
80
pub fn remove < Q : ?Sized > ( & mut self , value : & Q ) -> bool
71
81
where
72
82
T : Borrow < Q > ,
73
83
Q : Hash + Eq ,
74
84
{
75
85
self . base . remove ( value)
76
86
}
87
+
88
+ #[ inline]
89
+ pub fn contains ( & self , value : & T ) -> bool {
90
+ self . base . contains ( value)
91
+ }
92
+ }
93
+
94
+ impl < T , HCX > HashStable < HCX > for StableSet < T >
95
+ where
96
+ T : ToStableHashKey < HCX > + Eq ,
97
+ {
98
+ #[ inline]
99
+ fn hash_stable ( & self , hcx : & mut HCX , hasher : & mut StableHasher ) {
100
+ stable_hash_reduce ( hcx, hasher, self . base . iter ( ) , self . base . len ( ) , |hasher, hcx, key| {
101
+ let key = key. to_stable_hash_key ( hcx) ;
102
+ key. hash_stable ( hcx, hasher) ;
103
+ } ) ;
104
+ }
77
105
}
0 commit comments