Skip to content

Commit 8b4c75b

Browse files
committed
Switch NonZero alias direction.
1 parent 0e3035b commit 8b4c75b

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

library/core/src/num/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,15 @@ 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(
63+
feature = "nonzero_internals",
64+
reason = "implementation detail which may disappear or be replaced at any time",
65+
issue = "none"
66+
)]
67+
pub use nonzero::ZeroablePrimitive;
68+
69+
#[unstable(feature = "generic_nonzero", issue = "82363")]
70+
pub use nonzero::NonZero;
6371

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

library/core/src/num/nonzero.rs

+30-19
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

@@ -30,9 +30,7 @@ mod private {
3030
issue = "none"
3131
)]
3232
#[const_trait]
33-
pub trait ZeroablePrimitive: Sized + Copy + private::Sealed {
34-
type NonZero;
35-
}
33+
pub trait ZeroablePrimitive: Sized + Copy + private::Sealed {}
3634

3735
macro_rules! impl_zeroable_primitive {
3836
($NonZero:ident ( $primitive:ty )) => {
@@ -48,9 +46,7 @@ macro_rules! impl_zeroable_primitive {
4846
reason = "implementation detail which may disappear or be replaced at any time",
4947
issue = "none"
5048
)]
51-
impl const ZeroablePrimitive for $primitive {
52-
type NonZero = $NonZero;
53-
}
49+
impl const ZeroablePrimitive for $primitive {}
5450
};
5551
}
5652

@@ -67,12 +63,23 @@ impl_zeroable_primitive!(NonZeroI64(i64));
6763
impl_zeroable_primitive!(NonZeroI128(i128));
6864
impl_zeroable_primitive!(NonZeroIsize(isize));
6965

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;
66+
/// A value that is known not to equal zero.
67+
///
68+
/// This enables some memory layout optimization.
69+
/// For example, `Option<NonZero<u32>>` is the same size as `u32`:
70+
///
71+
/// ```
72+
/// #![feature(generic_nonzero)]
73+
/// use core::mem::size_of;
74+
///
75+
/// assert_eq!(size_of::<Option<core::num::NonZero<u32>>>(), size_of::<u32>());
76+
/// ```
77+
#[unstable(feature = "generic_nonzero", issue = "82363")]
78+
#[repr(transparent)]
79+
#[rustc_layout_scalar_valid_range_start(1)]
80+
#[rustc_nonnull_optimization_guaranteed]
81+
#[rustc_diagnostic_item = "NonZero"]
82+
pub struct NonZero<T: ZeroablePrimitive>(T);
7683

7784
macro_rules! impl_nonzero_fmt {
7885
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
@@ -131,12 +138,7 @@ macro_rules! nonzero_integer {
131138
///
132139
/// [null pointer optimization]: crate::option#representation
133140
#[$stability]
134-
#[derive(Copy, Eq)]
135-
#[repr(transparent)]
136-
#[rustc_layout_scalar_valid_range_start(1)]
137-
#[rustc_nonnull_optimization_guaranteed]
138-
#[rustc_diagnostic_item = stringify!($Ty)]
139-
pub struct $Ty($Int);
141+
pub type $Ty = NonZero<$Int>;
140142

141143
impl $Ty {
142144
/// Creates a non-zero without checking whether the value is non-zero.
@@ -506,6 +508,9 @@ macro_rules! nonzero_integer {
506508
}
507509
}
508510

511+
#[$stability]
512+
impl Copy for $Ty {}
513+
509514
#[$stability]
510515
impl PartialEq for $Ty {
511516
#[inline]
@@ -522,6 +527,12 @@ macro_rules! nonzero_integer {
522527
#[unstable(feature = "structural_match", issue = "31434")]
523528
impl StructuralPartialEq for $Ty {}
524529

530+
#[$stability]
531+
impl Eq for $Ty {}
532+
533+
#[unstable(feature = "structural_match", issue = "31434")]
534+
impl StructuralEq for $Ty {}
535+
525536
#[$stability]
526537
impl PartialOrd for $Ty {
527538
#[inline]

library/std/src/num.rs

+10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ pub use core::num::Wrapping;
1616
#[stable(feature = "rust1", since = "1.0.0")]
1717
pub use core::num::{FpCategory, ParseFloatError, ParseIntError, TryFromIntError};
1818

19+
#[unstable(
20+
feature = "nonzero_internals",
21+
reason = "implementation detail which may disappear or be replaced at any time",
22+
issue = "none"
23+
)]
24+
pub use core::num::ZeroablePrimitive;
25+
26+
#[unstable(feature = "generic_nonzero", issue = "82363")]
27+
pub use core::num::NonZero;
28+
1929
#[stable(feature = "signed_nonzero", since = "1.34.0")]
2030
pub use core::num::{NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize};
2131
#[stable(feature = "nonzero", since = "1.28.0")]

0 commit comments

Comments
 (0)