Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NonZeroXxx::from_mut #129

Closed
SOF3 opened this issue Oct 29, 2022 · 1 comment
Closed

Add NonZeroXxx::from_mut #129

SOF3 opened this issue Oct 29, 2022 · 1 comment
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

Comments

@SOF3
Copy link

SOF3 commented Oct 29, 2022

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.

@SOF3 SOF3 added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Oct 29, 2022
@m-ou-se
Copy link
Member

m-ou-se commented Dec 30, 2022

Sounds good. Accepted as unstable in rust-lang/rust#103730

@m-ou-se m-ou-se closed this as completed Dec 30, 2022
@dtolnay dtolnay added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Nov 23, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 20, 2024
…lacrum,dtolnay

Added NonZeroXxx::from_mut(_unchecked)?

ACP: rust-lang/libs-team#129
Tracking issue: rust-lang#106290
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 20, 2024
Rollup merge of rust-lang#103730 - SOF3:nonzero-from-mut, r=Mark-Simulacrum,dtolnay

Added NonZeroXxx::from_mut(_unchecked)?

ACP: rust-lang/libs-team#129
Tracking issue: rust-lang#106290
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Jan 21, 2024
…olnay

Added NonZeroXxx::from_mut(_unchecked)?

ACP: rust-lang/libs-team#129
Tracking issue: #106290
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

3 participants