From 47b5e23e6b72183b993584dc41c51652eb6adb3a Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Mon, 26 Nov 2018 18:36:03 +0000 Subject: Introduce ptr::hash for references --- src/libcore/ptr.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/libcore') diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index e9cf11424ca..6b4ee66bb02 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),*) => { -- cgit 1.4.1-3-g733a5 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: FIXME is the new TODO --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 6b4ee66bb02..7a57c365b23 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) -- cgit 1.4.1-3-g733a5 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: Fix ptr::hex doc example --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 7a57c365b23..c0e8e581144 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 ??? -- cgit 1.4.1-3-g733a5 From 7b429b0440eb7e8888ea600ca2103cb13999b3a2 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 27 Nov 2018 16:25:38 +0000 Subject: Fix stability attribute for ptr::hash --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index c0e8e581144..13aae988d6c 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) -- cgit 1.4.1-3-g733a5 From 81251491ddecb5c55b6d22b04abf33ef65d3a74b Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 27 Nov 2018 16:33:01 +0000 Subject: Pick a better variable name for ptr::hash --- src/libcore/ptr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 13aae988d6c..ff679d92e60 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 -- cgit 1.4.1-3-g733a5 From afb4fbd951bc9780b5a7812f9a72aa9e2f085814 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 27 Nov 2018 16:46:24 +0000 Subject: Add an assert_eq in ptr::hash's doc example --- src/libcore/ptr.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index ff679d92e60..7b93795728b 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) { -- cgit 1.4.1-3-g733a5 From 4a7ffe2646b5d152297ab8008cc64693b4fd3d0a Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 27 Nov 2018 16:46:59 +0000 Subject: Fix issue number --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 7b93795728b..7c8b195db8f 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) -- cgit 1.4.1-3-g733a5 From 9755410f73584909ffa2f3241a946d47139d1902 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 27 Nov 2018 20:09:18 +0000 Subject: Try to fix ptr::hash's doc example --- src/libcore/ptr.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 7c8b195db8f..3024031b3bb 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); -- cgit 1.4.1-3-g733a5 From 097b5db5f67c28719d0065c6a74a2e56ae52d2d3 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 27 Nov 2018 21:18:20 +0000 Subject: Move feature enable in ptr::hash doc example --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 3024031b3bb..0213b310efa 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(); /// -- cgit 1.4.1-3-g733a5 From 6fab3f9c693e1699d9056ec64e3c950ad3f7bfb3 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 4 Dec 2018 07:48:20 +0000 Subject: Make ptr::hash take a raw painter like ptr::eq --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 0213b310efa..606dd765ce1 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) } -- cgit 1.4.1-3-g733a5 From ad765695d1a831b9e1c11102a1e8680af86a89bd Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 4 Dec 2018 08:06:26 +0000 Subject: Fix ptr::hash, just hash the raw pointer --- src/libcore/ptr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libcore') diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index 606dd765ce1..4be89180d67 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 -- cgit 1.4.1-3-g733a5