Add NonZeroXxx::from_mut #129
Labels
ACP-accepted
API Change Proposal is accepted (seconded with no objections)
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
T-libs-api
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 onlyNonZeroU32
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 separateNonZeroU32
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 originaln
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.
The text was updated successfully, but these errors were encountered: