Skip to content

Commit 25d6836

Browse files
committed
auto merge of #12522 : erickt/rust/hash, r=alexcrichton
This patch series does a couple things: * replaces manual `Hash` implementations with `#[deriving(Hash)]` * adds `Hash` back to `std::prelude` * minor cleanup of whitespace and variable names.
2 parents d222f03 + 6335a76 commit 25d6836

File tree

14 files changed

+68
-83
lines changed

14 files changed

+68
-83
lines changed

src/libcollections/hashmap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn resize_at(capacity: uint) -> uint {
9898
(capacity * 3) / 4
9999
}
100100

101-
impl<K:Hash + Eq,V> HashMap<K, V> {
101+
impl<K:Hash + Eq, V> HashMap<K, V> {
102102
#[inline]
103103
fn to_bucket(&self, h: uint) -> uint {
104104
// A good hash function with entropy spread over all of the

src/librustc/middle/trans/common.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ pub fn is_null(val: ValueRef) -> bool {
722722
}
723723

724724
// Used to identify cached monomorphized functions and vtables
725-
#[deriving(Eq,Hash)]
725+
#[deriving(Eq, Hash)]
726726
pub enum mono_param_id {
727727
mono_precise(ty::t, Option<@~[mono_id]>),
728728
mono_any,
@@ -732,7 +732,7 @@ pub enum mono_param_id {
732732
datum::RvalueMode),
733733
}
734734

735-
#[deriving(Eq,Hash)]
735+
#[deriving(Eq, Hash)]
736736
pub enum MonoDataClass {
737737
MonoBits, // Anything not treated differently from arbitrary integer data
738738
MonoNonNull, // Non-null pointers (used for optional-pointer optimization)
@@ -754,8 +754,7 @@ pub fn mono_data_classify(t: ty::t) -> MonoDataClass {
754754
}
755755
}
756756

757-
758-
#[deriving(Eq,Hash)]
757+
#[deriving(Eq, Hash)]
759758
pub struct mono_id_ {
760759
def: ast::DefId,
761760
params: ~[mono_param_id]

src/librustc/middle/ty.rs

+28-30
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub struct field_ty {
150150

151151
// Contains information needed to resolve types and (in the future) look up
152152
// the types of AST nodes.
153-
#[deriving(Eq,Hash)]
153+
#[deriving(Eq, Hash)]
154154
pub struct creader_cache_key {
155155
cnum: CrateNum,
156156
pos: uint,
@@ -4926,13 +4926,11 @@ pub fn trait_method_of_method(tcx: ctxt,
49264926
/// Creates a hash of the type `t` which will be the same no matter what crate
49274927
/// context it's calculated within. This is used by the `type_id` intrinsic.
49284928
pub fn hash_crate_independent(tcx: ctxt, t: t, local_hash: ~str) -> u64 {
4929-
use std::hash::{sip, Hash};
4930-
4931-
let mut hash = sip::SipState::new(0, 0);
4932-
macro_rules! byte( ($b:expr) => { ($b as u8).hash(&mut hash) } );
4933-
macro_rules! hash( ($e:expr) => { $e.hash(&mut hash) } );
4929+
let mut state = sip::SipState::new(0, 0);
4930+
macro_rules! byte( ($b:expr) => { ($b as u8).hash(&mut state) } );
4931+
macro_rules! hash( ($e:expr) => { $e.hash(&mut state) } );
49344932

4935-
let region = |_hash: &mut sip::SipState, r: Region| {
4933+
let region = |_state: &mut sip::SipState, r: Region| {
49364934
match r {
49374935
ReStatic => {}
49384936

@@ -4946,27 +4944,27 @@ pub fn hash_crate_independent(tcx: ctxt, t: t, local_hash: ~str) -> u64 {
49464944
}
49474945
}
49484946
};
4949-
let vstore = |hash: &mut sip::SipState, v: vstore| {
4947+
let vstore = |state: &mut sip::SipState, v: vstore| {
49504948
match v {
4951-
vstore_fixed(_) => 0u8.hash(hash),
4952-
vstore_uniq => 1u8.hash(hash),
4949+
vstore_fixed(_) => 0u8.hash(state),
4950+
vstore_uniq => 1u8.hash(state),
49534951
vstore_slice(r) => {
4954-
2u8.hash(hash);
4955-
region(hash, r);
4952+
2u8.hash(state);
4953+
region(state, r);
49564954
}
49574955
}
49584956
};
4959-
let did = |hash: &mut sip::SipState, did: DefId| {
4957+
let did = |state: &mut sip::SipState, did: DefId| {
49604958
let h = if ast_util::is_local(did) {
49614959
local_hash.clone()
49624960
} else {
49634961
tcx.sess.cstore.get_crate_hash(did.krate)
49644962
};
4965-
h.as_bytes().hash(hash);
4966-
did.node.hash(hash);
4963+
h.as_bytes().hash(state);
4964+
did.node.hash(state);
49674965
};
4968-
let mt = |hash: &mut sip::SipState, mt: mt| {
4969-
mt.mutbl.hash(hash);
4966+
let mt = |state: &mut sip::SipState, mt: mt| {
4967+
mt.mutbl.hash(state);
49704968
};
49714969
ty::walk_ty(t, |t| {
49724970
match ty::get(t).sty {
@@ -5002,17 +5000,17 @@ pub fn hash_crate_independent(tcx: ctxt, t: t, local_hash: ~str) -> u64 {
50025000
}
50035001
ty_vec(m, v) => {
50045002
byte!(11);
5005-
mt(&mut hash, m);
5006-
vstore(&mut hash, v);
5003+
mt(&mut state, m);
5004+
vstore(&mut state, v);
50075005
}
50085006
ty_ptr(m) => {
50095007
byte!(12);
5010-
mt(&mut hash, m);
5008+
mt(&mut state, m);
50115009
}
50125010
ty_rptr(r, m) => {
50135011
byte!(13);
5014-
region(&mut hash, r);
5015-
mt(&mut hash, m);
5012+
region(&mut state, r);
5013+
mt(&mut state, m);
50165014
}
50175015
ty_bare_fn(ref b) => {
50185016
byte!(14);
@@ -5025,24 +5023,24 @@ pub fn hash_crate_independent(tcx: ctxt, t: t, local_hash: ~str) -> u64 {
50255023
hash!(c.sigil);
50265024
hash!(c.onceness);
50275025
hash!(c.bounds);
5028-
region(&mut hash, c.region);
5026+
region(&mut state, c.region);
50295027
}
50305028
ty_trait(d, _, store, m, bounds) => {
50315029
byte!(17);
5032-
did(&mut hash, d);
5030+
did(&mut state, d);
50335031
match store {
50345032
UniqTraitStore => byte!(0),
50355033
RegionTraitStore(r) => {
50365034
byte!(1)
5037-
region(&mut hash, r);
5035+
region(&mut state, r);
50385036
}
50395037
}
50405038
hash!(m);
50415039
hash!(bounds);
50425040
}
50435041
ty_struct(d, _) => {
50445042
byte!(18);
5045-
did(&mut hash, d);
5043+
did(&mut state, d);
50465044
}
50475045
ty_tup(ref inner) => {
50485046
byte!(19);
@@ -5051,22 +5049,22 @@ pub fn hash_crate_independent(tcx: ctxt, t: t, local_hash: ~str) -> u64 {
50515049
ty_param(p) => {
50525050
byte!(20);
50535051
hash!(p.idx);
5054-
did(&mut hash, p.def_id);
5052+
did(&mut state, p.def_id);
50555053
}
50565054
ty_self(d) => {
50575055
byte!(21);
5058-
did(&mut hash, d);
5056+
did(&mut state, d);
50595057
}
50605058
ty_infer(_) => unreachable!(),
50615059
ty_err => byte!(23),
50625060
ty_unboxed_vec(m) => {
50635061
byte!(24);
5064-
mt(&mut hash, m);
5062+
mt(&mut state, m);
50655063
}
50665064
}
50675065
});
50685066

5069-
hash.result()
5067+
state.result()
50705068
}
50715069

50725070
impl Variance {

src/libstd/ascii.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ use str::OwnedStr;
1818
use container::Container;
1919
use cast;
2020
use fmt;
21-
use hash::{Hash, sip};
2221
use iter::Iterator;
2322
use vec::{ImmutableVector, MutableVector, Vector};
2423
use option::{Option, Some, None};
2524

2625
/// Datatype to hold one ascii character. It wraps a `u8`, with the highest bit always zero.
27-
#[deriving(Clone, Eq, Ord, TotalOrd, TotalEq)]
26+
#[deriving(Clone, Eq, Ord, TotalOrd, TotalEq, Hash)]
2827
pub struct Ascii { priv chr: u8 }
2928

3029
impl Ascii {
@@ -306,13 +305,6 @@ impl IntoStr for ~[Ascii] {
306305
}
307306
}
308307

309-
impl Hash for Ascii {
310-
#[inline]
311-
fn hash(&self, s: &mut sip::SipState) {
312-
self.to_byte().hash(s)
313-
}
314-
}
315-
316308
/// Trait to convert to an owned byte array by consuming self
317309
pub trait IntoBytes {
318310
/// Converts to an owned byte array by consuming self

src/libstd/io/net/ip.rs

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ pub struct SocketAddr {
5555
port: Port,
5656
}
5757

58-
5958
impl fmt::Show for SocketAddr {
6059
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6160
match self.ip {

src/libstd/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ pub mod container;
151151
pub mod default;
152152
pub mod any;
153153

154-
155154
/* Common data structures */
156155

157156
pub mod option;

src/libsyntax/abi.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::hash::{Hash, sip};
1211
use std::fmt;
1312
use std::fmt::Show;
1413

1514
#[deriving(Eq)]
1615
pub enum Os { OsWin32, OsMacos, OsLinux, OsAndroid, OsFreebsd, }
1716

18-
#[deriving(Eq)]
17+
#[deriving(Eq, Hash)]
1918
pub enum Abi {
2019
// NB: This ordering MUST match the AbiDatas array below.
2120
// (This is ensured by the test indices_are_correct().)
@@ -267,12 +266,6 @@ impl AbiSet {
267266
}
268267
}
269268

270-
impl Hash for Abi {
271-
fn hash(&self, s: &mut sip::SipState) {
272-
self.index().hash(s)
273-
}
274-
}
275-
276269
impl fmt::Show for Abi {
277270
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
278271
self.data().name.fmt(f)

0 commit comments

Comments
 (0)