Skip to content

Commit 6d3501e

Browse files
authored
Rollup merge of rust-lang#56250 - dwijnand:ptr-hash, r=alexcrichton
Introduce ptr::hash for references The RHS is what I used, which wasn't as convenient as `ptr::eq`, so I wondered: should `ptr::hash` exist? My first Rust PR, so I'm going to need some guidance. :)
2 parents 92638ef + ad76569 commit 6d3501e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/libcore/ptr.rs

+30
Original file line numberDiff line numberDiff line change
@@ -2516,6 +2516,36 @@ pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
25162516
a == b
25172517
}
25182518

2519+
/// Hash the raw pointer address behind a reference, rather than the value
2520+
/// it points to.
2521+
///
2522+
/// # Examples
2523+
///
2524+
/// ```
2525+
/// #![feature(ptr_hash)]
2526+
/// use std::collections::hash_map::DefaultHasher;
2527+
/// use std::hash::{Hash, Hasher};
2528+
/// use std::ptr;
2529+
///
2530+
/// let five = 5;
2531+
/// let five_ref = &five;
2532+
///
2533+
/// let mut hasher = DefaultHasher::new();
2534+
/// ptr::hash(five_ref, &mut hasher);
2535+
/// let actual = hasher.finish();
2536+
///
2537+
/// let mut hasher = DefaultHasher::new();
2538+
/// (five_ref as *const i32).hash(&mut hasher);
2539+
/// let expected = hasher.finish();
2540+
///
2541+
/// assert_eq!(actual, expected);
2542+
/// ```
2543+
#[unstable(feature = "ptr_hash", reason = "newly added", issue = "56286")]
2544+
pub fn hash<T, S: hash::Hasher>(hashee: *const T, into: &mut S) {
2545+
use hash::Hash;
2546+
hashee.hash(into);
2547+
}
2548+
25192549
// Impls for function pointers
25202550
macro_rules! fnptr_impls_safety_abi {
25212551
($FnTy: ty, $($Arg: ident),*) => {

0 commit comments

Comments
 (0)