Skip to content

Tracking Issue for NonZeroXxx::from_mut/from_mut_unchecked (nonzero_from_mut) #106290

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

Open
2 of 4 tasks
SOF3 opened this issue Dec 30, 2022 · 3 comments
Open
2 of 4 tasks
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@SOF3
Copy link
Contributor

SOF3 commented Dec 30, 2022

Feature gate: #![feature(nonzero_from_mut)]

This is a tracking issue for the associated functions NonZeroXxx::from_mut and NonZeroXxx::from_mut_unchecked.

These associated functions allow converting a mutable reference to a primitive integer (&mut xxx) into the equivalent &mut NonZeroXxx, with or without validation. The safety is guaranteed by the mutable reference, but currently there is no way to perform this conversion without relying on an unsafe transparent cast.

Public API

// core::num

// $NonZeroInt is one of all signed and unsigned non-zero integer types in core::num,
// and $int is the corresponding primitive type.
impl $NonZeroInt {
    pub fn from_mut(n: &mut $int) -> Option<&mut $NonZeroInt>;
    pub unsafe fn from_mut_unchecked(n: &mut int) -> &mut $NonZeroInt;
}

Steps / History

Unresolved Questions

  • None yet.

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

@SOF3 SOF3 added C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Dec 30, 2022
@asquared31415

This comment was marked as resolved.

@SOF3

This comment was marked as resolved.

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
@zachs18
Copy link
Contributor

zachs18 commented Oct 28, 2024

NonZero::from_mut could return &mut Option<Self> instead of Option<&mut Self>, due to the layout guarantee that 0 is None::<NonZero<_>>1. This would be strictly more powerful (you could get a Option<&mut Self> with Option::as_mut after the cast; in fact, that is how from_mut is currently implemented).

Also, note that you can do this cast (checked, returning Option<NonZero<_>>) using bytemuck and zerocopy on stable.

Footnotes

  1. This is true for all currently valid NonZero<_> types, but note that technically the layout-compatbility guarantee is currently only given on the specific NonZeroU32 (etc) aliases/instantiations, not the generic NonZero<T> type itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants