-
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 Weak::{strong_count, weak_count} #56696
Conversation
This comment has been minimized.
This comment has been minimized.
src/liballoc/sync.rs
Outdated
// Due to the implicit weak pointer added when any strong pointers are | ||
// around, we cannot implement `weak_count` correctly since it necessarily | ||
// requires accessing the strong count and weak count in an unsynchronized | ||
// fashion. |
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've added a strong_count
method to sync::Weak
as well, but here the tradeoff is less clear since (I think) weak_count
can not be implemented correctly, so the API must stay asymmetric like this (or not offer strong_count
at all, since it isn't very useful)
ping from triage, can anyone from @rust-lang/libs review this? |
Having a discrepancy in |
Oops sorry for the delay, @jonas-schievink feel free to ping the PR when it's updated as notifications aren't always sent out. I wonder if |
Should the same be done for |
Yeah I agree that |
Er to add some more, I'm not too worried about the inconsistency of return values, these are pretty niche methods so having them be absolutely consistent isn't so important |
@bors: r+ Ok looks great to me to add, thanks @jonas-schievink! |
📌 Commit 0d314f0 has been approved by |
Implement Weak::{strong_count, weak_count} The counters are also useful on `Weak`, not just on strong references (`Rc` or `Arc`). In situations where there are still strong references around, you can also get these counts by temporarily upgrading and adjusting the values accordingly. Using the methods introduced here is simpler to do, less error-prone (since you can't forget to adjust the counts), can also be used when no strong references are around anymore, and might be more efficient due to not having to temporarily create an `Rc`. This is mainly useful in assertions or tests of complex data structures. Data structures might have internal invariants that make them the sole owner of a `Weak` pointer, and an assertion on the weak count could be used to ensure that this indeed happens as expected. Due to the presence of `Weak::upgrade`, the `strong_count` becomes less useful, but it still seems worthwhile to mirror the API of `Rc`. TODO: * [X] Tracking issue - #57977 Closes #50158
☀️ Test successful - checks-travis, status-appveyor |
Stabilize `std::{rc,sync}::Weak::{weak_count, strong_count}` * Original PR: rust-lang#56696 * Tracking issue: rust-lang#57977 Closes: rust-lang#57977 Supporting comments: > Although these were added for testing, it is occasionally useful to have a way to probe optimistically for whether a weak pointer has become dangling, without actually taking the overhead of manipulating atomics. Are there any plans to stabilize this? _Originally posted by @bdonlan in rust-lang#57977 (comment) > Having this stabilized would help. Currently, the only way to check if a weak pointer has become dangling is to call `upgrade`, which is by far expensive. _Originally posted by @glebpom in rust-lang#57977 (comment) Not sure if stabilizing these warrants a full RFC, so throwing this out here as a start for now. Note: per CONTRIBUTING.md, I ran the tidy checks, but they seem to be failing on unchanged files (primarily in `src/stdsimd`).
Stabilize `std::{rc,sync}::Weak::{weak_count, strong_count}` * Original PR: rust-lang#56696 * Tracking issue: rust-lang#57977 Closes: rust-lang#57977 Supporting comments: > Although these were added for testing, it is occasionally useful to have a way to probe optimistically for whether a weak pointer has become dangling, without actually taking the overhead of manipulating atomics. Are there any plans to stabilize this? _Originally posted by @bdonlan in rust-lang#57977 (comment) > Having this stabilized would help. Currently, the only way to check if a weak pointer has become dangling is to call `upgrade`, which is by far expensive. _Originally posted by @glebpom in rust-lang#57977 (comment) Not sure if stabilizing these warrants a full RFC, so throwing this out here as a start for now. Note: per CONTRIBUTING.md, I ran the tidy checks, but they seem to be failing on unchanged files (primarily in `src/stdsimd`).
Stabilize `std::{rc,sync}::Weak::{weak_count, strong_count}` * Original PR: rust-lang#56696 * Tracking issue: rust-lang#57977 Closes: rust-lang#57977 Supporting comments: > Although these were added for testing, it is occasionally useful to have a way to probe optimistically for whether a weak pointer has become dangling, without actually taking the overhead of manipulating atomics. Are there any plans to stabilize this? _Originally posted by @bdonlan in rust-lang#57977 (comment) > Having this stabilized would help. Currently, the only way to check if a weak pointer has become dangling is to call `upgrade`, which is by far expensive. _Originally posted by @glebpom in rust-lang#57977 (comment) Not sure if stabilizing these warrants a full RFC, so throwing this out here as a start for now. Note: per CONTRIBUTING.md, I ran the tidy checks, but they seem to be failing on unchanged files (primarily in `src/stdsimd`).
The counters are also useful on
Weak
, not just on strong references (Rc
orArc
).In situations where there are still strong references around, you can also get these counts by temporarily upgrading and adjusting the values accordingly. Using the methods introduced here is simpler to do, less error-prone (since you can't forget to adjust the counts), can also be used when no strong references are around anymore, and might be more efficient due to not having to temporarily create an
Rc
.This is mainly useful in assertions or tests of complex data structures. Data structures might have internal invariants that make them the sole owner of a
Weak
pointer, and an assertion on the weak count could be used to ensure that this indeed happens as expected. Due to the presence ofWeak::upgrade
, thestrong_count
becomes less useful, but it still seems worthwhile to mirror the API ofRc
.TODO:
Closes #50158