2018-08-30 12:18:55 +00:00
|
|
|
// run-pass
|
2018-09-25 21:51:35 +00:00
|
|
|
#![allow(dead_code)]
|
|
|
|
#![allow(unused_imports)]
|
2018-08-31 13:02:01 +00:00
|
|
|
#![allow(deprecated)]
|
|
|
|
#![allow(non_camel_case_types)]
|
|
|
|
#![allow(non_snake_case)]
|
2018-09-04 12:33:34 +00:00
|
|
|
#![allow(overflowing_literals)]
|
2018-08-31 13:02:01 +00:00
|
|
|
|
2015-08-12 00:27:05 +00:00
|
|
|
use std::hash::{Hash, SipHasher, Hasher};
|
2016-03-05 19:51:24 +00:00
|
|
|
use std::mem::size_of;
|
2014-02-22 05:33:23 +00:00
|
|
|
|
2014-12-31 04:32:49 +00:00
|
|
|
#[derive(Hash)]
|
2014-02-22 05:33:23 +00:00
|
|
|
struct Person {
|
2015-03-26 00:06:52 +00:00
|
|
|
id: usize,
|
2014-05-22 23:57:53 +00:00
|
|
|
name: String,
|
2015-03-26 00:06:52 +00:00
|
|
|
phone: usize,
|
2014-02-22 05:33:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 18:24:28 +00:00
|
|
|
// test for hygiene name collisions
|
|
|
|
#[derive(Hash)] struct __H__H;
|
|
|
|
#[derive(Hash)] enum Collision<__H> { __H { __H__H: __H } }
|
|
|
|
|
2016-03-05 19:51:24 +00:00
|
|
|
#[derive(Hash)]
|
|
|
|
enum E { A=1, B }
|
|
|
|
|
2015-02-18 04:48:07 +00:00
|
|
|
fn hash<T: Hash>(t: &T) -> u64 {
|
2020-05-01 00:41:51 +00:00
|
|
|
let mut s = SipHasher::new();
|
2015-08-12 00:27:05 +00:00
|
|
|
t.hash(&mut s);
|
|
|
|
s.finish()
|
std: Stabilize the std::hash module
This commit aims to prepare the `std::hash` module for alpha by formalizing its
current interface whileholding off on adding `#[stable]` to the new APIs. The
current usage with the `HashMap` and `HashSet` types is also reconciled by
separating out composable parts of the design. The primary goal of this slight
redesign is to separate the concepts of a hasher's state from a hashing
algorithm itself.
The primary change of this commit is to separate the `Hasher` trait into a
`Hasher` and a `HashState` trait. Conceptually the old `Hasher` trait was
actually just a factory for various states, but hashing had very little control
over how these states were used. Additionally the old `Hasher` trait was
actually fairly unrelated to hashing.
This commit redesigns the existing `Hasher` trait to match what the notion of a
`Hasher` normally implies with the following definition:
trait Hasher {
type Output;
fn reset(&mut self);
fn finish(&self) -> Output;
}
This `Hasher` trait emphasizes that hashing algorithms may produce outputs other
than a `u64`, so the output type is made generic. Other than that, however, very
little is assumed about a particular hasher. It is left up to implementors to
provide specific methods or trait implementations to feed data into a hasher.
The corresponding `Hash` trait becomes:
trait Hash<H: Hasher> {
fn hash(&self, &mut H);
}
The old default of `SipState` was removed from this trait as it's not something
that we're willing to stabilize until the end of time, but the type parameter is
always required to implement `Hasher`. Note that the type parameter `H` remains
on the trait to enable multidispatch for specialization of hashing for
particular hashers.
Note that `Writer` is not mentioned in either of `Hash` or `Hasher`, it is
simply used as part `derive` and the implementations for all primitive types.
With these definitions, the old `Hasher` trait is realized as a new `HashState`
trait in the `collections::hash_state` module as an unstable addition for
now. The current definition looks like:
trait HashState {
type Hasher: Hasher;
fn hasher(&self) -> Hasher;
}
The purpose of this trait is to emphasize that the one piece of functionality
for implementors is that new instances of `Hasher` can be created. This
conceptually represents the two keys from which more instances of a
`SipHasher` can be created, and a `HashState` is what's stored in a
`HashMap`, not a `Hasher`.
Implementors of custom hash algorithms should implement the `Hasher` trait, and
only hash algorithms intended for use in hash maps need to implement or worry
about the `HashState` trait.
The entire module and `HashState` infrastructure remains `#[unstable]` due to it
being recently redesigned, but some other stability decision made for the
`std::hash` module are:
* The `Writer` trait remains `#[experimental]` as it's intended to be replaced
with an `io::Writer` (more details soon).
* The top-level `hash` function is `#[unstable]` as it is intended to be generic
over the hashing algorithm instead of hardwired to `SipHasher`
* The inner `sip` module is now private as its one export, `SipHasher` is
reexported in the `hash` module.
And finally, a few changes were made to the default parameters on `HashMap`.
* The `RandomSipHasher` default type parameter was renamed to `RandomState`.
This renaming emphasizes that it is not a hasher, but rather just state to
generate hashers. It also moves away from the name "sip" as it may not always
be implemented as `SipHasher`. This type lives in the
`std::collections::hash_map` module as `#[unstable]`
* The associated `Hasher` type of `RandomState` is creatively called...
`Hasher`! This concrete structure lives next to `RandomState` as an
implemenation of the "default hashing algorithm" used for a `HashMap`. Under
the hood this is currently implemented as `SipHasher`, but it draws an
explicit interface for now and allows us to modify the implementation over
time if necessary.
There are many breaking changes outlined above, and as a result this commit is
a:
[breaking-change]
2014-12-09 20:37:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-05 19:51:24 +00:00
|
|
|
struct FakeHasher<'a>(&'a mut Vec<u8>);
|
|
|
|
impl<'a> Hasher for FakeHasher<'a> {
|
|
|
|
fn finish(&self) -> u64 {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, bytes: &[u8]) {
|
|
|
|
self.0.extend(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 19:59:20 +00:00
|
|
|
fn fake_hash<A: Hash>(v: &mut Vec<u8>, a: A) {
|
|
|
|
a.hash(&mut FakeHasher(v));
|
2016-03-05 19:51:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 19:18:08 +00:00
|
|
|
struct OnlyOneByteHasher;
|
|
|
|
impl Hasher for OnlyOneByteHasher {
|
|
|
|
fn finish(&self) -> u64 {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, bytes: &[u8]) {
|
|
|
|
assert_eq!(bytes.len(), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 05:33:23 +00:00
|
|
|
fn main() {
|
2014-05-13 00:56:43 +00:00
|
|
|
let person1 = Person {
|
|
|
|
id: 5,
|
2014-05-25 10:17:19 +00:00
|
|
|
name: "Janet".to_string(),
|
2014-05-13 00:56:43 +00:00
|
|
|
phone: 555_666_7777
|
|
|
|
};
|
|
|
|
let person2 = Person {
|
|
|
|
id: 5,
|
2014-05-25 10:17:19 +00:00
|
|
|
name: "Bob".to_string(),
|
2014-05-13 00:56:43 +00:00
|
|
|
phone: 555_666_7777
|
|
|
|
};
|
2015-06-07 18:00:38 +00:00
|
|
|
assert_eq!(hash(&person1), hash(&person1));
|
std: Stabilize the std::hash module
This commit aims to prepare the `std::hash` module for alpha by formalizing its
current interface whileholding off on adding `#[stable]` to the new APIs. The
current usage with the `HashMap` and `HashSet` types is also reconciled by
separating out composable parts of the design. The primary goal of this slight
redesign is to separate the concepts of a hasher's state from a hashing
algorithm itself.
The primary change of this commit is to separate the `Hasher` trait into a
`Hasher` and a `HashState` trait. Conceptually the old `Hasher` trait was
actually just a factory for various states, but hashing had very little control
over how these states were used. Additionally the old `Hasher` trait was
actually fairly unrelated to hashing.
This commit redesigns the existing `Hasher` trait to match what the notion of a
`Hasher` normally implies with the following definition:
trait Hasher {
type Output;
fn reset(&mut self);
fn finish(&self) -> Output;
}
This `Hasher` trait emphasizes that hashing algorithms may produce outputs other
than a `u64`, so the output type is made generic. Other than that, however, very
little is assumed about a particular hasher. It is left up to implementors to
provide specific methods or trait implementations to feed data into a hasher.
The corresponding `Hash` trait becomes:
trait Hash<H: Hasher> {
fn hash(&self, &mut H);
}
The old default of `SipState` was removed from this trait as it's not something
that we're willing to stabilize until the end of time, but the type parameter is
always required to implement `Hasher`. Note that the type parameter `H` remains
on the trait to enable multidispatch for specialization of hashing for
particular hashers.
Note that `Writer` is not mentioned in either of `Hash` or `Hasher`, it is
simply used as part `derive` and the implementations for all primitive types.
With these definitions, the old `Hasher` trait is realized as a new `HashState`
trait in the `collections::hash_state` module as an unstable addition for
now. The current definition looks like:
trait HashState {
type Hasher: Hasher;
fn hasher(&self) -> Hasher;
}
The purpose of this trait is to emphasize that the one piece of functionality
for implementors is that new instances of `Hasher` can be created. This
conceptually represents the two keys from which more instances of a
`SipHasher` can be created, and a `HashState` is what's stored in a
`HashMap`, not a `Hasher`.
Implementors of custom hash algorithms should implement the `Hasher` trait, and
only hash algorithms intended for use in hash maps need to implement or worry
about the `HashState` trait.
The entire module and `HashState` infrastructure remains `#[unstable]` due to it
being recently redesigned, but some other stability decision made for the
`std::hash` module are:
* The `Writer` trait remains `#[experimental]` as it's intended to be replaced
with an `io::Writer` (more details soon).
* The top-level `hash` function is `#[unstable]` as it is intended to be generic
over the hashing algorithm instead of hardwired to `SipHasher`
* The inner `sip` module is now private as its one export, `SipHasher` is
reexported in the `hash` module.
And finally, a few changes were made to the default parameters on `HashMap`.
* The `RandomSipHasher` default type parameter was renamed to `RandomState`.
This renaming emphasizes that it is not a hasher, but rather just state to
generate hashers. It also moves away from the name "sip" as it may not always
be implemented as `SipHasher`. This type lives in the
`std::collections::hash_map` module as `#[unstable]`
* The associated `Hasher` type of `RandomState` is creatively called...
`Hasher`! This concrete structure lives next to `RandomState` as an
implemenation of the "default hashing algorithm" used for a `HashMap`. Under
the hood this is currently implemented as `SipHasher`, but it draws an
explicit interface for now and allows us to modify the implementation over
time if necessary.
There are many breaking changes outlined above, and as a result this commit is
a:
[breaking-change]
2014-12-09 20:37:23 +00:00
|
|
|
assert!(hash(&person1) != hash(&person2));
|
2016-03-05 19:51:24 +00:00
|
|
|
|
|
|
|
// test #21714
|
|
|
|
let mut va = vec![];
|
|
|
|
let mut vb = vec![];
|
|
|
|
fake_hash(&mut va, E::A);
|
|
|
|
fake_hash(&mut vb, E::B);
|
|
|
|
assert!(va != vb);
|
2017-06-16 19:59:20 +00:00
|
|
|
|
|
|
|
// issue #39137: single variant enum hash should not hash discriminant
|
|
|
|
#[derive(Hash)]
|
|
|
|
enum SingleVariantEnum {
|
|
|
|
A(u8),
|
|
|
|
}
|
|
|
|
let mut v = vec![];
|
|
|
|
fake_hash(&mut v, SingleVariantEnum::A(17));
|
|
|
|
assert_eq!(vec![17], v);
|
2022-10-09 19:18:08 +00:00
|
|
|
|
|
|
|
// issue #39137
|
|
|
|
#[repr(u8)]
|
|
|
|
#[derive(Hash)]
|
|
|
|
enum E {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
}
|
|
|
|
E::A.hash(&mut OnlyOneByteHasher);
|
2014-02-22 05:33:23 +00:00
|
|
|
}
|