Skip to content

Add Arc::into_inner for safely discarding Arcs without calling the destructor on the inner type. #162

Closed
@steffahn

Description

@steffahn
Member

Proposal

Problem / Motivation / Solution

There already is a PR: rust-lang/rust#106854

Motivation

The functionality provided by this new “method” on Arc was previously not archievable with the Arc API. The function into_inner is related to try_unwrap. The expression Arc::into_inner(x) is almost the same as Arc::try_unwrap(x).ok(), however the latter includes two steps, the try_unwrap call and dropping the Arc, whereas into_inner accesses the Arc atomically. Since this is an issue in multi-threaded settings only, a similar function on Rc is not strictly necessary but could be wanted nontheless for ergonomic and API-similarity reasons. (The existing PR currently only offers the function on Arc eventually adding an analogue for Rc can be reasonable.)

The function Arc::into_inner(this: Arc<T>) -> Option<T> offers a way to “drop” an Arc without calling the destructor on the contained type. When the Arc 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 are async to properly get rid of.

Rendered Documentation

Screenshot_20230114_224308

Links and related work

Some time ago, there was a discussion on IRLO.

Current PR: rust-lang/rust#106854

Two previous PRs closed due to me being inactive: rust-lang/rust#79665, rust-lang/rust#75911

What happens now?

This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.

Activity

dtolnay

dtolnay commented on Jan 14, 2023

@dtolnay
Member

Seconded. Please open a tracking issue in rust-lang/rust.

added
initial-comment-periodWill be merged/postponed/closed in ~10 calendar days unless new substational objections are raised.
on Jan 14, 2023
added a commit that references this issue on Jan 23, 2023

Rollup merge of rust-lang#106854 - steffahn:drop_linear_arc_rebased, …

28081a6
steffahn

steffahn commented on Mar 23, 2023

@steffahn
MemberAuthor

The API is implemented and about to be stabilized.

added
ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)
and removed
initial-comment-periodWill be merged/postponed/closed in ~10 calendar days unless new substational objections are raised.
on May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @dtolnay@steffahn

        Issue actions

          Add `Arc::into_inner` for safely discarding `Arc`s without calling the destructor on the inner type. · Issue #162 · rust-lang/libs-team