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

Implement Hash for raw pointers to unsized types #45483

Merged
merged 1 commit into from
Oct 26, 2017
Merged

Conversation

mbrubeck
Copy link
Contributor

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

@rust-highfive
Copy link
Collaborator

r? @alexcrichton

(rust_highfive has picked a reviewer for you, use r? to override)

// 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))
Copy link
Member

@cuviper cuviper Oct 23, 2017

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))

Copy link
Contributor Author

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.

@kennytm kennytm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Oct 24, 2017
@@ -664,13 +664,39 @@ mod impls {
}
}

#[unstable(feature = "unsized_pointer_hash", issue = "0")] // TODO
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@kennytm kennytm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 24, 2017
@alexcrichton
Copy link
Member

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

@mbrubeck
Copy link
Contributor Author

mbrubeck commented Oct 25, 2017

Yes, it could be done in a single ?Sized impl that checks if size_of::<*const T>() == size_of::<usize>().

@alexcrichton
Copy link
Member

In that case, could this PR be updated to that implementation?

@kennytm kennytm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 25, 2017
@mbrubeck
Copy link
Contributor Author

Updated to not use specialization.

@mbrubeck mbrubeck added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 25, 2017
@alexcrichton
Copy link
Member

@bors: r+

Awesome, thanks!

cc @rust-lang/libs

I'm going ahead an approving this as this seems like the natural extension of PartialEq to me which compares both pointers. If y'all have any objections though please let me know!

@bors
Copy link
Contributor

bors commented Oct 25, 2017

📌 Commit c2c1910 has been approved by alexcrichton

@kennytm kennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 25, 2017
kennytm added a commit to kennytm/rust that referenced this pull request Oct 25, 2017
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
bors added a commit that referenced this pull request Oct 25, 2017
Rollup of 7 pull requests

- Successful merges: #45059, #45212, #45398, #45483, #45496, #45508, #45526
- Failed merges:
Copy link
Member

@dtolnay dtolnay left a 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.

@bors bors merged commit c2c1910 into rust-lang:master Oct 26, 2017
mbrubeck added a commit to mbrubeck/by_address that referenced this pull request Jan 19, 2018
This fixes the problem where only the "data" part of a fat pointer could
be hashed.  Depends on rust-lang/rust#45483.  Fixes #2.
kennytm added a commit to kennytm/rust that referenced this pull request Dec 14, 2018
Allow ptr::hash to accept fat pointers

Fat pointers implement Hash since rust-lang#45483.  This is a follow-up to rust-lang#56250.
kennytm added a commit to kennytm/rust that referenced this pull request Dec 14, 2018
Allow ptr::hash to accept fat pointers

Fat pointers implement Hash since rust-lang#45483.  This is a follow-up to rust-lang#56250.
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Dec 14, 2018
Allow ptr::hash to accept fat pointers

Fat pointers implement Hash since rust-lang#45483.  This is a follow-up to rust-lang#56250.
pietroalbini added a commit to pietroalbini/rust that referenced this pull request Dec 15, 2018
Allow ptr::hash to accept fat pointers

Fat pointers implement Hash since rust-lang#45483.  This is a follow-up to rust-lang#56250.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants