Skip to content

Commit 3829ac2

Browse files
committed
use TotalEq for HashMap
Closes #5283
1 parent 94e4e91 commit 3829ac2

File tree

28 files changed

+203
-180
lines changed

28 files changed

+203
-180
lines changed

src/libcollections/enum_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
use std::num::Bitwise;
1717

18-
#[deriving(Clone, Eq, Hash, Show)]
18+
#[deriving(Clone, Eq, TotalEq, Hash, Show)]
1919
/// A specialized Set implementation to use enum types.
2020
pub struct EnumSet<E> {
2121
// We must maintain the invariant that no bits are set

src/libcollections/hashmap.rs

+27-30
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use std::container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
1414
use std::clone::Clone;
15-
use std::cmp::{Eq, Equiv, max};
15+
use std::cmp::{Eq, TotalEq, Equiv, max};
1616
use std::default::Default;
1717
use std::fmt;
1818
use std::fmt::Show;
@@ -140,6 +140,7 @@ mod table {
140140
}
141141

142142
/// A hash that is not zero, since we use that to represent empty buckets.
143+
#[deriving(Eq)]
143144
pub struct SafeHash {
144145
priv hash: u64,
145146
}
@@ -149,10 +150,6 @@ mod table {
149150
pub fn inspect(&self) -> u64 { self.hash }
150151
}
151152

152-
impl Eq for SafeHash {
153-
fn eq(&self, other: &SafeHash) -> bool { self.hash == other.hash }
154-
}
155-
156153
/// We need to remove hashes of 0. That's reserved for empty buckets.
157154
/// This function wraps up `hash_keyed` to be the only way outside this
158155
/// module to generate a SafeHash.
@@ -698,7 +695,7 @@ fn grow_at(capacity: uint, load_factor: Fraction) -> uint {
698695
fraction_mul(capacity, load_factor)
699696
}
700697

701-
impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
698+
impl<K: TotalEq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
702699
/// Get the number of elements which will force the capacity to shrink.
703700
/// When size == self.shrink_at(), we halve the capacity.
704701
fn shrink_at(&self) -> uint {
@@ -799,12 +796,12 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
799796
}
800797
}
801798

802-
impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> Container for HashMap<K, V, H> {
799+
impl<K: TotalEq + Hash<S>, V, S, H: Hasher<S>> Container for HashMap<K, V, H> {
803800
/// Return the number of elements in the map
804801
fn len(&self) -> uint { self.table.size() }
805802
}
806803

807-
impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> Mutable for HashMap<K, V, H> {
804+
impl<K: TotalEq + Hash<S>, V, S, H: Hasher<S>> Mutable for HashMap<K, V, H> {
808805
/// Clear the map, removing all key-value pairs.
809806
fn clear(&mut self) {
810807
self.minimum_capacity = self.table.size();
@@ -819,7 +816,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> Mutable for HashMap<K, V, H> {
819816
}
820817

821818

822-
impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> Map<K, V> for HashMap<K, V, H> {
819+
impl<K: TotalEq + Hash<S>, V, S, H: Hasher<S>> Map<K, V> for HashMap<K, V, H> {
823820
fn find<'a>(&'a self, k: &K) -> Option<&'a V> {
824821
self.search(k).map(|idx| {
825822
let (_, v) = self.table.read(&idx);
@@ -832,7 +829,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> Map<K, V> for HashMap<K, V, H> {
832829
}
833830
}
834831

835-
impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> MutableMap<K, V> for HashMap<K, V, H> {
832+
impl<K: TotalEq + Hash<S>, V, S, H: Hasher<S>> MutableMap<K, V> for HashMap<K, V, H> {
836833
fn find_mut<'a>(&'a mut self, k: &K) -> Option<&'a mut V> {
837834
match self.search(k) {
838835
None => None,
@@ -969,7 +966,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> MutableMap<K, V> for HashMap<K, V, H>
969966
}
970967
}
971968

972-
impl<K: Hash + Eq, V> HashMap<K, V, sip::SipHasher> {
969+
impl<K: Hash + TotalEq, V> HashMap<K, V, sip::SipHasher> {
973970
/// Create an empty HashMap.
974971
pub fn new() -> HashMap<K, V, sip::SipHasher> {
975972
HashMap::with_capacity(INITIAL_CAPACITY)
@@ -984,7 +981,7 @@ impl<K: Hash + Eq, V> HashMap<K, V, sip::SipHasher> {
984981
}
985982
}
986983

987-
impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
984+
impl<K: TotalEq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
988985
pub fn with_hasher(hasher: H) -> HashMap<K, V, H> {
989986
HashMap::with_capacity_and_hasher(INITIAL_CAPACITY, hasher)
990987
}
@@ -1296,7 +1293,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
12961293
}
12971294
}
12981295

1299-
impl<K: Eq + Hash<S>, V: Clone, S, H: Hasher<S>> HashMap<K, V, H> {
1296+
impl<K: TotalEq + Hash<S>, V: Clone, S, H: Hasher<S>> HashMap<K, V, H> {
13001297
/// Like `find`, but returns a copy of the value.
13011298
pub fn find_copy(&self, k: &K) -> Option<V> {
13021299
self.find(k).map(|v| (*v).clone())
@@ -1308,7 +1305,7 @@ impl<K: Eq + Hash<S>, V: Clone, S, H: Hasher<S>> HashMap<K, V, H> {
13081305
}
13091306
}
13101307

1311-
impl<K: Eq + Hash<S>, V: Eq, S, H: Hasher<S>> Eq for HashMap<K, V, H> {
1308+
impl<K: TotalEq + Hash<S>, V: Eq, S, H: Hasher<S>> Eq for HashMap<K, V, H> {
13121309
fn eq(&self, other: &HashMap<K, V, H>) -> bool {
13131310
if self.len() != other.len() { return false; }
13141311

@@ -1321,7 +1318,7 @@ impl<K: Eq + Hash<S>, V: Eq, S, H: Hasher<S>> Eq for HashMap<K, V, H> {
13211318
}
13221319
}
13231320

1324-
impl<K: Eq + Hash<S> + Show, V: Show, S, H: Hasher<S>> Show for HashMap<K, V, H> {
1321+
impl<K: TotalEq + Hash<S> + Show, V: Show, S, H: Hasher<S>> Show for HashMap<K, V, H> {
13251322
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
13261323
try!(write!(f.buf, r"\{"));
13271324

@@ -1334,7 +1331,7 @@ impl<K: Eq + Hash<S> + Show, V: Show, S, H: Hasher<S>> Show for HashMap<K, V, H>
13341331
}
13351332
}
13361333

1337-
impl<K: Eq + Hash<S>, V, S, H: Hasher<S> + Default> Default for HashMap<K, V, H> {
1334+
impl<K: TotalEq + Hash<S>, V, S, H: Hasher<S> + Default> Default for HashMap<K, V, H> {
13381335
fn default() -> HashMap<K, V, H> {
13391336
HashMap::with_capacity_and_hasher(INITIAL_CAPACITY, Default::default())
13401337
}
@@ -1358,7 +1355,7 @@ pub type Keys<'a, K, V> =
13581355
pub type Values<'a, K, V> =
13591356
iter::Map<'static, (&'a K, &'a V), &'a V, Entries<'a, K, V>>;
13601357

1361-
impl<K: Eq + Hash<S>, V, S, H: Hasher<S> + Default> FromIterator<(K, V)> for HashMap<K, V, H> {
1358+
impl<K: TotalEq + Hash<S>, V, S, H: Hasher<S> + Default> FromIterator<(K, V)> for HashMap<K, V, H> {
13621359
fn from_iterator<T: Iterator<(K, V)>>(iter: &mut T) -> HashMap<K, V, H> {
13631360
let (lower, _) = iter.size_hint();
13641361
let mut map = HashMap::with_capacity_and_hasher(lower, Default::default());
@@ -1367,7 +1364,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S> + Default> FromIterator<(K, V)> for Has
13671364
}
13681365
}
13691366

1370-
impl<K: Eq + Hash<S>, V, S, H: Hasher<S> + Default> Extendable<(K, V)> for HashMap<K, V, H> {
1367+
impl<K: TotalEq + Hash<S>, V, S, H: Hasher<S> + Default> Extendable<(K, V)> for HashMap<K, V, H> {
13711368
fn extend<T: Iterator<(K, V)>>(&mut self, iter: &mut T) {
13721369
for (k, v) in *iter {
13731370
self.insert(k, v);
@@ -1391,7 +1388,7 @@ pub struct HashSet<T, H = sip::SipHasher> {
13911388
priv map: HashMap<T, (), H>
13921389
}
13931390

1394-
impl<T: Eq + Hash<S>, S, H: Hasher<S>> Eq for HashSet<T, H> {
1391+
impl<T: TotalEq + Hash<S>, S, H: Hasher<S>> Eq for HashSet<T, H> {
13951392
// FIXME #11998: Since the value is a (), and `find` returns a Some(&()),
13961393
// we trigger #11998 when matching on it. I've fallen back to manual
13971394
// iteration until this is fixed.
@@ -1402,17 +1399,17 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> Eq for HashSet<T, H> {
14021399
}
14031400
}
14041401

1405-
impl<T: Eq + Hash<S>, S, H: Hasher<S>> Container for HashSet<T, H> {
1402+
impl<T: TotalEq + Hash<S>, S, H: Hasher<S>> Container for HashSet<T, H> {
14061403
/// Return the number of elements in the set
14071404
fn len(&self) -> uint { self.map.len() }
14081405
}
14091406

1410-
impl<T: Eq + Hash<S>, S, H: Hasher<S>> Mutable for HashSet<T, H> {
1407+
impl<T: TotalEq + Hash<S>, S, H: Hasher<S>> Mutable for HashSet<T, H> {
14111408
/// Clear the set, removing all values.
14121409
fn clear(&mut self) { self.map.clear() }
14131410
}
14141411

1415-
impl<T: Eq + Hash<S>, S, H: Hasher<S>> Set<T> for HashSet<T, H> {
1412+
impl<T: TotalEq + Hash<S>, S, H: Hasher<S>> Set<T> for HashSet<T, H> {
14161413
/// Return true if the set contains a value
14171414
fn contains(&self, value: &T) -> bool { self.map.search(value).is_some() }
14181415

@@ -1433,7 +1430,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> Set<T> for HashSet<T, H> {
14331430
}
14341431
}
14351432

1436-
impl<T: Eq + Hash<S>, S, H: Hasher<S>> MutableSet<T> for HashSet<T, H> {
1433+
impl<T: TotalEq + Hash<S>, S, H: Hasher<S>> MutableSet<T> for HashSet<T, H> {
14371434
/// Add a value to the set. Return true if the value was not already
14381435
/// present in the set.
14391436
fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
@@ -1443,7 +1440,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> MutableSet<T> for HashSet<T, H> {
14431440
fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
14441441
}
14451442

1446-
impl<T: Hash + Eq> HashSet<T, sip::SipHasher> {
1443+
impl<T: Hash + TotalEq> HashSet<T, sip::SipHasher> {
14471444
/// Create an empty HashSet
14481445
pub fn new() -> HashSet<T, sip::SipHasher> {
14491446
HashSet::with_capacity(INITIAL_CAPACITY)
@@ -1456,7 +1453,7 @@ impl<T: Hash + Eq> HashSet<T, sip::SipHasher> {
14561453
}
14571454
}
14581455

1459-
impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
1456+
impl<T: TotalEq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
14601457
pub fn with_hasher(hasher: H) -> HashSet<T, H> {
14611458
HashSet::with_capacity_and_hasher(INITIAL_CAPACITY, hasher)
14621459
}
@@ -1529,7 +1526,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
15291526

15301527
}
15311528

1532-
impl<T: Eq + Hash<S> + fmt::Show, S, H: Hasher<S>> fmt::Show for HashSet<T, H> {
1529+
impl<T: TotalEq + Hash<S> + fmt::Show, S, H: Hasher<S>> fmt::Show for HashSet<T, H> {
15331530
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15341531
try!(write!(f.buf, r"\{"));
15351532

@@ -1542,7 +1539,7 @@ impl<T: Eq + Hash<S> + fmt::Show, S, H: Hasher<S>> fmt::Show for HashSet<T, H> {
15421539
}
15431540
}
15441541

1545-
impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> FromIterator<T> for HashSet<T, H> {
1542+
impl<T: TotalEq + Hash<S>, S, H: Hasher<S> + Default> FromIterator<T> for HashSet<T, H> {
15461543
fn from_iterator<I: Iterator<T>>(iter: &mut I) -> HashSet<T, H> {
15471544
let (lower, _) = iter.size_hint();
15481545
let mut set = HashSet::with_capacity_and_hasher(lower, Default::default());
@@ -1551,15 +1548,15 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> FromIterator<T> for HashSet<T,
15511548
}
15521549
}
15531550

1554-
impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> Extendable<T> for HashSet<T, H> {
1551+
impl<T: TotalEq + Hash<S>, S, H: Hasher<S> + Default> Extendable<T> for HashSet<T, H> {
15551552
fn extend<I: Iterator<T>>(&mut self, iter: &mut I) {
15561553
for k in *iter {
15571554
self.insert(k);
15581555
}
15591556
}
15601557
}
15611558

1562-
impl<T: Eq + Hash> Default for HashSet<T, sip::SipHasher> {
1559+
impl<T: TotalEq + Hash> Default for HashSet<T, sip::SipHasher> {
15631560
fn default() -> HashSet<T> { HashSet::new() }
15641561
}
15651562

@@ -1601,7 +1598,7 @@ mod test_map {
16011598

16021599
local_data_key!(drop_vector: vec::Vec<int>)
16031600

1604-
#[deriving(Hash, Eq)]
1601+
#[deriving(Hash, Eq, TotalEq)]
16051602
struct Dropable {
16061603
k: int
16071604
}

src/libcollections/lru_cache.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ impl<K: Eq> Eq for KeyRef<K> {
7474
}
7575
}
7676

77+
impl<K: TotalEq> TotalEq for KeyRef<K> {}
78+
7779
impl<K, V> LruEntry<K, V> {
7880
fn new() -> LruEntry<K, V> {
7981
LruEntry {
@@ -94,7 +96,7 @@ impl<K, V> LruEntry<K, V> {
9496
}
9597
}
9698

97-
impl<K: Hash + Eq, V> LruCache<K, V> {
99+
impl<K: Hash + TotalEq, V> LruCache<K, V> {
98100
/// Create an LRU Cache that holds at most `capacity` items.
99101
pub fn new(capacity: uint) -> LruCache<K, V> {
100102
let cache = LruCache {
@@ -218,7 +220,7 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
218220
}
219221
}
220222

221-
impl<A: fmt::Show + Hash + Eq, B: fmt::Show> fmt::Show for LruCache<A, B> {
223+
impl<A: fmt::Show + Hash + TotalEq, B: fmt::Show> fmt::Show for LruCache<A, B> {
222224
/// Return a string that lists the key-value pairs from most-recently
223225
/// used to least-recently used.
224226
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -247,14 +249,14 @@ impl<A: fmt::Show + Hash + Eq, B: fmt::Show> fmt::Show for LruCache<A, B> {
247249
}
248250
}
249251

250-
impl<K: Hash + Eq, V> Container for LruCache<K, V> {
252+
impl<K: Hash + TotalEq, V> Container for LruCache<K, V> {
251253
/// Return the number of key-value pairs in the cache.
252254
fn len(&self) -> uint {
253255
self.map.len()
254256
}
255257
}
256258

257-
impl<K: Hash + Eq, V> Mutable for LruCache<K, V> {
259+
impl<K: Hash + TotalEq, V> Mutable for LruCache<K, V> {
258260
/// Clear the cache of all key-value pairs.
259261
fn clear(&mut self) {
260262
self.map.clear();

src/librustc/middle/borrowck/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub struct BorrowStats {
205205
//
206206
// Note that there is no entry with derefs:3---the type of that expression
207207
// is T, which is not a box.
208-
#[deriving(Eq, Hash)]
208+
#[deriving(Eq, TotalEq, Hash)]
209209
pub struct root_map_key {
210210
id: ast::NodeId,
211211
derefs: uint
@@ -243,13 +243,13 @@ pub enum LoanCause {
243243
RefBinding,
244244
}
245245

246-
#[deriving(Eq, Hash)]
246+
#[deriving(Eq, TotalEq, Hash)]
247247
pub enum LoanPath {
248248
LpVar(ast::NodeId), // `x` in doc.rs
249249
LpExtend(@LoanPath, mc::MutabilityCategory, LoanPathElem)
250250
}
251251

252-
#[deriving(Eq, Hash)]
252+
#[deriving(Eq, TotalEq, Hash)]
253253
pub enum LoanPathElem {
254254
LpDeref(mc::PointerKind), // `*LV` in doc.rs
255255
LpInterior(mc::InteriorKind) // `LV.f` in doc.rs

src/librustc/middle/mem_categorization.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub struct CopiedUpvar {
9595
}
9696

9797
// different kinds of pointers:
98-
#[deriving(Eq, Hash)]
98+
#[deriving(Eq, TotalEq, Hash)]
9999
pub enum PointerKind {
100100
OwnedPtr,
101101
GcPtr,
@@ -105,26 +105,26 @@ pub enum PointerKind {
105105

106106
// We use the term "interior" to mean "something reachable from the
107107
// base without a pointer dereference", e.g. a field
108-
#[deriving(Eq, Hash)]
108+
#[deriving(Eq, TotalEq, Hash)]
109109
pub enum InteriorKind {
110110
InteriorField(FieldName),
111111
InteriorElement(ElementKind),
112112
}
113113

114-
#[deriving(Eq, Hash)]
114+
#[deriving(Eq, TotalEq, Hash)]
115115
pub enum FieldName {
116116
NamedField(ast::Name),
117117
PositionalField(uint)
118118
}
119119

120-
#[deriving(Eq, Hash)]
120+
#[deriving(Eq, TotalEq, Hash)]
121121
pub enum ElementKind {
122122
VecElement,
123123
StrElement,
124124
OtherElement,
125125
}
126126

127-
#[deriving(Eq, Hash, Show)]
127+
#[deriving(Eq, TotalEq, Hash, Show)]
128128
pub enum MutabilityCategory {
129129
McImmutable, // Immutable.
130130
McDeclared, // Directly declared as mutable.

src/librustc/middle/resolve.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ enum PatternBindingMode {
107107
ArgumentIrrefutableMode,
108108
}
109109

110-
#[deriving(Eq, Hash)]
110+
#[deriving(Eq, TotalEq, Hash)]
111111
enum Namespace {
112112
TypeNS,
113113
ValueNS

src/librustc/middle/trans/common.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ pub fn is_null(val: ValueRef) -> bool {
710710
}
711711

712712
// Used to identify cached monomorphized functions and vtables
713-
#[deriving(Eq, Hash)]
713+
#[deriving(Eq, TotalEq, Hash)]
714714
pub enum mono_param_id {
715715
mono_precise(ty::t, Option<@Vec<mono_id> >),
716716
mono_any,
@@ -720,7 +720,7 @@ pub enum mono_param_id {
720720
datum::RvalueMode),
721721
}
722722

723-
#[deriving(Eq, Hash)]
723+
#[deriving(Eq, TotalEq, Hash)]
724724
pub enum MonoDataClass {
725725
MonoBits, // Anything not treated differently from arbitrary integer data
726726
MonoNonNull, // Non-null pointers (used for optional-pointer optimization)
@@ -742,7 +742,7 @@ pub fn mono_data_classify(t: ty::t) -> MonoDataClass {
742742
}
743743
}
744744

745-
#[deriving(Eq, Hash)]
745+
#[deriving(Eq, TotalEq, Hash)]
746746
pub struct mono_id_ {
747747
def: ast::DefId,
748748
params: Vec<mono_param_id> }

0 commit comments

Comments
 (0)