-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Implement Hash for raw pointers to unsized types #45483
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
src/libcore/hash/mod.rs
Outdated
// We know T is unsized, because Sized types are handled | ||
// by the specialized impl below. | ||
let (a, b) = unsafe { | ||
*(*self as *const T as *const (usize, usize)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this end up interpreting T
itself as the usize
pair? i.e. self
is &*const T
, then *self as *const T
is a redundant cast, then the final cast changes it to a pointer the fat pointer pieces.
I think you want *(self as *const *const T as *const (usize, usize))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, you are right, of course. Fixed this and added a test.
7b24872
to
f4b4631
Compare
src/libcore/hash/mod.rs
Outdated
@@ -664,13 +664,39 @@ mod impls { | |||
} | |||
} | |||
|
|||
#[unstable(feature = "unsized_pointer_hash", issue = "0")] // TODO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought impl
are insta-stable?
Also, please change the TODO to FIXME to pass tidy
check.
[00:03:26] tidy error: /checkout/src/libcore/hash/mod.rs:667: TODO is deprecated; use FIXME
[00:03:26] tidy error: /checkout/src/libcore/hash/mod.rs:687: TODO is deprecated; use FIXME
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Is this something that's possible to implement without specialization? We've been hesitant to stabilize any API surface area that requires specialization to exist up to this point unfortunately |
Yes, it could be done in a single |
In that case, could this PR be updated to that implementation? |
Updated to not use specialization. |
@bors: r+ Awesome, thanks! cc @rust-lang/libs I'm going ahead an approving this as this seems like the natural extension of |
📌 Commit c2c1910 has been approved by |
Implement Hash for raw pointers to unsized types This is useful for some niche cases, like a hash table of slices or trait objects where the key is the raw pointer. Example use case: https://docs.rs/by_address
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems good to me. This implementation is not necessarily correct if we ever add extra-fat pointers, but we can sort that out at the time.
This fixes the problem where only the "data" part of a fat pointer could be hashed. Depends on rust-lang/rust#45483. Fixes #2.
Allow ptr::hash to accept fat pointers Fat pointers implement Hash since rust-lang#45483. This is a follow-up to rust-lang#56250.
Allow ptr::hash to accept fat pointers Fat pointers implement Hash since rust-lang#45483. This is a follow-up to rust-lang#56250.
Allow ptr::hash to accept fat pointers Fat pointers implement Hash since rust-lang#45483. This is a follow-up to rust-lang#56250.
Allow ptr::hash to accept fat pointers Fat pointers implement Hash since rust-lang#45483. This is a follow-up to rust-lang#56250.
This is useful for some niche cases, like a hash table of slices or trait objects where the key is the raw pointer. Example use case: https://docs.rs/by_address