Skip to content

Commit bb1cd33

Browse files
SimonSapinVardhan Thigle
authored and
Vardhan Thigle
committed
Add signed num::NonZeroI* types
Multiple people have asked for them, in rust-lang#49137. Given that the unsigned ones already exist, they are very easy to add and not an additional maintenance burden.
1 parent 8371c7e commit bb1cd33

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

src/libcore/num/mod.rs

+21-15
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use ops;
1010
use str::FromStr;
1111

1212
macro_rules! impl_nonzero_fmt {
13-
( ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
13+
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
1414
$(
15-
#[stable(feature = "nonzero", since = "1.28.0")]
15+
#[$stability]
1616
impl fmt::$Trait for $Ty {
1717
#[inline]
1818
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -31,7 +31,7 @@ macro_rules! doc_comment {
3131
}
3232

3333
macro_rules! nonzero_integers {
34-
( $( $Ty: ident($Int: ty); )+ ) => {
34+
( $( #[$stability: meta] $Ty: ident($Int: ty); )+ ) => {
3535
$(
3636
doc_comment! {
3737
concat!("An integer that is known not to equal zero.
@@ -41,10 +41,10 @@ For example, `Option<", stringify!($Ty), ">` is the same size as `", stringify!(
4141
4242
```rust
4343
use std::mem::size_of;
44-
assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", stringify!($Int),
44+
assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", stringify!($Int),
4545
">());
4646
```"),
47-
#[stable(feature = "nonzero", since = "1.28.0")]
47+
#[$stability]
4848
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
4949
#[repr(transparent)]
5050
#[rustc_layout_scalar_valid_range_start(1)]
@@ -57,14 +57,14 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
5757
/// # Safety
5858
///
5959
/// The value must not be zero.
60-
#[stable(feature = "nonzero", since = "1.28.0")]
60+
#[$stability]
6161
#[inline]
6262
pub const unsafe fn new_unchecked(n: $Int) -> Self {
6363
$Ty(n)
6464
}
6565

6666
/// Create a non-zero if the given value is not zero.
67-
#[stable(feature = "nonzero", since = "1.28.0")]
67+
#[$stability]
6868
#[inline]
6969
pub fn new(n: $Int) -> Option<Self> {
7070
if n != 0 {
@@ -75,7 +75,7 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
7575
}
7676

7777
/// Returns the value as a primitive type.
78-
#[stable(feature = "nonzero", since = "1.28.0")]
78+
#[$stability]
7979
#[inline]
8080
pub const fn get(self) -> $Int {
8181
self.0
@@ -91,19 +91,25 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
9191
}
9292

9393
impl_nonzero_fmt! {
94-
(Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
94+
#[$stability] (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
9595
}
9696
)+
9797
}
9898
}
9999

100100
nonzero_integers! {
101-
NonZeroU8(u8);
102-
NonZeroU16(u16);
103-
NonZeroU32(u32);
104-
NonZeroU64(u64);
105-
NonZeroU128(u128);
106-
NonZeroUsize(usize);
101+
#[stable(feature = "nonzero", since = "1.28.0")] NonZeroU8(u8);
102+
#[stable(feature = "nonzero", since = "1.28.0")] NonZeroU16(u16);
103+
#[stable(feature = "nonzero", since = "1.28.0")] NonZeroU32(u32);
104+
#[stable(feature = "nonzero", since = "1.28.0")] NonZeroU64(u64);
105+
#[stable(feature = "nonzero", since = "1.28.0")] NonZeroU128(u128);
106+
#[stable(feature = "nonzero", since = "1.28.0")] NonZeroUsize(usize);
107+
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI8(i8);
108+
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI16(i16);
109+
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI32(i32);
110+
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI64(i64);
111+
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroI128(i128);
112+
#[stable(feature = "signed_nonzero", since = "1.34.0")] NonZeroIsize(isize);
107113
}
108114

109115
/// Provides intentionally-wrapped arithmetic on `T`.

src/libcore/tests/nonzero.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::num::NonZeroU32;
1+
use core::num::{NonZeroU32, NonZeroI32};
22
use core::option::Option;
33
use core::option::Option::{Some, None};
44
use std::mem::size_of;
@@ -13,6 +13,7 @@ fn test_create_nonzero_instance() {
1313
#[test]
1414
fn test_size_nonzero_in_option() {
1515
assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());
16+
assert_eq!(size_of::<NonZeroI32>(), size_of::<Option<NonZeroI32>>());
1617
}
1718

1819
#[test]
@@ -118,3 +119,10 @@ fn test_from_nonzero() {
118119
let num: u32 = nz.into();
119120
assert_eq!(num, 1u32);
120121
}
122+
123+
#[test]
124+
fn test_from_signed_nonzero() {
125+
let nz = NonZeroI32::new(1).unwrap();
126+
let num: i32 = nz.into();
127+
assert_eq!(num, 1i32);
128+
}

src/test/ui/try-block/try-block-bad-type.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ LL | Err("")?; //~ ERROR the trait bound `i32: std::convert::From<&str>`
55
| ^^^^^^^^ the trait `std::convert::From<&str>` is not implemented for `i32`
66
|
77
= help: the following implementations were found:
8-
<i32 as std::convert::From<bool>>
8+
<i32 as std::convert::From<core::num::NonZeroI32>>
99
<i32 as std::convert::From<i16>>
1010
<i32 as std::convert::From<i8>>
11-
<i32 as std::convert::From<u16>>
1211
<i32 as std::convert::From<u8>>
12+
and 2 others
1313
= note: required by `std::convert::From::from`
1414

1515
error[E0271]: type mismatch resolving `<std::result::Result<i32, i32> as std::ops::Try>::Ok == &str`

0 commit comments

Comments
 (0)