Repository navigation
Tracking Issue for {Arc, Rc}::into_inner #106894
Copy link
Copy link
Closed
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Description
Feature gates: #![feature(arc_into_inner)], #![feature(rc_into_inner)]
This is a tracking issue for the Arc::into_inner method, and (for API-consistency) also an Rc::into_inner analogue.
The function Arc::into_inner(x) serves as an atomic race-condition-free alternative to Arc::try_unwrap(x).ok(). See the ACP for more details.
Public API
// alloc::sync
impl<T> Arc<T> {
/// Returns the inner value, if the `Arc` has exactly one strong reference.
/// Otherwise, `None` is returned and the `Arc` is dropped.
pub fn into_inner(this: Arc<T>) -> Option<T>;
}and the extension to Rc
// alloc::rc
impl<T> Rc<T> {
/// Returns the inner value, if the `Rc` has exactly one strong reference.
/// Otherwise, `None` is returned and the `Rc` is dropped.
pub fn into_inner(this: Rc<T>) -> Option<T>;
}Steps / History
- ACP: Add
Arc::into_innerfor safely discardingArcs without calling the destructor on the inner type. libs-team#162 - Implementation:
- Final comment period (FCP)1
- Stabilization PR: Stabilize
arc_into_innerandrc_into_inner. #109504
Unresolved Questions
- None yet.
Footnotes
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libs-api[DEPRECATED; DO NOT USE][DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.This issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.The final comment period is finished for this PR / Issue.
Activity
Rollup merge of rust-lang#106854 - steffahn:drop_linear_arc_rebased, …
ryoqun commentedon Mar 1, 2023
Hey, I've stumbled on this. I wonder how soon this can be stabilized? My usecase is complex. but in a nut shell, I want this to conditionally and cleanly restart bunch of interconnected threads of mixed roles, all sharing
Arc<GlobalContext>, depending on which thread did actually dropped the GlobalContext.if sharing helps, i can write the usecase in detail...
Of course, I can resort to sync::Weak or tls. but it incurs overhead and/or further complexities. or
mem::transmutelike this to forcibly bring::into_innerinto stable rustc: https://rust.godbolt.org/z/r5rbsYYcT, in which i have to useunsafe... Also, due to already-existing wide-spread use ofArc<GlobalContext>in our codebase, switching to different Arc impl altogether isn't viable as well. That precondition also precludes more saner cleaning code arrangement in the first place. ;)joshtriplett commentedon Mar 11, 2023
Submitted the
Rc::into_innerimplementation as #109026joshtriplett commentedon Mar 11, 2023
I think
Arc::into_innermakes sense to stabilize at this point. AndRc::into_inneris trivial.@rust-lang/libs-api, shall we stabilize these?
@rfcbot merge
rfcbot commentedon Mar 11, 2023
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:
No concerns currently listed.
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!
See this document for info about what commands tagged team members can give me.
rfcbot commentedon Mar 12, 2023
🔔 This is now entering its final comment period, as per the review above. 🔔
6 remaining items
Rollup merge of #106854 - steffahn:drop_linear_arc_rebased, r=Mark-Si…
thynson commentedon Jan 23, 2024
Probably I'm too late here, but I think the
Arc::<T>::into_innershould returnsResult<T, Self>, instead ofOption<T>.Unlike
Rc, checkingArc::strong_count(arc) == 1does not guarantee that the subsequence call toArc::<T>::into_innerwill no fail. And when it happens, the caller lost an owningArc, making it impossible to recover from such a situation.Since
Arc::into_innerhas been stabilized, a new interfaceArc::try_into_inner::<T>(x: Arc<T>) -> Result<T, Arc<T>>could be proposed. It should check the reference count is1and unwrap it atomically, or returns theArcitself when the condition was not met, so the caller still have an owningArc.In theory, the above issue is not applicable for
Rc, butRc::try_into_inner::<T>(x: Rc<T>) -> Result<T, Rc<T>>could also be proposed for consistency.steffahn commentedon Jan 23, 2024
@thynson Feel free to open an issue if you have concrete ideas how the documentation of
Arc::into_innercan be improved to convince users to actually look at theArc::try_unwrapfunction. Of course, it's already mentioned there multiple times (it's also mentioned on this tracking issue) but apparently not in a way that convinced you to actually look at that method.Maybe also a doc alias “
try_into_inner” fortry_unwrapto further help discoverability 🤔 (though I'm not sure off the top of my head, how well those work for associated items; and also that would assume people use the search bar, anyways)thynson commentedon Jan 23, 2024
Umm, it's my bad that
Arc::try_unwrapalready mentioned in the doc ofArc::into_inner, which is exactly what I'm looking for, through the doc make me feel it's discouraged to be usedActually they're designed for different scenarios. Probably better to document it like:
steffahn commentedon Jan 23, 2024
Hmm, no
try_unwrapis only meant to be discouraged in the case where you ignore theArc<T>you get back in theErrcase. That’s clearly documented, too… but it’s describing the details of when and how to usetry_unwrap, and hence not documented ininto_inner’s documentation.On the other hand, I wasn’t aware that someone could try to combine
into_innerwith aArc::strong_count(…) == 1check.Anyways… maybe a sentence that introduces
Arc::try_unwrapbefore immediately calling out an expression using it could be sufficiently useful, after all.^^^ Maybe like this?
thynson commentedon Jan 23, 2024
If I understand it right, it looks like a typos of
The modified version should be clear enough for me.
And thanks for your quick response. 🥇
steffahn commentedon Jan 23, 2024
@thynson I’ve changed the wording a bit more, feel free to take a look #120266
Rollup merge of rust-lang#120266 - steffahn:a_rc_into_inner_docs, r=M…
Rollup merge of rust-lang#120266 - steffahn:a_rc_into_inner_docs, r=M…
Rollup merge of rust-lang#120266 - steffahn:a_rc_into_inner_docs, r=M…
Rollup merge of rust-lang#120266 - steffahn:a_rc_into_inner_docs, r=M…
Rollup merge of rust-lang#120266 - steffahn:a_rc_into_inner_docs, r=M…
Rollup merge of rust-lang#120266 - steffahn:a_rc_into_inner_docs, r=M…
Rollup merge of rust-lang#120266 - steffahn:a_rc_into_inner_docs, r=M…
Unrolled build for rust-lang#120266