Skip to content

Commit

Permalink
Rollup merge of rust-lang#68976 - ecstatic-morse:const-non-zero, r=dt…
Browse files Browse the repository at this point in the history
…olnay

Make `num::NonZeroX::new` an unstable `const fn`

cc rust-lang#53718

These require `#[feature(const_if_match)]`, meaning they must remain unstable for the time being.
  • Loading branch information
Dylan-DPC authored Feb 10, 2020
2 parents b3cfb97 + 0755c41 commit e3315f6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", s

/// Creates a non-zero if the given value is not zero.
#[$stability]
#[rustc_const_unstable(feature = "const_nonzero_int_methods", issue = "53718")]
#[inline]
pub fn new(n: $Int) -> Option<Self> {
pub const fn new(n: $Int) -> Option<Self> {
if n != 0 {
// SAFETY: we just checked that there's no `0`
Some(unsafe { Self(n) })
Expand Down
11 changes: 10 additions & 1 deletion src/test/ui/consts/const-nonzero.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
// build-pass (FIXME(62277): could be check-pass?)
// run-pass

#![feature(const_nonzero_int_methods)]

use std::num::NonZeroU8;

const X: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(5) };
const Y: u8 = X.get();

const ZERO: Option<NonZeroU8> = NonZeroU8::new(0);
const ONE: Option<NonZeroU8> = NonZeroU8::new(1);

fn main() {
assert_eq!(Y, 5);

assert!(ZERO.is_none());
assert_eq!(ONE.unwrap().get(), 1);
}

0 comments on commit e3315f6

Please sign in to comment.