Skip to content

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

Closed
@steffahn

Description

@steffahn

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.

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

    Issue actions