Skip to content

Commit 20db398

Browse files
committed
Make NonZero*::new const
With rust-lang#49146 merged, NonZero*::new can be const; see rust-lang#53718.
1 parent 9a9cbd6 commit 20db398

File tree

3 files changed

+62
-9
lines changed

3 files changed

+62
-9
lines changed

src/libcore/num/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", s
7070
/// Creates a non-zero if the given value is not zero.
7171
#[$stability]
7272
#[inline]
73+
#[rustc_const_unstable(feature = "const_int_nonzero")]
74+
#[cfg(not(bootstrap))]
75+
pub const fn new(n: $Int) -> Option<Self> {
76+
if n != 0 {
77+
// SAFETY: we just checked that there's no `0`
78+
Some(unsafe { $Ty(n) })
79+
} else {
80+
None
81+
}
82+
}
83+
84+
/// No docs for bootstrap.
85+
#[$stability]
86+
#[inline]
87+
#[cfg(bootstrap)]
7388
pub fn new(n: $Int) -> Option<Self> {
7489
if n != 0 {
7590
// SAFETY: we just checked that there's no `0`
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// run-pass
2+
#![feature(const_int_nonzero)]
3+
4+
use std::num::{
5+
NonZeroI8,
6+
NonZeroU8,
7+
NonZeroI32,
8+
NonZeroUsize,
9+
};
10+
11+
macro_rules! assert_same_const {
12+
($(const $ident:ident: $ty:ty = $exp:expr;)+) => {
13+
$(const $ident: $ty = $exp;)+
14+
15+
pub fn main() {
16+
$(assert_eq!($exp, $ident);)+
17+
}
18+
}
19+
}
20+
21+
assert_same_const! {
22+
const NON_ZERO_NEW_1: Option<NonZeroI8> = NonZeroI8::new(1);
23+
const NON_ZERO_NEW_2: Option<NonZeroI8> = NonZeroI8::new(0);
24+
const NON_ZERO_NEW_3: Option<NonZeroI8> = NonZeroI8::new(-38);
25+
const NON_ZERO_NEW_4: Option<NonZeroU8> = NonZeroU8::new(1);
26+
const NON_ZERO_NEW_5: Option<NonZeroU8> = NonZeroU8::new(0);
27+
const NON_ZERO_NEW_6: Option<NonZeroI32> = NonZeroI32::new(1);
28+
const NON_ZERO_NEW_7: Option<NonZeroI32> = NonZeroI32::new(0);
29+
const NON_ZERO_NEW_8: Option<NonZeroI32> = NonZeroI32::new(-38);
30+
const NON_ZERO_NEW_9: Option<NonZeroUsize> = NonZeroUsize::new(1);
31+
const NON_ZERO_NEW_10: Option<NonZeroUsize> = NonZeroUsize::new(0);
32+
33+
// Option::unwrap isn't supported in const yet, so we use new_unchecked.
34+
const NON_ZERO_GET_1: i8 = unsafe { NonZeroI8::new_unchecked(1) }.get();
35+
const NON_ZERO_GET_2: i8 = unsafe { NonZeroI8::new_unchecked(-38) }.get();
36+
const NON_ZERO_GET_3: u8 = unsafe { NonZeroU8::new_unchecked(1) }.get();
37+
const NON_ZERO_GET_4: i32 = unsafe { NonZeroI32::new_unchecked(1) }.get();
38+
const NON_ZERO_GET_5: i32 = unsafe { NonZeroI32::new_unchecked(-38) }.get();
39+
const NON_ZERO_GET_6: usize = unsafe { NonZeroUsize::new_unchecked(1) }.get();
40+
41+
const NON_ZERO_NEW_UNCHECKED_1: NonZeroI8 = unsafe { NonZeroI8::new_unchecked(1) };
42+
const NON_ZERO_NEW_UNCHECKED_2: NonZeroI8 = unsafe { NonZeroI8::new_unchecked(-38) };
43+
const NON_ZERO_NEW_UNCHECKED_3: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(1) };
44+
const NON_ZERO_NEW_UNCHECKED_4: NonZeroI32 = unsafe { NonZeroI32::new_unchecked(1) };
45+
const NON_ZERO_NEW_UNCHECKED_5: NonZeroI32 = unsafe { NonZeroI32::new_unchecked(-38) };
46+
const NON_ZERO_NEW_UNCHECKED_6: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(1) };
47+
}

src/test/ui/consts/const-nonzero.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)