Skip to content

Add NonZeroXxx::from_mut #129

@SOF3

Description

@SOF3

Proposal

Problem statement

Add a function to convert a &mut xxx to &mut NonZeroXxx.

Motivation, use-cases

My project requires working on a &mut NonZeroU32 (because only NonZeroU32 implements a certain trait I want to use), but the value actually came from a &mut u32 somewhere deep inside my data structures, so it would be inconvenient to create a separate NonZeroU32 and proxy back the result later on.

Solution sketches

For n: &mut i32, if *n != 0 is currently true, and we provide the aforementioned function to map this reference into a &mut NonZeroI32, since the original n cannot be dereferenced without dropping the new &mut NonZeroI32, it is safe to work with the &mut NonZeroI32 directly.

Links and related work

As this change is very simple, I have already implemented it in a libcore patch: rust-lang/rust#103730

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
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions