-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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 Arc::into_inner
for safely discarding Arc
s without calling the destructor on the inner type.
#79665
Conversation
Also add documentation and tests for it. This commit has some minor unresolved questions and is intended to be amended.
…'s a lot squash me later
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joshtriplett (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. |
This comment has been minimized.
This comment has been minimized.
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 like how this is looking! 👌
/// ``` | ||
/// | ||
/// A more practical example demonstrating the need for `Arc::into_inner`: | ||
/// ``` |
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.
/// ``` | |
/// ```no_run |
In case a 100_000
iteration loop within a (doc-)test is not deemed desirable.
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.
At least on my machine this doc-test does not run particularly slowly. There seem to be lots of other doc tests that take much longer than this one. The 100_000 iterations are necessary to ensure stack overflows are even possible on enough platforms.
/// It is strongly recommended to use [`Arc::into_inner`] instead if you don't | ||
/// want to keep the `Arc` in the [`Err`] case. | ||
/// Immediately dropping the [`Err`] payload, like in the expression | ||
/// `Arc::try_unwrap(this).ok()`, can still cause the strong count to |
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.
Talking for the future here, but if/when this feature gets stabilized, it will be nice to have a (clippy?) lint for this 🙂
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 don't know if this has been done before, but adding a FIXME
comment about opening an issue for it on clippy would ensure this is not forgotten when this becomes stable.
Co-authored-by: Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>
It looks very very nice, thanks for sticking with this ! If you want to work on |
Would it make more sense for the signature to be |
@coolreader18 no, that’s I could take this as a hint that the docs might need improvement. On the other hand, in practice this could not be that confusing since the docs of |
Ah, whooooopssss, sorry... I was thinking for some reason that nothing like this existed already, but I think I was thinking of a |
@steffahn - Thanks for your contribution but I'm closing this is inactive. If you want to continue feel free to reopen. |
Very unfortunate to see this being closed. What are the steps missing, exactly? Could someone involved in a discussion make a list of things that need to be addressed for this to proceed? I'm sure somebody (if not me) will pick this up. This functionality is very much needed and missing currently. cc @steffahn |
@kvark Thanks for commenting. Reviving this PR was still somewhere deep on my (over-procrastinated) mental TODO list of things I’ll eventually need to get around to. AFAIR, there’s not too much effort necessary to bring this to completion, I’ll have to review the review comments so far, but I don’t remember there being any major problems. Also, this PR is merely about introducing the API behind a feature gate, not stabilizing it. Thanks for confirming that this functionality is “much needed”; for me personally it was only a technically missing portion of API, but I didn’t have practical need for it personally. I have also gained more contribution experience in the meantime anyways… let’s see, perhaps I can get this PR back on track fairly quickly… e.g. this weekend, or next week. |
I see, thanks for the info! I suppose I can't rely on this right now even if it's merged, given than it's behind a feature gate, so I'll search for a workaround.
I find this very clean and elegant for not relying on any unsafe functionality. But it will only work if I can do "unwrap or drop" atomically, which is what your PR (fortunately) already does. If you have ideas on the minimal workaround, please share them somewhere (matrix, email, etc, not sure if this PR is really appropriate). |
It it gets merged, stabilization can be the next step and needs not take extraordinarily long though (of course if you need a solution right now or quickly, that’s still not enough). Regarding workarounds, you could
|
I went the route of implementing my own linear |
|
@CAD97 this crate relies on a re-implementation of the coercion mechanism - https://crates.io/crates/unsize |
@steffahn one thing that I believe this PR is missing is a function similar to |
Triomphe only uses the unsize crate for thin arcs, IIRC. But yes, I'm very aware of the exact capabilities and limitations of the current unsizing mechanism. |
The code still rebases without problems. As far as I can tell there aren't actually any blockers to merging this as-is; if I don't implement it now, adding I opened #106854, let's get some progress here, finally. |
…structor on the inner type. Mainly rebased and squashed from PR rust-lang#79665, furthermore includes minor changes in comments.
…r=Mark-Simulacrum Add `Arc::into_inner` for safely discarding `Arc`s without calling the destructor on the inner type. ACP: rust-lang/libs-team#162 Reviving rust-lang#79665. I want to get this merged this time; this does not contain changes (apart from very minor changes in comments/docs). See rust-lang#79665 for further description of the PR. The only “unresolved” points that led to that PR being closed, AFAICT, were * The desire to also implement a `Rc::into_inner` function * however, this can very well also happen as a subsequent PR * Possible need for further discussion on the naming “`into_inner`” (?) * `into_inner` seems fine to me; also, this PR introduces unstable API, and names can be changed later, too * ~~I don't know if a tracking issue for the feature flag is supposed to be opened before or after this PR gets merged (if *before*, then I can add the issue number to the `#[unstable…]` attribute)~~ There is a [tracking issue](rust-lang#106894) now. I say “unresolved” in quotation marks because from my point of view, if reviewers agree, the PR can be merged immediately and as-is :-)
…structor on the inner type. Mainly rebased and squashed from PR rust-lang/rust#79665, furthermore includes minor changes in comments.
Re-opening #75911 after it was closed due to inactivity (sorry for having been so inactive)
For now that’s:
5d81f76
00d50fe
ca7066d
948e402
7d43bcb
1e28132
81623f4
f57de56
cd444ca
ecb85f0
earlier commits were already present in the previous PR and are only updated due to a rebase to the current master
// SAFETY
commentRc::into_inner
version forRc
, accordingly. Last previous comment on that topic. Probably being added to this PR very soon.into_inner
for now or are there people preferring something different?try_unwrap
documentation referring tointo_inner
. I commented that paragraph out for now with aFIXME
to make sure it gets re-introduced onceinto_inner
is stabilized; otherwise, it would be confusing if we had a stable function recommended using an unstable one, right? On the other hand the fact thatinto_inner
is not stable doesn’t help the fact that usingtry_unwrap
can introduce bugs... (relevant commit: 00d50fe)#[unstable(..)]
attribute. And perhaps also create a page in the Unstable Book? Or is that supposed to be in a seperate PR?cc @danielhenrymantilla @Diggsey
This section comes from the previous PR, #75911:
There was previously some discussion on IRLO.
Motivation
The functionality provided by this new “method” on
Arc
was previously not archievable with theArc
API. The functioninto_inner
is related to (and hence similarly named similar to)try_unwrap
. The expressionArc::into_inner(x)
is almost the same asArc::try_unwrap(x).ok()
, however the latter includes two steps, thetry_unwrap
call and dropping theArc
, whereasinto_inner
accesses theArc
atomically. Since this is an issue in multi-threaded settings only, a similar function onRc
is not strictly necessary but could be wanted nontheless for ergonomic and API-similarity reasons. (This PR currently only offers the function onArc
, but I could add one forRc
if wanted.) In the IRLO discussion, I also mentioned two more functions that could possibly extend this API.The function
Arc::into_inner(this: Arc<T>) -> Option<T>
offers a way to “drop” anArc
without calling the destructor on the contained type. When theArc
provided was the last strong pointer to its target, the target value is returned. Being able to do this is valueable around linear(-ish) types that should not or cannot just be dropped ordinarity, but require extra arguments, or operations that can fail or areasync
to properly get rid of.Rendered Documentation