From 47b5e23e6b72183b993584dc41c51652eb6adb3a Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Mon, 26 Nov 2018 18:36:03 +0000 Subject: [PATCH 01/11] Introduce ptr::hash for references --- src/libcore/ptr.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index e9cf11424cae1..6b4ee66bb02cf 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2509,6 +2509,29 @@ pub fn eq(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 +/// +/// ``` +/// use std::collections::hash_map::DefaultHasher; +/// use std::hash::Hasher; +/// use std::ptr; +/// +/// let five = 5; +/// let five_ref = &five; +/// +/// let mut hasher = DefaultHasher::new(); +/// ptr::hash(five_ref, hasher); +/// println!("Hash is {:x}!", hasher.finish()); +/// ``` +#[stable(feature = "rust1", since = "1.0.0")] // TODO: replace with ??? +pub fn hash(a: &T, into: &mut S) { + use hash::Hash; + NonNull::from(a).hash(into) +} + // Impls for function pointers macro_rules! fnptr_impls_safety_abi { ($FnTy: ty, $($Arg: ident),*) => { From 86d20f9342c8b6cfd24d199eacb26ed11a95da12 Mon Sep 17 00:00:00 2001 From: Dale Wijnand <344610+dwijnand@users.noreply.github.com> Date: Mon, 26 Nov 2018 19:23:20 +0000 Subject: [PATCH 02/11] FIXME is the new TODO --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 6b4ee66bb02cf..7a57c365b2301 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2526,7 +2526,7 @@ pub fn eq(a: *const T, b: *const T) -> bool { /// ptr::hash(five_ref, hasher); /// println!("Hash is {:x}!", hasher.finish()); /// ``` -#[stable(feature = "rust1", since = "1.0.0")] // TODO: replace with ??? +#[stable(feature = "rust1", since = "1.0.0")] // FIXME: replace with ??? pub fn hash(a: &T, into: &mut S) { use hash::Hash; NonNull::from(a).hash(into) From 5558c07c6e24b6677e3a60a1314d705c8c23ab47 Mon Sep 17 00:00:00 2001 From: Dale Wijnand <344610+dwijnand@users.noreply.github.com> Date: Mon, 26 Nov 2018 21:31:12 +0000 Subject: [PATCH 03/11] Fix ptr::hex doc example --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 7a57c365b2301..c0e8e58114414 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2523,7 +2523,7 @@ pub fn eq(a: *const T, b: *const T) -> bool { /// let five_ref = &five; /// /// let mut hasher = DefaultHasher::new(); -/// ptr::hash(five_ref, hasher); +/// ptr::hash(five_ref, &mut hasher); /// println!("Hash is {:x}!", hasher.finish()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] // FIXME: replace with ??? From 7b429b0440eb7e8888ea600ca2103cb13999b3a2 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 27 Nov 2018 16:25:38 +0000 Subject: [PATCH 04/11] Fix stability attribute for ptr::hash --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index c0e8e58114414..13aae988d6c10 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2526,7 +2526,7 @@ pub fn eq(a: *const T, b: *const T) -> bool { /// ptr::hash(five_ref, &mut hasher); /// println!("Hash is {:x}!", hasher.finish()); /// ``` -#[stable(feature = "rust1", since = "1.0.0")] // FIXME: replace with ??? +#[unstable(feature = "ptr_hash", reason = "newly added", issue = "56285")] pub fn hash(a: &T, into: &mut S) { use hash::Hash; NonNull::from(a).hash(into) From 81251491ddecb5c55b6d22b04abf33ef65d3a74b Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 27 Nov 2018 16:33:01 +0000 Subject: [PATCH 05/11] Pick a better variable name for ptr::hash --- src/libcore/ptr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 13aae988d6c10..ff679d92e6096 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2527,9 +2527,9 @@ pub fn eq(a: *const T, b: *const T) -> bool { /// println!("Hash is {:x}!", hasher.finish()); /// ``` #[unstable(feature = "ptr_hash", reason = "newly added", issue = "56285")] -pub fn hash(a: &T, into: &mut S) { +pub fn hash(hashee: &T, into: &mut S) { use hash::Hash; - NonNull::from(a).hash(into) + NonNull::from(hashee).hash(into) } // Impls for function pointers From afb4fbd951bc9780b5a7812f9a72aa9e2f085814 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 27 Nov 2018 16:46:24 +0000 Subject: [PATCH 06/11] Add an assert_eq in ptr::hash's doc example --- src/libcore/ptr.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index ff679d92e6096..7b93795728b92 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2524,7 +2524,13 @@ pub fn eq(a: *const T, b: *const T) -> bool { /// /// let mut hasher = DefaultHasher::new(); /// ptr::hash(five_ref, &mut hasher); -/// println!("Hash is {:x}!", hasher.finish()); +/// let actual = hasher.finish(); +/// +/// let mut hasher = DefaultHasher::new(); +/// (five_ref as *const T).hash(&mut hasher); +/// let expected = hasher.finish(); +/// +/// assert_eq!(actual, expected); /// ``` #[unstable(feature = "ptr_hash", reason = "newly added", issue = "56285")] pub fn hash(hashee: &T, into: &mut S) { From 4a7ffe2646b5d152297ab8008cc64693b4fd3d0a Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 27 Nov 2018 16:46:59 +0000 Subject: [PATCH 07/11] Fix issue number --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 7b93795728b92..7c8b195db8f1c 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2532,7 +2532,7 @@ pub fn eq(a: *const T, b: *const T) -> bool { /// /// assert_eq!(actual, expected); /// ``` -#[unstable(feature = "ptr_hash", reason = "newly added", issue = "56285")] +#[unstable(feature = "ptr_hash", reason = "newly added", issue = "56286")] pub fn hash(hashee: &T, into: &mut S) { use hash::Hash; NonNull::from(hashee).hash(into) From 9755410f73584909ffa2f3241a946d47139d1902 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 27 Nov 2018 20:09:18 +0000 Subject: [PATCH 08/11] Try to fix ptr::hash's doc example --- src/libcore/ptr.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 7c8b195db8f1c..3024031b3bb29 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2516,18 +2516,19 @@ pub fn eq(a: *const T, b: *const T) -> bool { /// /// ``` /// use std::collections::hash_map::DefaultHasher; -/// use std::hash::Hasher; +/// use std::hash::{Hash, Hasher}; /// use std::ptr; /// /// let five = 5; /// let five_ref = &five; /// /// let mut hasher = DefaultHasher::new(); +/// #[feature(ptr_hash)] /// ptr::hash(five_ref, &mut hasher); /// let actual = hasher.finish(); /// /// let mut hasher = DefaultHasher::new(); -/// (five_ref as *const T).hash(&mut hasher); +/// (five_ref as *const i32).hash(&mut hasher); /// let expected = hasher.finish(); /// /// assert_eq!(actual, expected); From 097b5db5f67c28719d0065c6a74a2e56ae52d2d3 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 27 Nov 2018 21:18:20 +0000 Subject: [PATCH 09/11] Move feature enable in ptr::hash doc example --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 3024031b3bb29..0213b310efaa3 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2515,6 +2515,7 @@ pub fn eq(a: *const T, b: *const T) -> bool { /// # Examples /// /// ``` +/// #![feature(ptr_hash)] /// use std::collections::hash_map::DefaultHasher; /// use std::hash::{Hash, Hasher}; /// use std::ptr; @@ -2523,7 +2524,6 @@ pub fn eq(a: *const T, b: *const T) -> bool { /// let five_ref = &five; /// /// let mut hasher = DefaultHasher::new(); -/// #[feature(ptr_hash)] /// ptr::hash(five_ref, &mut hasher); /// let actual = hasher.finish(); /// From 6fab3f9c693e1699d9056ec64e3c950ad3f7bfb3 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 4 Dec 2018 07:48:20 +0000 Subject: [PATCH 10/11] Make ptr::hash take a raw painter like ptr::eq --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 0213b310efaa3..606dd765ce1d2 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2534,7 +2534,7 @@ pub fn eq(a: *const T, b: *const T) -> bool { /// assert_eq!(actual, expected); /// ``` #[unstable(feature = "ptr_hash", reason = "newly added", issue = "56286")] -pub fn hash(hashee: &T, into: &mut S) { +pub fn hash(hashee: *const T, into: &mut S) { use hash::Hash; NonNull::from(hashee).hash(into) } From ad765695d1a831b9e1c11102a1e8680af86a89bd Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 4 Dec 2018 08:06:26 +0000 Subject: [PATCH 11/11] Fix ptr::hash, just hash the raw pointer --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 606dd765ce1d2..4be89180d677d 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2536,7 +2536,7 @@ pub fn eq(a: *const T, b: *const T) -> bool { #[unstable(feature = "ptr_hash", reason = "newly added", issue = "56286")] pub fn hash(hashee: *const T, into: &mut S) { use hash::Hash; - NonNull::from(hashee).hash(into) + hashee.hash(into); } // Impls for function pointers