Skip to content

Commit 609f649

Browse files
committed
Modify StableSet with a HashStable implementation.
1 parent 2d5a21f commit 609f649

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

compiler/rustc_data_structures/src/stable_hasher.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -544,19 +544,6 @@ where
544544
}
545545
}
546546

547-
impl<K, R, HCX> HashStable<HCX> for ::std::collections::HashSet<K, R>
548-
where
549-
K: ToStableHashKey<HCX> + Eq,
550-
R: BuildHasher,
551-
{
552-
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
553-
stable_hash_reduce(hcx, hasher, self.iter(), self.len(), |hasher, hcx, key| {
554-
let key = key.to_stable_hash_key(hcx);
555-
key.hash_stable(hcx, hasher);
556-
});
557-
}
558-
}
559-
560547
impl<K, V, HCX> HashStable<HCX> for ::std::collections::BTreeMap<K, V>
561548
where
562549
K: ToStableHashKey<HCX>,
@@ -583,7 +570,7 @@ where
583570
}
584571
}
585572

586-
fn stable_hash_reduce<HCX, I, C, F>(
573+
pub fn stable_hash_reduce<HCX, I, C, F>(
587574
hcx: &mut HCX,
588575
hasher: &mut StableHasher,
589576
mut collection: C,

compiler/rustc_data_structures/src/stable_set.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::borrow::Borrow;
33
use std::fmt;
44
use std::hash::Hash;
55

6+
use crate::stable_hasher::{stable_hash_reduce, HashStable, StableHasher, ToStableHashKey};
7+
68
/// A deterministic wrapper around FxHashSet that does not provide iteration support.
79
///
810
/// It supports insert, remove, get functions from FxHashSet.
@@ -16,6 +18,7 @@ impl<T> Default for StableSet<T>
1618
where
1719
T: Eq + Hash,
1820
{
21+
#[inline]
1922
fn default() -> StableSet<T> {
2023
StableSet::new()
2124
}
@@ -25,6 +28,7 @@ impl<T> fmt::Debug for StableSet<T>
2528
where
2629
T: Eq + Hash + fmt::Debug,
2730
{
31+
#[inline]
2832
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2933
write!(f, "{:?}", self.base)
3034
}
@@ -34,6 +38,7 @@ impl<T> PartialEq<StableSet<T>> for StableSet<T>
3438
where
3539
T: Eq + Hash,
3640
{
41+
#[inline]
3742
fn eq(&self, other: &StableSet<T>) -> bool {
3843
self.base == other.base
3944
}
@@ -42,19 +47,22 @@ where
4247
impl<T> Eq for StableSet<T> where T: Eq + Hash {}
4348

4449
impl<T: Hash + Eq> StableSet<T> {
50+
#[inline]
4551
pub fn new() -> StableSet<T> {
4652
StableSet { base: FxHashSet::default() }
4753
}
4854

49-
pub fn into_sorted_vector(self) -> Vec<T>
55+
#[inline]
56+
pub fn into_sorted_vector<HCX>(self, hcx: &HCX) -> Vec<T>
5057
where
51-
T: Ord,
58+
T: ToStableHashKey<HCX>,
5259
{
5360
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));
5562
vector
5663
}
5764

65+
#[inline]
5866
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
5967
where
6068
T: Borrow<Q>,
@@ -63,15 +71,35 @@ impl<T: Hash + Eq> StableSet<T> {
6371
self.base.get(value)
6472
}
6573

74+
#[inline]
6675
pub fn insert(&mut self, value: T) -> bool {
6776
self.base.insert(value)
6877
}
6978

79+
#[inline]
7080
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
7181
where
7282
T: Borrow<Q>,
7383
Q: Hash + Eq,
7484
{
7585
self.base.remove(value)
7686
}
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+
}
77105
}

0 commit comments

Comments
 (0)