Skip to content

Commit 9300482

Browse files
committed
Auto merge of #44015 - kennytm:hasher, r=<try>
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.
2 parents 426711d + ccf6d90 commit 9300482

File tree

5 files changed

+117
-2
lines changed

5 files changed

+117
-2
lines changed

src/liballoc/boxed.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use core::any::Any;
6262
use core::borrow;
6363
use core::cmp::Ordering;
6464
use core::fmt;
65-
use core::hash::{self, Hash};
65+
use core::hash::{self, Hash, Hasher};
6666
use core::iter::FusedIterator;
6767
use core::marker::{self, Unsize};
6868
use core::mem;
@@ -456,6 +456,52 @@ impl<T: ?Sized + Hash> Hash for Box<T> {
456456
}
457457
}
458458

459+
#[stable(feature = "indirect_hasher_impl", since = "1.21.0")]
460+
impl<T: ?Sized + Hasher> Hasher for Box<T> {
461+
fn finish(&self) -> u64 {
462+
(**self).finish()
463+
}
464+
fn write(&mut self, bytes: &[u8]) {
465+
(**self).write(bytes)
466+
}
467+
fn write_u8(&mut self, i: u8) {
468+
(**self).write_u8(i)
469+
}
470+
fn write_u16(&mut self, i: u16) {
471+
(**self).write_u16(i)
472+
}
473+
fn write_u32(&mut self, i: u32) {
474+
(**self).write_u32(i)
475+
}
476+
fn write_u64(&mut self, i: u64) {
477+
(**self).write_u64(i)
478+
}
479+
fn write_u128(&mut self, i: u128) {
480+
(**self).write_u128(i)
481+
}
482+
fn write_usize(&mut self, i: usize) {
483+
(**self).write_usize(i)
484+
}
485+
fn write_i8(&mut self, i: i8) {
486+
(**self).write_i8(i)
487+
}
488+
fn write_i16(&mut self, i: i16) {
489+
(**self).write_i16(i)
490+
}
491+
fn write_i32(&mut self, i: i32) {
492+
(**self).write_i32(i)
493+
}
494+
fn write_i64(&mut self, i: i64) {
495+
(**self).write_i64(i)
496+
}
497+
fn write_i128(&mut self, i: i128) {
498+
(**self).write_i128(i)
499+
}
500+
fn write_isize(&mut self, i: isize) {
501+
(**self).write_isize(i)
502+
}
503+
}
504+
459505
#[stable(feature = "from_for_ptrs", since = "1.6.0")]
460506
impl<T> From<T> for Box<T> {
461507
fn from(t: T) -> Self {

src/liballoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
#![feature(unsize)]
122122
#![feature(allocator_internals)]
123123

124-
#![cfg_attr(not(test), feature(fused, fn_traits, placement_new_protocol))]
124+
#![cfg_attr(not(test), feature(fused, fn_traits, placement_new_protocol, i128))]
125125
#![cfg_attr(test, feature(test, box_heap))]
126126

127127
// Allow testing this library

src/liballoc/tests/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,16 @@ fn hash<T: Hash>(t: &T) -> u64 {
5555
t.hash(&mut s);
5656
s.finish()
5757
}
58+
59+
#[test]
60+
fn test_boxed_hasher() {
61+
let ordinary_hash = hash(&5u32);
62+
63+
let mut hasher_1 = Box::new(DefaultHasher::new());
64+
5u32.hash(&mut hasher_1);
65+
assert_eq!(ordinary_hash, hasher_1.finish());
66+
67+
let mut hasher_2 = Box::new(DefaultHasher::new()) as Box<Hasher>;
68+
5u32.hash(&mut hasher_2);
69+
assert_eq!(ordinary_hash, hasher_2.finish());
70+
}

src/libcore/hash/mod.rs

+46
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,52 @@ pub trait Hasher {
359359
}
360360
}
361361

362+
#[stable(feature = "indirect_hasher_impl", since = "1.21.0")]
363+
impl<'a, H: Hasher + ?Sized> Hasher for &'a mut H {
364+
fn finish(&self) -> u64 {
365+
(**self).finish()
366+
}
367+
fn write(&mut self, bytes: &[u8]) {
368+
(**self).write(bytes)
369+
}
370+
fn write_u8(&mut self, i: u8) {
371+
(**self).write_u8(i)
372+
}
373+
fn write_u16(&mut self, i: u16) {
374+
(**self).write_u16(i)
375+
}
376+
fn write_u32(&mut self, i: u32) {
377+
(**self).write_u32(i)
378+
}
379+
fn write_u64(&mut self, i: u64) {
380+
(**self).write_u64(i)
381+
}
382+
fn write_u128(&mut self, i: u128) {
383+
(**self).write_u128(i)
384+
}
385+
fn write_usize(&mut self, i: usize) {
386+
(**self).write_usize(i)
387+
}
388+
fn write_i8(&mut self, i: i8) {
389+
(**self).write_i8(i)
390+
}
391+
fn write_i16(&mut self, i: i16) {
392+
(**self).write_i16(i)
393+
}
394+
fn write_i32(&mut self, i: i32) {
395+
(**self).write_i32(i)
396+
}
397+
fn write_i64(&mut self, i: i64) {
398+
(**self).write_i64(i)
399+
}
400+
fn write_i128(&mut self, i: i128) {
401+
(**self).write_i128(i)
402+
}
403+
fn write_isize(&mut self, i: isize) {
404+
(**self).write_isize(i)
405+
}
406+
}
407+
362408
/// A trait for creating instances of [`Hasher`].
363409
///
364410
/// A `BuildHasher` is typically used (e.g. by [`HashMap`]) to create

src/libcore/tests/hash/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,13 @@ fn test_custom_state() {
109109

110110
assert_eq!(hash(&Custom { hash: 5 }), 5);
111111
}
112+
113+
#[test]
114+
fn test_indirect_hasher() {
115+
let mut hasher = MyHasher { hash: 0 };
116+
{
117+
let mut indirect_hasher: &mut Hasher = &mut hasher;
118+
5u32.hash(&mut indirect_hasher);
119+
}
120+
assert_eq!(hasher.hash, 5);
121+
}

0 commit comments

Comments
 (0)