Skip to content

Commit 96547fa

Browse files
committed
Switch NonZero alias direction.
1 parent 086d887 commit 96547fa

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

library/core/src/num/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ pub use dec2flt::ParseFloatError;
5959
#[stable(feature = "rust1", since = "1.0.0")]
6060
pub use error::ParseIntError;
6161

62-
pub(crate) use nonzero::NonZero;
62+
#[unstable(feature = "generic_nonzero", issue = "82363")]
63+
pub use nonzero::NonZero;
6364

6465
#[stable(feature = "nonzero", since = "1.28.0")]
6566
pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};

library/core/src/num/nonzero.rs

+28-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::cmp::Ordering;
44
use crate::fmt;
55
use crate::hash::{Hash, Hasher};
6-
use crate::marker::StructuralPartialEq;
6+
use crate::marker::{StructuralEq, StructuralPartialEq};
77
use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem};
88
use crate::str::FromStr;
99

@@ -67,12 +67,23 @@ impl_zeroable_primitive!(NonZeroI64(i64));
6767
impl_zeroable_primitive!(NonZeroI128(i128));
6868
impl_zeroable_primitive!(NonZeroIsize(isize));
6969

70-
#[unstable(
71-
feature = "nonzero_internals",
72-
reason = "implementation detail which may disappear or be replaced at any time",
73-
issue = "none"
74-
)]
75-
pub(crate) type NonZero<T> = <T as ZeroablePrimitive>::NonZero;
70+
/// A value that is known not to equal zero.
71+
///
72+
/// This enables some memory layout optimization.
73+
/// For example, `Option<NonZero<u32>>` is the same size as `u32`:
74+
///
75+
/// ```rust
76+
/// #![feature(generic_nonzero)]
77+
///
78+
/// use core::mem::size_of;
79+
/// assert_eq!(size_of::<Option<core::num::NonZero<u32>>>(), size_of::<u32>());
80+
/// ```
81+
#[unstable(feature = "generic_nonzero", issue = "82363")]
82+
#[repr(transparent)]
83+
#[rustc_layout_scalar_valid_range_start(1)]
84+
#[rustc_nonnull_optimization_guaranteed]
85+
#[rustc_diagnostic_item = "NonZero"]
86+
pub struct NonZero<T: ZeroablePrimitive>(T);
7687

7788
macro_rules! impl_nonzero_traits {
7889
(#[$stability:meta] $Ty:ty) => {
@@ -85,6 +96,9 @@ macro_rules! impl_nonzero_traits {
8596
}
8697
}
8798

99+
#[$stability]
100+
impl Copy for $Ty {}
101+
88102
#[$stability]
89103
impl PartialEq for $Ty {
90104
#[inline]
@@ -101,6 +115,12 @@ macro_rules! impl_nonzero_traits {
101115
#[unstable(feature = "structural_match", issue = "31434")]
102116
impl StructuralPartialEq for $Ty {}
103117

118+
#[$stability]
119+
impl Eq for $Ty {}
120+
121+
#[unstable(feature = "structural_match", issue = "31434")]
122+
impl StructuralEq for $Ty {}
123+
104124
#[$stability]
105125
impl PartialOrd for $Ty {
106126
#[inline]
@@ -225,12 +245,7 @@ macro_rules! nonzero_integer {
225245
///
226246
/// [null pointer optimization]: crate::option#representation
227247
#[$stability]
228-
#[derive(Copy, Eq)]
229-
#[repr(transparent)]
230-
#[rustc_layout_scalar_valid_range_start(1)]
231-
#[rustc_nonnull_optimization_guaranteed]
232-
#[rustc_diagnostic_item = stringify!($Ty)]
233-
pub struct $Ty($Int);
248+
pub type $Ty = NonZero<$Int>;
234249

235250
impl_nonzero_traits!(#[$stability] $Ty);
236251

0 commit comments

Comments
 (0)