Skip to content

Commit 47b5e23

Browse files
committed
Introduce ptr::hash for references
1 parent 6bfb46e commit 47b5e23

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/libcore/ptr.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,6 +2509,29 @@ pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
25092509
a == b
25102510
}
25112511

2512+
/// Hash the raw pointer address behind a reference, rather than the value
2513+
/// it points to.
2514+
///
2515+
/// # Examples
2516+
///
2517+
/// ```
2518+
/// use std::collections::hash_map::DefaultHasher;
2519+
/// use std::hash::Hasher;
2520+
/// use std::ptr;
2521+
///
2522+
/// let five = 5;
2523+
/// let five_ref = &five;
2524+
///
2525+
/// let mut hasher = DefaultHasher::new();
2526+
/// ptr::hash(five_ref, hasher);
2527+
/// println!("Hash is {:x}!", hasher.finish());
2528+
/// ```
2529+
#[stable(feature = "rust1", since = "1.0.0")] // TODO: replace with ???
2530+
pub fn hash<T, S: hash::Hasher>(a: &T, into: &mut S) {
2531+
use hash::Hash;
2532+
NonNull::from(a).hash(into)
2533+
}
2534+
25122535
// Impls for function pointers
25132536
macro_rules! fnptr_impls_safety_abi {
25142537
($FnTy: ty, $($Arg: ident),*) => {

0 commit comments

Comments
 (0)