Skip to content

Commit 02aedec

Browse files
committed
Rollup merge of rust-lang#50808 - SimonSapin:nonzero, r=alexcrichton
Stabilize num::NonZeroU* Tracking issue: rust-lang#49137
2 parents 63ea42f + 89d9ca9 commit 02aedec

File tree

14 files changed

+18
-150
lines changed

14 files changed

+18
-150
lines changed

src/liballoc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@
102102
#![feature(lang_items)]
103103
#![feature(libc)]
104104
#![feature(needs_allocator)]
105-
#![feature(nonzero)]
106105
#![feature(optin_builtin_traits)]
107106
#![feature(pattern)]
108107
#![feature(pin)]

src/libcore/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ pub mod prelude;
171171

172172
pub mod intrinsics;
173173
pub mod mem;
174-
pub mod nonzero;
175174
pub mod ptr;
176175
pub mod hint;
177176

@@ -221,6 +220,7 @@ pub mod heap {
221220

222221
// note: does not need to be public
223222
mod iter_private;
223+
mod nonzero;
224224
mod tuple;
225225
mod unit;
226226

src/libcore/nonzero.rs

+3-93
Original file line numberDiff line numberDiff line change
@@ -9,103 +9,13 @@
99
// except according to those terms.
1010

1111
//! Exposes the NonZero lang item which provides optimization hints.
12-
#![unstable(feature = "nonzero", reason = "deprecated", issue = "49137")]
13-
#![rustc_deprecated(reason = "use `std::ptr::NonNull` or `std::num::NonZero*` instead",
14-
since = "1.26.0")]
15-
#![allow(deprecated)]
1612
1713
use ops::CoerceUnsized;
1814

19-
/// Unsafe trait to indicate what types are usable with the NonZero struct
20-
pub unsafe trait Zeroable {
21-
/// Whether this value is zero
22-
fn is_zero(&self) -> bool;
23-
}
24-
25-
macro_rules! impl_zeroable_for_pointer_types {
26-
( $( $Ptr: ty )+ ) => {
27-
$(
28-
/// For fat pointers to be considered "zero", only the "data" part needs to be null.
29-
unsafe impl<T: ?Sized> Zeroable for $Ptr {
30-
#[inline]
31-
fn is_zero(&self) -> bool {
32-
(*self).is_null()
33-
}
34-
}
35-
)+
36-
}
37-
}
38-
39-
macro_rules! impl_zeroable_for_integer_types {
40-
( $( $Int: ty )+ ) => {
41-
$(
42-
unsafe impl Zeroable for $Int {
43-
#[inline]
44-
fn is_zero(&self) -> bool {
45-
*self == 0
46-
}
47-
}
48-
)+
49-
}
50-
}
51-
52-
impl_zeroable_for_pointer_types! {
53-
*const T
54-
*mut T
55-
}
56-
57-
impl_zeroable_for_integer_types! {
58-
usize u8 u16 u32 u64 u128
59-
isize i8 i16 i32 i64 i128
60-
}
61-
6215
/// A wrapper type for raw pointers and integers that will never be
6316
/// NULL or 0 that might allow certain optimizations.
6417
#[lang = "non_zero"]
65-
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Hash)]
66-
pub struct NonZero<T: Zeroable>(pub(crate) T);
67-
68-
impl<T: Zeroable> NonZero<T> {
69-
/// Creates an instance of NonZero with the provided value.
70-
/// You must indeed ensure that the value is actually "non-zero".
71-
#[inline]
72-
pub const unsafe fn new_unchecked(inner: T) -> Self {
73-
NonZero(inner)
74-
}
75-
76-
/// Creates an instance of NonZero with the provided value.
77-
#[inline]
78-
pub fn new(inner: T) -> Option<Self> {
79-
if inner.is_zero() {
80-
None
81-
} else {
82-
Some(NonZero(inner))
83-
}
84-
}
85-
86-
/// Gets the inner value.
87-
pub fn get(self) -> T {
88-
self.0
89-
}
90-
}
91-
92-
impl<T: Zeroable+CoerceUnsized<U>, U: Zeroable> CoerceUnsized<NonZero<U>> for NonZero<T> {}
93-
94-
impl<'a, T: ?Sized> From<&'a mut T> for NonZero<*mut T> {
95-
fn from(reference: &'a mut T) -> Self {
96-
NonZero(reference)
97-
}
98-
}
99-
100-
impl<'a, T: ?Sized> From<&'a mut T> for NonZero<*const T> {
101-
fn from(reference: &'a mut T) -> Self {
102-
let ptr: *mut T = reference;
103-
NonZero(ptr)
104-
}
105-
}
18+
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
19+
pub(crate) struct NonZero<T>(pub(crate) T);
10620

107-
impl<'a, T: ?Sized> From<&'a T> for NonZero<*const T> {
108-
fn from(reference: &'a T) -> Self {
109-
NonZero(reference)
110-
}
111-
}
21+
impl<T: CoerceUnsized<U>, U> CoerceUnsized<NonZero<U>> for NonZero<T> {}

src/libcore/num/mod.rs

+8-28
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ use convert::TryFrom;
1616
use fmt;
1717
use intrinsics;
1818
use mem;
19-
#[allow(deprecated)] use nonzero::NonZero;
19+
use nonzero::NonZero;
2020
use ops;
2121
use str::FromStr;
2222

2323
macro_rules! impl_nonzero_fmt {
24-
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
24+
( ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
2525
$(
26-
#[$stability]
27-
#[allow(deprecated)]
26+
#[stable(feature = "nonzero", since = "1.28.0")]
2827
impl fmt::$Trait for $Ty {
2928
#[inline]
3029
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -36,7 +35,7 @@ macro_rules! impl_nonzero_fmt {
3635
}
3736

3837
macro_rules! nonzero_integers {
39-
( #[$stability: meta] #[$deprecation: meta] $( $Ty: ident($Int: ty); )+ ) => {
38+
( $( $Ty: ident($Int: ty); )+ ) => {
4039
$(
4140
/// An integer that is known not to equal zero.
4241
///
@@ -47,27 +46,24 @@ macro_rules! nonzero_integers {
4746
/// use std::mem::size_of;
4847
/// assert_eq!(size_of::<Option<std::num::NonZeroU32>>(), size_of::<u32>());
4948
/// ```
50-
#[$stability]
51-
#[$deprecation]
52-
#[allow(deprecated)]
49+
#[stable(feature = "nonzero", since = "1.28.0")]
5350
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
5451
pub struct $Ty(NonZero<$Int>);
5552

56-
#[allow(deprecated)]
5753
impl $Ty {
5854
/// Create a non-zero without checking the value.
5955
///
6056
/// # Safety
6157
///
6258
/// The value must not be zero.
63-
#[$stability]
59+
#[stable(feature = "nonzero", since = "1.28.0")]
6460
#[inline]
6561
pub const unsafe fn new_unchecked(n: $Int) -> Self {
6662
$Ty(NonZero(n))
6763
}
6864

6965
/// Create a non-zero if the given value is not zero.
70-
#[$stability]
66+
#[stable(feature = "nonzero", since = "1.28.0")]
7167
#[inline]
7268
pub fn new(n: $Int) -> Option<Self> {
7369
if n != 0 {
@@ -78,7 +74,7 @@ macro_rules! nonzero_integers {
7874
}
7975

8076
/// Returns the value as a primitive type.
81-
#[$stability]
77+
#[stable(feature = "nonzero", since = "1.28.0")]
8278
#[inline]
8379
pub fn get(self) -> $Int {
8480
self.0 .0
@@ -87,16 +83,13 @@ macro_rules! nonzero_integers {
8783
}
8884

8985
impl_nonzero_fmt! {
90-
#[$stability]
9186
(Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
9287
}
9388
)+
9489
}
9590
}
9691

9792
nonzero_integers! {
98-
#[unstable(feature = "nonzero", issue = "49137")]
99-
#[allow(deprecated)] // Redundant, works around "error: inconsistent lockstep iteration"
10093
NonZeroU8(u8);
10194
NonZeroU16(u16);
10295
NonZeroU32(u32);
@@ -105,19 +98,6 @@ nonzero_integers! {
10598
NonZeroUsize(usize);
10699
}
107100

108-
nonzero_integers! {
109-
#[unstable(feature = "nonzero", issue = "49137")]
110-
#[rustc_deprecated(since = "1.26.0", reason = "\
111-
signed non-zero integers are considered for removal due to lack of known use cases. \
112-
If you’re using them, please comment on https://github.com/rust-lang/rust/issues/49137")]
113-
NonZeroI8(i8);
114-
NonZeroI16(i16);
115-
NonZeroI32(i32);
116-
NonZeroI64(i64);
117-
NonZeroI128(i128);
118-
NonZeroIsize(isize);
119-
}
120-
121101
/// Provides intentionally-wrapped arithmetic on `T`.
122102
///
123103
/// Operations like `+` on `u32` values is intended to never overflow,

src/libcore/ptr.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use fmt;
2323
use hash;
2424
use marker::{PhantomData, Unsize};
2525
use mem;
26-
#[allow(deprecated)] use nonzero::NonZero;
26+
use nonzero::NonZero;
2727

2828
use cmp::Ordering::{self, Less, Equal, Greater};
2929

@@ -2742,7 +2742,6 @@ impl<T: ?Sized> PartialOrd for *mut T {
27422742
#[unstable(feature = "ptr_internals", issue = "0",
27432743
reason = "use NonNull instead and consider PhantomData<T> \
27442744
(if you also use #[may_dangle]), Send, and/or Sync")]
2745-
#[allow(deprecated)]
27462745
#[doc(hidden)]
27472746
pub struct Unique<T: ?Sized> {
27482747
pointer: NonZero<*const T>,
@@ -2790,7 +2789,6 @@ impl<T: Sized> Unique<T> {
27902789
}
27912790

27922791
#[unstable(feature = "ptr_internals", issue = "0")]
2793-
#[allow(deprecated)]
27942792
impl<T: ?Sized> Unique<T> {
27952793
/// Creates a new `Unique`.
27962794
///
@@ -2855,15 +2853,13 @@ impl<T: ?Sized> fmt::Pointer for Unique<T> {
28552853
}
28562854

28572855
#[unstable(feature = "ptr_internals", issue = "0")]
2858-
#[allow(deprecated)]
28592856
impl<'a, T: ?Sized> From<&'a mut T> for Unique<T> {
28602857
fn from(reference: &'a mut T) -> Self {
28612858
Unique { pointer: NonZero(reference as _), _marker: PhantomData }
28622859
}
28632860
}
28642861

28652862
#[unstable(feature = "ptr_internals", issue = "0")]
2866-
#[allow(deprecated)]
28672863
impl<'a, T: ?Sized> From<&'a T> for Unique<T> {
28682864
fn from(reference: &'a T) -> Self {
28692865
Unique { pointer: NonZero(reference as _), _marker: PhantomData }
@@ -2896,7 +2892,7 @@ impl<'a, T: ?Sized> From<NonNull<T>> for Unique<T> {
28962892
/// provide a public API that follows the normal shared XOR mutable rules of Rust.
28972893
#[stable(feature = "nonnull", since = "1.25.0")]
28982894
pub struct NonNull<T: ?Sized> {
2899-
#[allow(deprecated)] pointer: NonZero<*const T>,
2895+
pointer: NonZero<*const T>,
29002896
}
29012897

29022898
/// `NonNull` pointers are not `Send` because the data they reference may be aliased.
@@ -2923,7 +2919,6 @@ impl<T: Sized> NonNull<T> {
29232919
}
29242920
}
29252921

2926-
#[allow(deprecated)]
29272922
impl<T: ?Sized> NonNull<T> {
29282923
/// Creates a new `NonNull`.
29292924
///
@@ -3054,15 +3049,13 @@ impl<T: ?Sized> From<Unique<T>> for NonNull<T> {
30543049
}
30553050

30563051
#[stable(feature = "nonnull", since = "1.25.0")]
3057-
#[allow(deprecated)]
30583052
impl<'a, T: ?Sized> From<&'a mut T> for NonNull<T> {
30593053
fn from(reference: &'a mut T) -> Self {
30603054
NonNull { pointer: NonZero(reference as _) }
30613055
}
30623056
}
30633057

30643058
#[stable(feature = "nonnull", since = "1.25.0")]
3065-
#[allow(deprecated)]
30663059
impl<'a, T: ?Sized> From<&'a T> for NonNull<T> {
30673060
fn from(reference: &'a T) -> Self {
30683061
NonNull { pointer: NonZero(reference as _) }

src/libcore/tests/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#![feature(iterator_step_by)]
2727
#![feature(iterator_flatten)]
2828
#![feature(iterator_repeat_with)]
29-
#![feature(nonzero)]
3029
#![feature(pattern)]
3130
#![feature(range_is_empty)]
3231
#![feature(raw)]

src/librustc/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
#![feature(never_type)]
5757
#![feature(exhaustive_patterns)]
5858
#![feature(non_exhaustive)]
59-
#![feature(nonzero)]
6059
#![feature(proc_macro_internals)]
6160
#![feature(quote)]
6261
#![feature(optin_builtin_traits)]

src/librustc_data_structures/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
html_root_url = "https://doc.rust-lang.org/nightly/")]
2222

2323
#![feature(collections_range)]
24-
#![feature(nonzero)]
2524
#![feature(unboxed_closures)]
2625
#![feature(fn_traits)]
2726
#![feature(unsize)]

src/librustc_mir/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
3030
#![feature(exhaustive_patterns)]
3131
#![feature(range_contains)]
3232
#![feature(rustc_diagnostic_macros)]
33-
#![feature(nonzero)]
3433
#![feature(inclusive_range_methods)]
3534
#![feature(crate_visibility_modifier)]
3635
#![feature(never_type)]

src/libstd/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@
277277
#![feature(needs_panic_runtime)]
278278
#![feature(never_type)]
279279
#![feature(exhaustive_patterns)]
280-
#![feature(nonzero)]
281280
#![feature(num_bits_bytes)]
282281
#![feature(old_wrapping)]
283282
#![feature(on_unimplemented)]

src/libstd/num.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ pub use core::num::{FpCategory, ParseIntError, ParseFloatError, TryFromIntError}
2121
#[stable(feature = "rust1", since = "1.0.0")]
2222
pub use core::num::Wrapping;
2323

24-
#[unstable(feature = "nonzero", issue = "49137")]
25-
#[allow(deprecated)]
26-
pub use core::num::{
27-
NonZeroU8, NonZeroI8, NonZeroU16, NonZeroI16, NonZeroU32, NonZeroI32,
28-
NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize,
29-
};
24+
#[stable(feature = "nonzero", since = "1.28.0")]
25+
pub use core::num::{NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU128, NonZeroUsize};
3026

3127
#[cfg(test)] use fmt;
3228
#[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem};

src/test/run-pass/ctfe/tuple-struct-constructors.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010

1111
// https://github.com/rust-lang/rust/issues/41898
1212

13-
#![feature(nonzero, const_fn)]
14-
extern crate core;
15-
use core::nonzero::NonZero;
13+
use std::num::NonZeroU64;
1614

1715
fn main() {
18-
const FOO: NonZero<u64> = unsafe { NonZero::new_unchecked(2) };
16+
const FOO: NonZeroU64 = unsafe { NonZeroU64::new_unchecked(2) };
1917
if let FOO = FOO {}
2018
}

src/test/run-pass/enum-null-pointer-opt.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(nonzero, core)]
12-
1311
use std::mem::size_of;
1412
use std::num::NonZeroUsize;
1513
use std::ptr::NonNull;

src/test/ui/print_type_sizes/niche-filling.rs

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
// padding and overall computed sizes can be quite different.
2323

2424
#![feature(start)]
25-
#![feature(nonzero)]
2625
#![allow(dead_code)]
2726

2827
use std::num::NonZeroU32;

0 commit comments

Comments
 (0)