Skip to content

Commit f83e23a

Browse files
committed
std: Stabilize the hash module
This commit is an implementation of [RFC 823][rfc] which is another pass over the `std::hash` module for stabilization. The contents of the module were not entirely marked stable, but some portions which remained quite similar to the previous incarnation are now marked `#[stable]`. Specifically: [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0823-hash-simplification.md * `std::hash` is now stable (the name) * `Hash` is now stable * `Hash::hash` is now stable * `Hasher` is now stable * `SipHasher` is now stable * `SipHasher::new` and `new_with_keys` are now stable * `Hasher for SipHasher` is now stable * Many `Hash` implementations are now stable All other portions of the `hash` module remain `#[unstable]` as they are less commonly used and were recently redesigned. This commit is a breaking change due to the modifications to the `std::hash` API and more details can be found on the [RFC][rfc]. Closes #22467 [breaking-change]
1 parent dfc5c0f commit f83e23a

File tree

54 files changed

+4988
-356
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4988
-356
lines changed

Diff for: src/liballoc/arc.rs

+8
Original file line numberDiff line numberDiff line change
@@ -605,11 +605,19 @@ impl<T: Default + Sync + Send> Default for Arc<T> {
605605
fn default() -> Arc<T> { Arc::new(Default::default()) }
606606
}
607607

608+
#[cfg(stage0)]
608609
impl<H: Hasher, T: Hash<H>> Hash<H> for Arc<T> {
609610
fn hash(&self, state: &mut H) {
610611
(**self).hash(state)
611612
}
612613
}
614+
#[cfg(not(stage0))]
615+
#[stable(feature = "rust1", since = "1.0.0")]
616+
impl<T: Hash> Hash for Arc<T> {
617+
fn hash<H: Hasher>(&self, state: &mut H) {
618+
(**self).hash(state)
619+
}
620+
}
613621

614622
#[cfg(test)]
615623
mod tests {

Diff for: src/liballoc/boxed.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010

1111
//! A pointer type for heap allocation.
1212
//!
13-
//! `Box<T>`, casually referred to as a 'box', provides the simplest form of heap allocation in
14-
//! Rust. Boxes provide ownership for this allocation, and drop their contents when they go out of
15-
//! scope.
13+
//! `Box<T>`, casually referred to as a 'box', provides the simplest form of
14+
//! heap allocation in Rust. Boxes provide ownership for this allocation, and
15+
//! drop their contents when they go out of scope.
1616
//!
17-
//! Boxes are useful in two situations: recursive data structures, and occasionally when returning
18-
//! data. [The Pointer chapter of the Book](../../../book/pointers.html#best-practices-1) explains
19-
//! these cases in detail.
17+
//! Boxes are useful in two situations: recursive data structures, and
18+
//! occasionally when returning data. [The Pointer chapter of the
19+
//! Book](../../../book/pointers.html#best-practices-1) explains these cases in
20+
//! detail.
2021
//!
2122
//! # Examples
2223
//!
@@ -58,8 +59,8 @@ use core::ops::{Deref, DerefMut};
5859
use core::ptr::Unique;
5960
use core::raw::TraitObject;
6061

61-
/// A value that represents the heap. This is the default place that the `box` keyword allocates
62-
/// into when no place is supplied.
62+
/// A value that represents the heap. This is the default place that the `box`
63+
/// keyword allocates into when no place is supplied.
6364
///
6465
/// The following two examples are equivalent:
6566
///
@@ -219,12 +220,20 @@ impl<T: ?Sized + Ord> Ord for Box<T> {
219220
#[stable(feature = "rust1", since = "1.0.0")]
220221
impl<T: ?Sized + Eq> Eq for Box<T> {}
221222

223+
#[cfg(stage0)]
222224
impl<S: hash::Hasher, T: ?Sized + Hash<S>> Hash<S> for Box<T> {
223225
#[inline]
224226
fn hash(&self, state: &mut S) {
225227
(**self).hash(state);
226228
}
227229
}
230+
#[cfg(not(stage0))]
231+
#[stable(feature = "rust1", since = "1.0.0")]
232+
impl<T: ?Sized + Hash> Hash for Box<T> {
233+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
234+
(**self).hash(state);
235+
}
236+
}
228237

229238
/// Extension methods for an owning `Any` trait object.
230239
#[unstable(feature = "alloc",

Diff for: src/liballoc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
#![feature(unboxed_closures)]
7474
#![feature(unsafe_no_drop_flag)]
7575
#![feature(core)]
76-
#![feature(hash)]
7776
#![cfg_attr(all(not(feature = "external_funcs"), not(feature = "external_crate")),
7877
feature(libc))]
7978

Diff for: src/liballoc/rc.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ use core::clone::Clone;
150150
use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering};
151151
use core::default::Default;
152152
use core::fmt;
153-
use core::hash::{self, Hash};
153+
use core::hash::{Hasher, Hash};
154154
use core::marker;
155155
use core::mem::{transmute, min_align_of, size_of, forget};
156156
use core::nonzero::NonZero;
@@ -599,12 +599,20 @@ impl<T: Ord> Ord for Rc<T> {
599599
}
600600

601601
// FIXME (#18248) Make `T` `Sized?`
602-
impl<S: hash::Hasher, T: Hash<S>> Hash<S> for Rc<T> {
602+
#[cfg(stage0)]
603+
impl<S: Hasher, T: Hash<S>> Hash<S> for Rc<T> {
603604
#[inline]
604605
fn hash(&self, state: &mut S) {
605606
(**self).hash(state);
606607
}
607608
}
609+
#[cfg(not(stage0))]
610+
#[stable(feature = "rust1", since = "1.0.0")]
611+
impl<T: Hash> Hash for Rc<T> {
612+
fn hash<H: Hasher>(&self, state: &mut H) {
613+
(**self).hash(state);
614+
}
615+
}
608616

609617
#[stable(feature = "rust1", since = "1.0.0")]
610618
impl<T: fmt::Display> fmt::Display for Rc<T> {

Diff for: src/libcollections/bit.rs

+21
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ impl fmt::Debug for Bitv {
984984
}
985985

986986
#[stable(feature = "rust1", since = "1.0.0")]
987+
#[cfg(stage0)]
987988
impl<S: hash::Writer + hash::Hasher> hash::Hash<S> for Bitv {
988989
fn hash(&self, state: &mut S) {
989990
self.nbits.hash(state);
@@ -992,6 +993,16 @@ impl<S: hash::Writer + hash::Hasher> hash::Hash<S> for Bitv {
992993
}
993994
}
994995
}
996+
#[stable(feature = "rust1", since = "1.0.0")]
997+
#[cfg(not(stage0))]
998+
impl hash::Hash for Bitv {
999+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1000+
self.nbits.hash(state);
1001+
for elem in self.blocks() {
1002+
elem.hash(state);
1003+
}
1004+
}
1005+
}
9951006

9961007
#[stable(feature = "rust1", since = "1.0.0")]
9971008
impl cmp::PartialEq for Bitv {
@@ -1756,13 +1767,23 @@ impl fmt::Debug for BitvSet {
17561767
}
17571768
}
17581769

1770+
#[cfg(stage0)]
17591771
impl<S: hash::Writer + hash::Hasher> hash::Hash<S> for BitvSet {
17601772
fn hash(&self, state: &mut S) {
17611773
for pos in self {
17621774
pos.hash(state);
17631775
}
17641776
}
17651777
}
1778+
#[stable(feature = "rust1", since = "1.0.0")]
1779+
#[cfg(not(stage0))]
1780+
impl hash::Hash for BitvSet {
1781+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1782+
for pos in self {
1783+
pos.hash(state);
1784+
}
1785+
}
1786+
}
17661787

17671788
/// An iterator for `BitvSet`.
17681789
#[derive(Clone)]

Diff for: src/libcollections/btree/map.rs

+10
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,7 @@ impl<K: Ord, V> Extend<(K, V)> for BTreeMap<K, V> {
843843
}
844844
}
845845

846+
#[cfg(stage0)]
846847
#[stable(feature = "rust1", since = "1.0.0")]
847848
impl<S: Hasher, K: Hash<S>, V: Hash<S>> Hash<S> for BTreeMap<K, V> {
848849
fn hash(&self, state: &mut S) {
@@ -851,6 +852,15 @@ impl<S: Hasher, K: Hash<S>, V: Hash<S>> Hash<S> for BTreeMap<K, V> {
851852
}
852853
}
853854
}
855+
#[cfg(not(stage0))]
856+
#[stable(feature = "rust1", since = "1.0.0")]
857+
impl<K: Hash, V: Hash> Hash for BTreeMap<K, V> {
858+
fn hash<H: Hasher>(&self, state: &mut H) {
859+
for elt in self {
860+
elt.hash(state);
861+
}
862+
}
863+
}
854864

855865
#[stable(feature = "rust1", since = "1.0.0")]
856866
impl<K: Ord, V> Default for BTreeMap<K, V> {

Diff for: src/libcollections/dlist.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ use alloc::boxed::Box;
2727
use core::cmp::Ordering;
2828
use core::default::Default;
2929
use core::fmt;
30-
use core::hash::{Writer, Hasher, Hash};
30+
use core::hash::{Hasher, Hash};
31+
#[cfg(stage0)]
32+
use core::hash::Writer;
3133
use core::iter::{self, FromIterator, IntoIterator};
3234
use core::mem;
3335
use core::ptr;
@@ -926,6 +928,7 @@ impl<A: fmt::Debug> fmt::Debug for DList<A> {
926928
}
927929

928930
#[stable(feature = "rust1", since = "1.0.0")]
931+
#[cfg(stage0)]
929932
impl<S: Writer + Hasher, A: Hash<S>> Hash<S> for DList<A> {
930933
fn hash(&self, state: &mut S) {
931934
self.len().hash(state);
@@ -934,6 +937,16 @@ impl<S: Writer + Hasher, A: Hash<S>> Hash<S> for DList<A> {
934937
}
935938
}
936939
}
940+
#[stable(feature = "rust1", since = "1.0.0")]
941+
#[cfg(not(stage0))]
942+
impl<A: Hash> Hash for DList<A> {
943+
fn hash<H: Hasher>(&self, state: &mut H) {
944+
self.len().hash(state);
945+
for elt in self {
946+
elt.hash(state);
947+
}
948+
}
949+
}
937950

938951
#[cfg(test)]
939952
mod tests {

Diff for: src/libcollections/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#![feature(box_syntax)]
2727
#![feature(box_patterns)]
2828
#![feature(core)]
29-
#![feature(hash)]
3029
#![feature(staged_api)]
3130
#![feature(unboxed_closures)]
3231
#![feature(unicode)]

Diff for: src/libcollections/ring_buf.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ use core::ops::{Index, IndexMut};
3131
use core::ptr;
3232
use core::raw::Slice as RawSlice;
3333

34-
use core::hash::{Writer, Hash, Hasher};
34+
use core::hash::{Hash, Hasher};
35+
#[cfg(stage0)] use core::hash::Writer;
3536
use core::cmp;
3637

3738
use alloc::heap;
@@ -1667,6 +1668,7 @@ impl<A: Ord> Ord for RingBuf<A> {
16671668
}
16681669

16691670
#[stable(feature = "rust1", since = "1.0.0")]
1671+
#[cfg(stage0)]
16701672
impl<S: Writer + Hasher, A: Hash<S>> Hash<S> for RingBuf<A> {
16711673
fn hash(&self, state: &mut S) {
16721674
self.len().hash(state);
@@ -1675,6 +1677,16 @@ impl<S: Writer + Hasher, A: Hash<S>> Hash<S> for RingBuf<A> {
16751677
}
16761678
}
16771679
}
1680+
#[stable(feature = "rust1", since = "1.0.0")]
1681+
#[cfg(not(stage0))]
1682+
impl<A: Hash> Hash for RingBuf<A> {
1683+
fn hash<H: Hasher>(&self, state: &mut H) {
1684+
self.len().hash(state);
1685+
for elt in self {
1686+
elt.hash(state);
1687+
}
1688+
}
1689+
}
16781690

16791691
#[stable(feature = "rust1", since = "1.0.0")]
16801692
impl<A> Index<usize> for RingBuf<A> {

Diff for: src/libcollections/string.rs

+9
Original file line numberDiff line numberDiff line change
@@ -833,12 +833,21 @@ impl fmt::Debug for String {
833833
}
834834

835835
#[unstable(feature = "collections", reason = "waiting on Hash stabilization")]
836+
#[cfg(stage0)]
836837
impl<H: hash::Writer + hash::Hasher> hash::Hash<H> for String {
837838
#[inline]
838839
fn hash(&self, hasher: &mut H) {
839840
(**self).hash(hasher)
840841
}
841842
}
843+
#[stable(feature = "rust1", since = "1.0.0")]
844+
#[cfg(not(stage0))]
845+
impl hash::Hash for String {
846+
#[inline]
847+
fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
848+
(**self).hash(hasher)
849+
}
850+
}
842851

843852
#[unstable(feature = "collections",
844853
reason = "recent addition, needs more experience")]

Diff for: src/libcollections/vec.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1302,12 +1302,21 @@ impl<T:Clone> Clone for Vec<T> {
13021302
}
13031303
}
13041304

1305+
#[cfg(stage0)]
13051306
impl<S: hash::Writer + hash::Hasher, T: Hash<S>> Hash<S> for Vec<T> {
13061307
#[inline]
13071308
fn hash(&self, state: &mut S) {
13081309
Hash::hash(&**self, state)
13091310
}
13101311
}
1312+
#[stable(feature = "rust1", since = "1.0.0")]
1313+
#[cfg(not(stage0))]
1314+
impl<T: Hash> Hash for Vec<T> {
1315+
#[inline]
1316+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1317+
Hash::hash(&**self, state)
1318+
}
1319+
}
13111320

13121321
#[stable(feature = "rust1", since = "1.0.0")]
13131322
impl<T> Index<usize> for Vec<T> {

Diff for: src/libcollections/vec_map.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use core::prelude::*;
2020
use core::cmp::Ordering;
2121
use core::default::Default;
2222
use core::fmt;
23-
use core::hash::{Hash, Writer, Hasher};
23+
use core::hash::{Hash, Hasher};
24+
#[cfg(stage0)] use core::hash::Writer;
2425
use core::iter::{Enumerate, FilterMap, Map, FromIterator, IntoIterator};
2526
use core::iter;
2627
use core::mem::replace;
@@ -99,6 +100,7 @@ impl<V> Default for VecMap<V> {
99100
fn default() -> VecMap<V> { VecMap::new() }
100101
}
101102

103+
#[stable(feature = "rust1", since = "1.0.0")]
102104
impl<V:Clone> Clone for VecMap<V> {
103105
#[inline]
104106
fn clone(&self) -> VecMap<V> {
@@ -111,6 +113,7 @@ impl<V:Clone> Clone for VecMap<V> {
111113
}
112114
}
113115

116+
#[cfg(stage0)]
114117
impl<S: Writer + Hasher, V: Hash<S>> Hash<S> for VecMap<V> {
115118
fn hash(&self, state: &mut S) {
116119
// In order to not traverse the `VecMap` twice, count the elements
@@ -123,6 +126,20 @@ impl<S: Writer + Hasher, V: Hash<S>> Hash<S> for VecMap<V> {
123126
count.hash(state);
124127
}
125128
}
129+
#[stable(feature = "rust1", since = "1.0.0")]
130+
#[cfg(not(stage0))]
131+
impl<V: Hash> Hash for VecMap<V> {
132+
fn hash<H: Hasher>(&self, state: &mut H) {
133+
// In order to not traverse the `VecMap` twice, count the elements
134+
// during iteration.
135+
let mut count: usize = 0;
136+
for elt in self {
137+
elt.hash(state);
138+
count += 1;
139+
}
140+
count.hash(state);
141+
}
142+
}
126143

127144
impl<V> VecMap<V> {
128145
/// Creates an empty `VecMap`.

Diff for: src/libcore/array.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use clone::Clone;
1818
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
1919
use fmt;
20-
use hash::{Hash, Hasher, self};
20+
use hash::{Hash, self};
2121
use iter::IntoIterator;
2222
use marker::Copy;
2323
use ops::Deref;
@@ -35,11 +35,19 @@ macro_rules! array_impls {
3535
}
3636
}
3737

38-
impl<S: hash::Writer + Hasher, T: Hash<S>> Hash<S> for [T; $N] {
38+
#[cfg(stage0)]
39+
impl<S: hash::Writer + hash::Hasher, T: Hash<S>> Hash<S> for [T; $N] {
3940
fn hash(&self, state: &mut S) {
4041
Hash::hash(&self[], state)
4142
}
4243
}
44+
#[cfg(not(stage0))]
45+
#[stable(feature = "rust1", since = "1.0.0")]
46+
impl<T: Hash> Hash for [T; $N] {
47+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
48+
Hash::hash(&self[], state)
49+
}
50+
}
4351

4452
#[stable(feature = "rust1", since = "1.0.0")]
4553
impl<T: fmt::Debug> fmt::Debug for [T; $N] {

0 commit comments

Comments
 (0)