Skip to content

Commit e4cfaa1

Browse files
authored
Rollup merge of #90077 - woppopo:const_nonzero_from, r=oli-obk
Make `From` impls of NonZero integer const. I also changed the feature gate added to `From` impls of Atomic integer to `const_num_from_num` from `const_convert`. Tracking issue: #87852
2 parents 371fd4f + 2fc7806 commit e4cfaa1

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

library/core/src/convert/num.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -390,15 +390,16 @@ use crate::num::NonZeroUsize;
390390
macro_rules! nzint_impl_from {
391391
($Small: ty, $Large: ty, #[$attr:meta], $doc: expr) => {
392392
#[$attr]
393-
impl From<$Small> for $Large {
393+
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
394+
impl const From<$Small> for $Large {
394395
// Rustdocs on the impl block show a "[+] show undocumented items" toggle.
395396
// Rustdocs on functions do not.
396397
#[doc = $doc]
397398
#[inline]
398399
fn from(small: $Small) -> Self {
399400
// SAFETY: input type guarantees the value is non-zero
400401
unsafe {
401-
Self::new_unchecked(small.get().into())
402+
Self::new_unchecked(From::from(small.get()))
402403
}
403404
}
404405
}

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
#![feature(const_likely)]
116116
#![feature(const_maybe_uninit_as_ptr)]
117117
#![feature(const_maybe_uninit_assume_init)]
118+
#![feature(const_num_from_num)]
118119
#![feature(const_option)]
119120
#![feature(const_pin)]
120121
#![feature(const_replace)]

library/core/src/num/nonzero.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ macro_rules! nonzero_integers {
8282
}
8383

8484
#[stable(feature = "from_nonzero", since = "1.31.0")]
85-
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
85+
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
8686
impl const From<$Ty> for $Int {
8787
#[doc = concat!("Converts a `", stringify!($Ty), "` into an `", stringify!($Int), "`")]
8888
#[inline]

library/core/src/sync/atomic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ macro_rules! atomic_int {
13651365
}
13661366

13671367
#[$stable_from]
1368-
#[rustc_const_unstable(feature = "const_convert", issue = "88674")]
1368+
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
13691369
impl const From<$int_type> for $atomic_type {
13701370
#[doc = concat!("Converts an `", stringify!($int_type), "` into an `", stringify!($atomic_type), "`.")]
13711371
#[inline]

library/core/tests/nonzero.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ fn nonzero_const() {
204204
// test that the methods of `NonZeroX>` are usable in a const context
205205
// Note: only tests NonZero8
206206

207-
const NONZERO: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(5) };
207+
const NONZERO_U8: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(5) };
208208

209-
const GET: u8 = NONZERO.get();
209+
const GET: u8 = NONZERO_U8.get();
210210
assert_eq!(GET, 5);
211211

212212
const ZERO: Option<NonZeroU8> = NonZeroU8::new(0);
@@ -215,8 +215,11 @@ fn nonzero_const() {
215215
const ONE: Option<NonZeroU8> = NonZeroU8::new(1);
216216
assert!(ONE.is_some());
217217

218-
const FROM_NONZERO: u8 = u8::from(NONZERO);
219-
assert_eq!(FROM_NONZERO, 5);
218+
const FROM_NONZERO_U8: u8 = u8::from(NONZERO_U8);
219+
assert_eq!(FROM_NONZERO_U8, 5);
220+
221+
const NONZERO_CONVERT: NonZeroU32 = NonZeroU32::from(NONZERO_U8);
222+
assert_eq!(NONZERO_CONVERT.get(), 5);
220223
}
221224

222225
#[test]

0 commit comments

Comments
 (0)