about summary refs log tree commit diff
diff options
context:
space:
mode:
authorkennytm <kennytm@gmail.com>2018-12-07 12:42:30 +0800
committerGitHub <noreply@github.com>2018-12-07 12:42:30 +0800
commit6d3501ebe3b2395456c90d7c6147ce5a8244979d (patch)
tree45cbce44eafaaf9eb4434e00ee1057ef6eb7ad41
parent92638ef0cb753b2bb25b9cec8b6a1a8dce581327 (diff)
parentad765695d1a831b9e1c11102a1e8680af86a89bd (diff)
downloadrust-6d3501ebe3b2395456c90d7c6147ce5a8244979d.tar.gz
rust-6d3501ebe3b2395456c90d7c6147ce5a8244979d.zip
Rollup merge of #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. :)
-rw-r--r--src/libcore/ptr.rs30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs
index 0387708033b..8630dd402ef 100644
--- a/src/libcore/ptr.rs
+++ b/src/libcore/ptr.rs
@@ -2516,6 +2516,36 @@ pub fn eq<T: ?Sized>(a: *const T, b: *const T) -> bool {
     a == b
 }
 
+/// Hash the raw pointer address behind a reference, rather than the value
+/// it points to.
+///
+/// # Examples
+///
+/// ```
+/// #![feature(ptr_hash)]
+/// use std::collections::hash_map::DefaultHasher;
+/// use std::hash::{Hash, Hasher};
+/// use std::ptr;
+///
+/// let five = 5;
+/// let five_ref = &five;
+///
+/// let mut hasher = DefaultHasher::new();
+/// ptr::hash(five_ref, &mut hasher);
+/// let actual = hasher.finish();
+///
+/// let mut hasher = DefaultHasher::new();
+/// (five_ref as *const i32).hash(&mut hasher);
+/// let expected = hasher.finish();
+///
+/// assert_eq!(actual, expected);
+/// ```
+#[unstable(feature = "ptr_hash", reason = "newly added", issue = "56286")]
+pub fn hash<T, S: hash::Hasher>(hashee: *const T, into: &mut S) {
+    use hash::Hash;
+    hashee.hash(into);
+}
+
 // Impls for function pointers
 macro_rules! fnptr_impls_safety_abi {
     ($FnTy: ty, $($Arg: ident),*) => {