Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce ptr::hash for references #56250

Merged
merged 11 commits into from Dec 7, 2018
29 changes: 29 additions & 0 deletions src/libcore/ptr.rs
Expand Up @@ -2509,6 +2509,35 @@ 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As now it's taking a pointer, "behind a reference" may need to be removed.

/// it points to.
///
/// # Examples
///
/// ```
Centril marked this conversation as resolved.
Show resolved Hide resolved
/// 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, &mut hasher);
/// 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 = "56286")]
pub fn hash<T, S: hash::Hasher>(hashee: &T, into: &mut S) {
use hash::Hash;
NonNull::from(hashee).hash(into)
}

// Impls for function pointers
macro_rules! fnptr_impls_safety_abi {
($FnTy: ty, $($Arg: ident),*) => {
Expand Down