-
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
Add pub fn ptr_eq(this: &Self, other: &Self) -> bool
to Rc and Arc
#35992
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @aturon (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
cc @rust-lang/libs I feel like there's some history here -- e.g. it used to exist -- but I'm not sure when/why this was removed offhand. In general I think this is a nice convenience to provide. |
I guess I don't see a reason to not have these in the standard library, but conversely I don't see a particular reason to have these in the standard library either. I've written them from time to time as well, but I never felt like there was a footgun or anything, the implementation was pretty clear. |
Here's a more general solution to the same problem (might make a useful crate): https://gist.github.com/61d5a030296ad3d5b0c3ea71c088c836 |
35201cd
to
6e292f4
Compare
(Side question: I’ve pushed what I think fixes the doctests, but Yes, it is possible and not particularly difficult to implement outside the standard library, but it seems to be a pretty basic operation when dealing with reference-counted or garbage-collected things. Both Python and Java have operators ( And Rust’s The only (small) footgun that I know of is being tempted to make a single generic function that works for both |
Agreed. I think we should ship it. |
Functionality seems fine to me. Is |
The |
Discussed at libs triage the conclusion was that these seem good to add. @Kimundi also pointed out that this is very similar to rust-lang/rfcs#1155 and we were also thinking it'd be good to add: fn std::mem::ptr_eq<T: ?Sized>(a: &T, b: &T) -> bool; @SimonSapin would you be interested in adding those methods as well? Also could you update the tracking issue reference here as well with a fresh issue? |
Sure, I can add this. Although in the RFC the arguments are Should these be three "features" and tracking issues, or just one? |
I kind of like |
Just to be clear however, I don’t think it works as a replacement for I’ll update the PR to add all three. |
Oh yeah, we absolutely want all three. |
6e292f4
to
bb8a439
Compare
bb8a439
to
8bb86c8
Compare
Filed #36497 and updated the PR. |
…rc`. Servo and Kuchiki have had helper functions doing this for some time.
8bb86c8
to
5ce9fee
Compare
@bors: r+ |
📌 Commit 5ce9fee has been approved by |
⌛ Testing commit 5ce9fee with merge d1acabe... |
Add `pub fn ptr_eq(this: &Self, other: &Self) -> bool` to Rc and Arc Servo and Kuchiki have had helper functions doing this for some time.
Could this reliably be used to perform an equality check for functions using pointers? For example: fn ein() { }
fn zwei() { }
fn main() {
println!("ein == zwei: {}", ein as *mut () == zwei as *mut ()); // false
println!("ein == zwei: {}", ein as *mut () == ein as *mut ()); // true
} The above works on stable, so what I'm really wondering about is how reliably this can be used. |
Servo and Kuchiki have had helper functions doing this for some time.