diff options
| author | bors <bors@rust-lang.org> | 2017-09-12 21:39:08 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-09-12 21:39:08 +0000 |
| commit | 2fdccaffe632e316c43224d0fae6fba903333aa3 (patch) | |
| tree | 8c4b986ac2c0e210e943e60146ada289396b6a8a /src/liballoc | |
| parent | dd08c30703d052205a68ae34331eea464178cd99 (diff) | |
| parent | 143e2dcd5ce3a7ba5953378cde0f2850850d6e9f (diff) | |
| download | rust-2fdccaffe632e316c43224d0fae6fba903333aa3.tar.gz rust-2fdccaffe632e316c43224d0fae6fba903333aa3.zip | |
Auto merge of #44015 - kennytm:hasher, r=alexcrichton
impl Hasher for {&mut Hasher, Box<Hasher>}
**Rationale:** The `Hash` trait has `fn hash<H: Hasher>(&self, state: &mut H)`, which can only accept a `Sized` hasher, even if the `Hasher` trait is object-safe. We cannot retroactively add the `?Sized` bound without breaking stability, thus implementing `Hasher` to a trait object reference is the next best solution.
**Warning:** These `impl` are insta-stable, and should need an FCP. I don't think a full RFC is necessary.
Diffstat (limited to 'src/liballoc')
| -rw-r--r-- | src/liballoc/boxed.rs | 48 | ||||
| -rw-r--r-- | src/liballoc/lib.rs | 2 | ||||
| -rw-r--r-- | src/liballoc/tests/lib.rs | 16 |
3 files changed, 64 insertions, 2 deletions
diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index d9f4a2217db..4341b0b2975 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -62,7 +62,7 @@ use core::any::Any; use core::borrow; use core::cmp::Ordering; use core::fmt; -use core::hash::{self, Hash}; +use core::hash::{self, Hash, Hasher}; use core::iter::FusedIterator; use core::marker::{self, Unsize}; use core::mem; @@ -456,6 +456,52 @@ impl<T: ?Sized + Hash> Hash for Box<T> { } } +#[stable(feature = "indirect_hasher_impl", since = "1.22.0")] +impl<T: ?Sized + Hasher> Hasher for Box<T> { + fn finish(&self) -> u64 { + (**self).finish() + } + fn write(&mut self, bytes: &[u8]) { + (**self).write(bytes) + } + fn write_u8(&mut self, i: u8) { + (**self).write_u8(i) + } + fn write_u16(&mut self, i: u16) { + (**self).write_u16(i) + } + fn write_u32(&mut self, i: u32) { + (**self).write_u32(i) + } + fn write_u64(&mut self, i: u64) { + (**self).write_u64(i) + } + fn write_u128(&mut self, i: u128) { + (**self).write_u128(i) + } + fn write_usize(&mut self, i: usize) { + (**self).write_usize(i) + } + fn write_i8(&mut self, i: i8) { + (**self).write_i8(i) + } + fn write_i16(&mut self, i: i16) { + (**self).write_i16(i) + } + fn write_i32(&mut self, i: i32) { + (**self).write_i32(i) + } + fn write_i64(&mut self, i: i64) { + (**self).write_i64(i) + } + fn write_i128(&mut self, i: i128) { + (**self).write_i128(i) + } + fn write_isize(&mut self, i: isize) { + (**self).write_isize(i) + } +} + #[stable(feature = "from_for_ptrs", since = "1.6.0")] impl<T> From<T> for Box<T> { fn from(t: T) -> Self { diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index dc64a787ae9..2845d349ae1 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -121,7 +121,7 @@ #![feature(unsize)] #![feature(allocator_internals)] -#![cfg_attr(not(test), feature(fused, fn_traits, placement_new_protocol, swap_with_slice))] +#![cfg_attr(not(test), feature(fused, fn_traits, placement_new_protocol, swap_with_slice, i128))] #![cfg_attr(test, feature(test, box_heap))] // Allow testing this library diff --git a/src/liballoc/tests/lib.rs b/src/liballoc/tests/lib.rs index 8f3e71ef794..c5beb63d12e 100644 --- a/src/liballoc/tests/lib.rs +++ b/src/liballoc/tests/lib.rs @@ -50,3 +50,19 @@ fn hash<T: Hash>(t: &T) -> u64 { t.hash(&mut s); s.finish() } + +// FIXME: Instantiated functions with i128 in the signature is not supported in Emscripten. +// See https://github.com/kripken/emscripten-fastcomp/issues/169 +#[cfg(not(target_os = "emscripten"))] +#[test] +fn test_boxed_hasher() { + let ordinary_hash = hash(&5u32); + + let mut hasher_1 = Box::new(DefaultHasher::new()); + 5u32.hash(&mut hasher_1); + assert_eq!(ordinary_hash, hasher_1.finish()); + + let mut hasher_2 = Box::new(DefaultHasher::new()) as Box<Hasher>; + 5u32.hash(&mut hasher_2); + assert_eq!(ordinary_hash, hasher_2.finish()); +} |
