Repository navigation
Open
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
Feature gate: #![feature(nonzero_ops)]
This is the tracking issue for #82703. The common arithmetic operators that keep the invariants of NonZero* types enforced are implemented for these types.
Public API
Example with U8/I8, but same for U16/I16 etc.
// core::num
impl NonZeroU8 {
pub const fn checked_add(self, other: u8) -> Option<Self>;
pub const fn saturating_add(self, other: u8) -> Self;
pub const unsafe fn unchecked_add(self, other: u8) -> Self;
pub const fn checked_next_power_of_two(self) -> Option<Self>;
}
impl NonZeroI8 {
pub const fn abs(self) -> Self;
pub const fn checked_abs(self) -> Option<Self>;
pub const fn overflowing_abs(self) -> (Self, bool);
pub const fn saturating_abs(self) -> Self;
pub const fn wrapping_abs(self) -> Self;
pub const fn unsigned_abs(self) -> NonZeroU8;
}
impl NonZeroU8 /* and NonZeroI8 */ {
pub const fn checked_mul(self, other: Self) -> Option<Self>;
pub const fn saturating_mul(self, other: Self) -> Self;
pub const unsafe fn unchecked_mul(self, other: Self) -> Self;
pub const fn checked_pow(self, other: u32) -> Option<Self>;
pub const fn saturating_pow(self, other: u32) -> Self;
}
/* Not yet on nightly, as it is insta-stable:
impl Neg for NonZeroI8 {
type Output = Self;
fn neg(self) -> Self;
}
*/Steps / History
- Implementation: Implement nonzero arithmetics for NonZero types. #82703
- Merge Mark unsafe methods NonZero*::unchecked_(add|mul) as const. #87910 marking unsafe methods as
const. - Final commenting period (FCP)
- Stabilize checked methods in Partial stabilization of "nonzero_checked_ops". #97547.
- Stabilize
unchecked_methods. - Add
impl Neg for NonZeroI*, which could not be feature-gated.
Unresolved Questions
- Should
unchecked_addandunchecked_mulbe markedconstand how? Would it imply that theirconst-ness for the underlying types be upgraded instd?
-> Leave as-is (non-const) until add const-ub RFC rfcs#3016 lands. (Implement nonzero arithmetics for NonZero types. #82703 (comment))
-> It has landed now, mark themconstin Mark unsafe methods NonZero*::unchecked_(add|mul) as const. #87910. - Naming: how to avoid confusion regarding the term
(un)checked, referring to checking for nonzeroness in std with theNonZeroInt::new_uncheckedmethod, but here referring to checking overflow? Discussion here.
->checkconsistently means "check everything that needs be checked in the current context, and refer to the doc to verify what needs to be checked in this context". The doc has been made explicit about what needs to be checked for every method above.
Reacted by Morten Lohne, Stanislav Tkach, José Luis Cruz, dzmitry-lahoda and Aaron Muir Hamilton
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCT-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Activity
[-]Tracking Issue for #82703 #![feature(nonzero_ops)][/-][+]Tracking Issue for `nonzero_ops`[/+]iago-lito commentedon Jun 12, 2021
How to not forget re-checking the constness of unchecked methods when this happens? Is there a way to leave some kind of mark in the repo that'll automatically trigger this discussion again when it's time?
programmerjake commentedon Aug 9, 2021
rust-lang/rfcs#3016 has landed...hoping these can be stabilized soon!
Rollup merge of rust-lang#87910 - iago-lito:mark_unsafe_nonzero_arith…
Rollup merge of rust-lang#87910 - iago-lito:mark_unsafe_nonzero_arith…
Noratrieb commentedon Apr 17, 2022
I just had a use for this, what's the state of this feature?
iago-lito commentedon Apr 19, 2022
Hi @Nilstrieb @jschwe. The state is that I don't quite know what's next. IIUC someone needs to write a stabilisation report for the feature to move forward, but this requires that a few use cases be collected first, right? I don't have many on my side, since I think I've only used it once then, but I'm happy to help :)
jschwe commentedon Apr 19, 2022
@iago-lito Is
nonzero_opsnot just a "library feature"? Looking at the guide for Stabilizing a library feature a stabilisation report does not seem to be necessary.iago-lito commentedon Apr 19, 2022
Hi @m-ou-se, is this the right time to ask for a FCP on this feature?
m-ou-se commentedon Apr 19, 2022
I've updated the top comment with an up to date summary of this feature.
@rfcbot merge
rfcbot commentedon Apr 19, 2022
Team member @m-ou-se has proposed to merge this. The next step is review by the rest of the tagged team members:
No concerns currently listed.
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!
See this document for info about what commands tagged team members can give me.
19 remaining items
Add strict methods to `NonZero`
Add strict methods to `NonZero`
Add strict methods to `NonZero`
Stabilize unchecked `nonzero_ops`
Stabilize unchecked `nonzero_ops`
Add strict methods to `NonZero`
Rollup merge of rust-lang#164848 - hsanzg:strict-nonzero, r=jhpratt
Rollup merge of rust-lang#164848 - hsanzg:strict-nonzero, r=jhpratt
Add strict methods to `NonZero`
Rollup merge of rust-lang#164848 - hsanzg:strict-nonzero, r=jhpratt
Rollup merge of rust-lang#164848 - hsanzg:strict-nonzero, r=jhpratt
Unrolled build for #164848 in rollup 165023
Stabilize unchecked `nonzero_ops`
Rollup merge of #164848 - hsanzg:strict-nonzero, r=jhpratt