Skip to content

Commit 66c297d

Browse files
committed
auto merge of #19800 : sfackler/rust/core-hash, r=alexcrichton
r? @alexcrichton
2 parents 2c533ef + 24a8ef6 commit 66c297d

File tree

18 files changed

+433
-172
lines changed

18 files changed

+433
-172
lines changed

src/liballoc/boxed.rs

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use core::clone::Clone;
1515
use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering};
1616
use core::default::Default;
1717
use core::fmt;
18+
use core::hash::{mod, Hash};
1819
use core::kinds::Sized;
1920
use core::mem;
2021
use core::option::Option;
@@ -93,6 +94,14 @@ impl<Sized? T: Ord> Ord for Box<T> {
9394
}
9495
impl<Sized? T: Eq> Eq for Box<T> {}
9596

97+
impl<S: hash::Writer, Sized? T: Hash<S>> Hash<S> for Box<T> {
98+
#[inline]
99+
fn hash(&self, state: &mut S) {
100+
(**self).hash(state);
101+
}
102+
}
103+
104+
96105
/// Extension methods for an owning `Any` trait object.
97106
#[unstable = "post-DST and coherence changes, this will not be a trait but \
98107
rather a direct `impl` on `Box<Any>`"]

src/liballoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
html_root_url = "http://doc.rust-lang.org/nightly/")]
6565

6666
#![no_std]
67-
#![feature(lang_items, phase, unsafe_destructor)]
67+
#![feature(lang_items, phase, unsafe_destructor, default_type_params)]
6868

6969
#[phase(plugin, link)]
7070
extern crate core;

src/liballoc/rc.rs

+9
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ use core::clone::Clone;
147147
use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering};
148148
use core::default::Default;
149149
use core::fmt;
150+
use core::hash::{mod, Hash};
150151
use core::kinds::marker;
151152
use core::mem::{transmute, min_align_of, size_of, forget};
152153
use core::ops::{Deref, Drop};
@@ -594,6 +595,14 @@ impl<T: Ord> Ord for Rc<T> {
594595
fn cmp(&self, other: &Rc<T>) -> Ordering { (**self).cmp(&**other) }
595596
}
596597

598+
// FIXME (#18248) Make `T` `Sized?`
599+
impl<S: hash::Writer, T: Hash<S>> Hash<S> for Rc<T> {
600+
#[inline]
601+
fn hash(&self, state: &mut S) {
602+
(**self).hash(state);
603+
}
604+
}
605+
597606
#[experimental = "Show is experimental."]
598607
impl<T: fmt::Show> fmt::Show for Rc<T> {
599608
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

src/libcollections/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ pub mod slice;
6767
pub mod str;
6868
pub mod string;
6969
pub mod vec;
70-
pub mod hash;
7170
pub mod vec_map;
7271

7372
pub mod bitv {
@@ -116,5 +115,5 @@ mod std {
116115
pub use core::clone; // deriving(Clone)
117116
pub use core::cmp; // deriving(Eq, Ord, etc.)
118117
pub use core::kinds; // deriving(Copy)
119-
pub use hash; // deriving(Hash)
118+
pub use core::hash; // deriving(Hash)
120119
}

src/libcollections/str.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ use self::DecompositionType::*;
6060
use core::borrow::{BorrowFrom, Cow, ToOwned};
6161
use core::default::Default;
6262
use core::fmt;
63+
use core::hash;
6364
use core::cmp;
6465
use core::iter::AdditiveIterator;
6566

66-
use hash;
6767
use ring_buf::RingBuf;
6868
use string::String;
6969
use unicode;

src/libcollections/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ use core::prelude::*;
1717
use core::borrow::{Cow, IntoCow};
1818
use core::default::Default;
1919
use core::fmt;
20+
use core::hash;
2021
use core::mem;
2122
use core::ptr;
2223
use core::ops;
2324
// FIXME: ICE's abound if you import the `Slice` type while importing `Slice` trait
2425
use core::raw::Slice as RawSlice;
2526

26-
use hash;
2727
use slice::CloneSliceExt;
2828
use str;
2929
use str::{CharRange, CowString, FromStr, StrAllocating, Owned};

src/libcollections/vec.rs

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use core::borrow::{Cow, IntoCow};
2020
use core::cmp::max;
2121
use core::default::Default;
2222
use core::fmt;
23+
use core::hash::{mod, Hash};
2324
use core::kinds::marker::{ContravariantLifetime, InvariantType};
2425
use core::kinds::Sized;
2526
use core::mem;
@@ -619,6 +620,13 @@ impl<T: Ord> Ord for Vec<T> {
619620
}
620621
}
621622

623+
impl<S: hash::Writer, T: Hash<S>> Hash<S> for Vec<T> {
624+
#[inline]
625+
fn hash(&self, state: &mut S) {
626+
self.as_slice().hash(state);
627+
}
628+
}
629+
622630
// FIXME: #13996: need a way to mark the return value as `noalias`
623631
#[inline(never)]
624632
unsafe fn alloc_or_realloc<T>(ptr: *mut T, old_size: uint, size: uint) -> *mut T {

src/libcollections/vec_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ use core::prelude::*;
1717

1818
use core::default::Default;
1919
use core::fmt;
20+
use core::hash::{Hash, Writer};
2021
use core::iter;
2122
use core::iter::{Enumerate, FilterMap};
2223
use core::mem::replace;
2324
use core::ops::FnOnce;
2425

25-
use hash::{Hash, Writer};
2626
use {vec, slice};
2727
use vec::Vec;
2828

@@ -642,7 +642,7 @@ pub type MoveItems<V> = FilterMap<
642642
mod test_map {
643643
use std::prelude::*;
644644
use vec::Vec;
645-
use hash::hash;
645+
use core::hash::hash;
646646

647647
use super::VecMap;
648648

src/libcollections/hash/mod.rs renamed to src/libcore/hash/mod.rs

+7-157
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,12 @@
6161
6262
#![allow(unused_must_use)]
6363

64-
use core::prelude::*;
64+
use prelude::*;
6565

66-
use alloc::boxed::Box;
67-
use alloc::rc::Rc;
68-
use core::borrow::{Cow, ToOwned};
69-
use core::intrinsics::TypeId;
70-
use core::mem;
71-
use core::num::Int;
72-
73-
use vec::Vec;
66+
use borrow::{Cow, ToOwned};
67+
use intrinsics::TypeId;
68+
use mem;
69+
use num::Int;
7470

7571
/// Reexport the `sip::hash` function as our default hasher.
7672
pub use self::sip::hash as hash;
@@ -92,6 +88,7 @@ pub trait Hasher<S> {
9288
fn hash<Sized? T: Hash<S>>(&self, value: &T) -> u64;
9389
}
9490

91+
#[allow(missing_docs)]
9592
pub trait Writer {
9693
fn write(&mut self, bytes: &[u8]);
9794
}
@@ -103,7 +100,7 @@ macro_rules! impl_hash {
103100
impl<S: Writer> Hash<S> for $ty {
104101
#[inline]
105102
fn hash(&self, state: &mut S) {
106-
let a: [u8, ..::core::$ty::BYTES] = unsafe {
103+
let a: [u8, ..::$ty::BYTES] = unsafe {
107104
mem::transmute((*self as $uty).to_le() as $ty)
108105
};
109106
state.write(a.as_slice())
@@ -197,13 +194,6 @@ impl<S: Writer, T: Hash<S>> Hash<S> for [T] {
197194
}
198195

199196

200-
impl<S: Writer, T: Hash<S>> Hash<S> for Vec<T> {
201-
#[inline]
202-
fn hash(&self, state: &mut S) {
203-
self.as_slice().hash(state);
204-
}
205-
}
206-
207197
impl<'a, S: Writer, Sized? T: Hash<S>> Hash<S> for &'a T {
208198
#[inline]
209199
fn hash(&self, state: &mut S) {
@@ -218,36 +208,6 @@ impl<'a, S: Writer, Sized? T: Hash<S>> Hash<S> for &'a mut T {
218208
}
219209
}
220210

221-
impl<S: Writer, Sized? T: Hash<S>> Hash<S> for Box<T> {
222-
#[inline]
223-
fn hash(&self, state: &mut S) {
224-
(**self).hash(state);
225-
}
226-
}
227-
228-
// FIXME (#18248) Make `T` `Sized?`
229-
impl<S: Writer, T: Hash<S>> Hash<S> for Rc<T> {
230-
#[inline]
231-
fn hash(&self, state: &mut S) {
232-
(**self).hash(state);
233-
}
234-
}
235-
236-
impl<S: Writer, T: Hash<S>> Hash<S> for Option<T> {
237-
#[inline]
238-
fn hash(&self, state: &mut S) {
239-
match *self {
240-
Some(ref x) => {
241-
0u8.hash(state);
242-
x.hash(state);
243-
}
244-
None => {
245-
1u8.hash(state);
246-
}
247-
}
248-
}
249-
}
250-
251211
impl<S: Writer, T> Hash<S> for *const T {
252212
#[inline]
253213
fn hash(&self, state: &mut S) {
@@ -273,119 +233,9 @@ impl<S: Writer> Hash<S> for TypeId {
273233
}
274234
}
275235

276-
impl<S: Writer, T: Hash<S>, U: Hash<S>> Hash<S> for Result<T, U> {
277-
#[inline]
278-
fn hash(&self, state: &mut S) {
279-
match *self {
280-
Ok(ref t) => { 1u.hash(state); t.hash(state); }
281-
Err(ref t) => { 2u.hash(state); t.hash(state); }
282-
}
283-
}
284-
}
285-
286236
impl<'a, T, Sized? B, S> Hash<S> for Cow<'a, T, B> where B: Hash<S> + ToOwned<T> {
287237
#[inline]
288238
fn hash(&self, state: &mut S) {
289239
Hash::hash(&**self, state)
290240
}
291241
}
292-
293-
//////////////////////////////////////////////////////////////////////////////
294-
295-
#[cfg(test)]
296-
mod tests {
297-
use core::kinds::Sized;
298-
use std::mem;
299-
300-
use slice::SliceExt;
301-
use super::{Hash, Hasher, Writer};
302-
303-
struct MyWriterHasher;
304-
305-
impl Hasher<MyWriter> for MyWriterHasher {
306-
fn hash<Sized? T: Hash<MyWriter>>(&self, value: &T) -> u64 {
307-
let mut state = MyWriter { hash: 0 };
308-
value.hash(&mut state);
309-
state.hash
310-
}
311-
}
312-
313-
struct MyWriter {
314-
hash: u64,
315-
}
316-
317-
impl Writer for MyWriter {
318-
// Most things we'll just add up the bytes.
319-
fn write(&mut self, buf: &[u8]) {
320-
for byte in buf.iter() {
321-
self.hash += *byte as u64;
322-
}
323-
}
324-
}
325-
326-
#[test]
327-
fn test_writer_hasher() {
328-
use alloc::boxed::Box;
329-
330-
let hasher = MyWriterHasher;
331-
332-
assert_eq!(hasher.hash(&()), 0);
333-
334-
assert_eq!(hasher.hash(&5u8), 5);
335-
assert_eq!(hasher.hash(&5u16), 5);
336-
assert_eq!(hasher.hash(&5u32), 5);
337-
assert_eq!(hasher.hash(&5u64), 5);
338-
assert_eq!(hasher.hash(&5u), 5);
339-
340-
assert_eq!(hasher.hash(&5i8), 5);
341-
assert_eq!(hasher.hash(&5i16), 5);
342-
assert_eq!(hasher.hash(&5i32), 5);
343-
assert_eq!(hasher.hash(&5i64), 5);
344-
assert_eq!(hasher.hash(&5i), 5);
345-
346-
assert_eq!(hasher.hash(&false), 0);
347-
assert_eq!(hasher.hash(&true), 1);
348-
349-
assert_eq!(hasher.hash(&'a'), 97);
350-
351-
let s: &str = "a";
352-
assert_eq!(hasher.hash(& s), 97 + 0xFF);
353-
// FIXME (#18283) Enable test
354-
//let s: Box<str> = box "a";
355-
//assert_eq!(hasher.hash(& s), 97 + 0xFF);
356-
let cs: &[u8] = &[1u8, 2u8, 3u8];
357-
assert_eq!(hasher.hash(& cs), 9);
358-
let cs: Box<[u8]> = box [1u8, 2u8, 3u8];
359-
assert_eq!(hasher.hash(& cs), 9);
360-
361-
// FIXME (#18248) Add tests for hashing Rc<str> and Rc<[T]>
362-
363-
unsafe {
364-
let ptr: *const int = mem::transmute(5i);
365-
assert_eq!(hasher.hash(&ptr), 5);
366-
}
367-
368-
unsafe {
369-
let ptr: *mut int = mem::transmute(5i);
370-
assert_eq!(hasher.hash(&ptr), 5);
371-
}
372-
}
373-
374-
struct Custom {
375-
hash: u64
376-
}
377-
378-
impl Hash<u64> for Custom {
379-
fn hash(&self, state: &mut u64) {
380-
*state = self.hash;
381-
}
382-
}
383-
384-
#[test]
385-
fn test_custom_state() {
386-
let custom = Custom { hash: 5 };
387-
let mut state = 0;
388-
custom.hash(&mut state);
389-
assert_eq!(state, 5);
390-
}
391-
}

src/libcollections/hash/sip.rs renamed to src/libcore/hash/sip.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
//! As such, all cryptographic uses of this implementation are strongly
2525
//! discouraged.
2626
27-
use core::prelude::*;
28-
29-
use core::default::Default;
27+
use prelude::*;
28+
use default::Default;
3029

3130
use super::{Hash, Hasher, Writer};
3231

src/libcore/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ pub mod simd;
121121
pub mod slice;
122122
pub mod str;
123123
pub mod tuple;
124+
pub mod hash;
124125
// FIXME #15320: primitive documentation needs top-level modules, this
125126
// should be `core::tuple::unit`.
126127
#[path = "tuple/unit.rs"]
@@ -142,4 +143,5 @@ mod std {
142143
pub use kinds;
143144
pub use option;
144145
pub use fmt;
146+
pub use hash;
145147
}

src/libcore/option.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ use ops::{Deref, FnOnce};
164164
// which basically means it must be `Option`.
165165

166166
/// The `Option` type.
167-
#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord, Show)]
167+
#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord, Show, Hash)]
168168
#[stable]
169169
pub enum Option<T> {
170170
/// No value

src/libcore/result.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ use ops::{FnMut, FnOnce};
244244
/// `Result` is a type that represents either success (`Ok`) or failure (`Err`).
245245
///
246246
/// See the [`std::result`](index.html) module documentation for details.
247-
#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord, Show)]
247+
#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord, Show, Hash)]
248248
#[must_use]
249249
#[stable]
250250
pub enum Result<T, E> {

0 commit comments

Comments
 (0)