Skip to content

Commit f760223

Browse files
committed
Add private NonZero<T> type alias.
1 parent 73252d5 commit f760223

File tree

4 files changed

+82
-15
lines changed

4 files changed

+82
-15
lines changed

library/core/src/num/mod.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -59,6 +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;
63+
6264
#[stable(feature = "nonzero", since = "1.28.0")]
6365
pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
6466

@@ -482,7 +484,7 @@ impl u8 {
482484
Self = u8,
483485
ActualT = u8,
484486
SignedT = i8,
485-
NonZeroT = NonZeroU8,
487+
NonZeroT = NonZero<u8>,
486488
BITS = 8,
487489
MAX = 255,
488490
rot = 2,
@@ -791,7 +793,7 @@ impl u8 {
791793
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
792794
#[inline]
793795
pub const fn is_ascii_alphanumeric(&self) -> bool {
794-
matches!(*self, b'0'..=b'9') | matches!(*self, b'A'..=b'Z') | matches!(*self, b'a'..=b'z')
796+
matches!(*self, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z')
795797
}
796798

797799
/// Checks if the value is an ASCII decimal digit:
@@ -1097,7 +1099,7 @@ impl u16 {
10971099
Self = u16,
10981100
ActualT = u16,
10991101
SignedT = i16,
1100-
NonZeroT = NonZeroU16,
1102+
NonZeroT = NonZero<u16>,
11011103
BITS = 16,
11021104
MAX = 65535,
11031105
rot = 4,
@@ -1146,7 +1148,7 @@ impl u32 {
11461148
Self = u32,
11471149
ActualT = u32,
11481150
SignedT = i32,
1149-
NonZeroT = NonZeroU32,
1151+
NonZeroT = NonZero<u32>,
11501152
BITS = 32,
11511153
MAX = 4294967295,
11521154
rot = 8,
@@ -1170,7 +1172,7 @@ impl u64 {
11701172
Self = u64,
11711173
ActualT = u64,
11721174
SignedT = i64,
1173-
NonZeroT = NonZeroU64,
1175+
NonZeroT = NonZero<u64>,
11741176
BITS = 64,
11751177
MAX = 18446744073709551615,
11761178
rot = 12,
@@ -1194,7 +1196,7 @@ impl u128 {
11941196
Self = u128,
11951197
ActualT = u128,
11961198
SignedT = i128,
1197-
NonZeroT = NonZeroU128,
1199+
NonZeroT = NonZero<u128>,
11981200
BITS = 128,
11991201
MAX = 340282366920938463463374607431768211455,
12001202
rot = 16,
@@ -1204,9 +1206,9 @@ impl u128 {
12041206
swapped = "0x12907856341290785634129078563412",
12051207
reversed = "0x48091e6a2c48091e6a2c48091e6a2c48",
12061208
le_bytes = "[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
1207-
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
1209+
0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
12081210
be_bytes = "[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, \
1209-
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
1211+
0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12]",
12101212
to_xe_bytes_doc = "",
12111213
from_xe_bytes_doc = "",
12121214
bound_condition = "",
@@ -1220,7 +1222,7 @@ impl usize {
12201222
Self = usize,
12211223
ActualT = u16,
12221224
SignedT = isize,
1223-
NonZeroT = NonZeroUsize,
1225+
NonZeroT = NonZero<usize>,
12241226
BITS = 16,
12251227
MAX = 65535,
12261228
rot = 4,
@@ -1245,7 +1247,7 @@ impl usize {
12451247
Self = usize,
12461248
ActualT = u32,
12471249
SignedT = isize,
1248-
NonZeroT = NonZeroUsize,
1250+
NonZeroT = NonZero<usize>,
12491251
BITS = 32,
12501252
MAX = 4294967295,
12511253
rot = 8,
@@ -1270,7 +1272,7 @@ impl usize {
12701272
Self = usize,
12711273
ActualT = u64,
12721274
SignedT = isize,
1273-
NonZeroT = NonZeroUsize,
1275+
NonZeroT = NonZero<usize>,
12741276
BITS = 64,
12751277
MAX = 18446744073709551615,
12761278
rot = 12,

library/core/src/num/nonzero.rs

+63
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,69 @@ use super::from_str_radix;
88
use super::{IntErrorKind, ParseIntError};
99
use crate::intrinsics;
1010

11+
mod private {
12+
#[unstable(
13+
feature = "nonzero_internals",
14+
reason = "implementation detail which may disappear or be replaced at any time",
15+
issue = "none"
16+
)]
17+
#[const_trait]
18+
pub trait Sealed {}
19+
}
20+
21+
/// A marker trait for primitive types which can be zero.
22+
///
23+
/// This is an implementation detail for [`NonZero<T>`](NonZero) which may disappear or be replaced at any time.
24+
#[unstable(
25+
feature = "nonzero_internals",
26+
reason = "implementation detail which may disappear or be replaced at any time",
27+
issue = "none"
28+
)]
29+
#[const_trait]
30+
pub trait ZeroablePrimitive: Sized + Copy + private::Sealed {
31+
type NonZero;
32+
}
33+
34+
#[unstable(
35+
feature = "nonzero_internals",
36+
reason = "implementation detail which may disappear or be replaced at any time",
37+
issue = "none"
38+
)]
39+
pub(crate) type NonZero<T> = <T as ZeroablePrimitive>::NonZero;
40+
41+
macro_rules! impl_zeroable_primitive {
42+
($NonZero:ident ( $primitive:ty )) => {
43+
#[unstable(
44+
feature = "nonzero_internals",
45+
reason = "implementation detail which may disappear or be replaced at any time",
46+
issue = "none"
47+
)]
48+
impl const private::Sealed for $primitive {}
49+
50+
#[unstable(
51+
feature = "nonzero_internals",
52+
reason = "implementation detail which may disappear or be replaced at any time",
53+
issue = "none"
54+
)]
55+
impl const ZeroablePrimitive for $primitive {
56+
type NonZero = $NonZero;
57+
}
58+
};
59+
}
60+
61+
impl_zeroable_primitive!(NonZeroU8(u8));
62+
impl_zeroable_primitive!(NonZeroU16(u16));
63+
impl_zeroable_primitive!(NonZeroU32(u32));
64+
impl_zeroable_primitive!(NonZeroU64(u64));
65+
impl_zeroable_primitive!(NonZeroU128(u128));
66+
impl_zeroable_primitive!(NonZeroUsize(usize));
67+
impl_zeroable_primitive!(NonZeroI8(i8));
68+
impl_zeroable_primitive!(NonZeroI16(i16));
69+
impl_zeroable_primitive!(NonZeroI32(i32));
70+
impl_zeroable_primitive!(NonZeroI64(i64));
71+
impl_zeroable_primitive!(NonZeroI128(i128));
72+
impl_zeroable_primitive!(NonZeroIsize(isize));
73+
1174
macro_rules! impl_nonzero_fmt {
1275
( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
1376
$(

library/core/src/num/uint_macros.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ macro_rules! uint_impl {
33
Self = $SelfT:ty,
44
ActualT = $ActualT:ident,
55
SignedT = $SignedT:ident,
6-
NonZeroT = $NonZeroT:ident,
6+
NonZeroT = $NonZeroT:ty,
77

88
// There are all for use *only* in doc comments.
99
// As such, they're all passed as literals -- passing them as a string
@@ -842,6 +842,7 @@ macro_rules! uint_impl {
842842
without modifying the original"]
843843
#[inline]
844844
pub const fn checked_ilog2(self) -> Option<u32> {
845+
// FIXME: Simply use `NonZero::new` once it is actually generic.
845846
if let Some(x) = <$NonZeroT>::new(self) {
846847
Some(x.ilog2())
847848
} else {
@@ -864,6 +865,7 @@ macro_rules! uint_impl {
864865
without modifying the original"]
865866
#[inline]
866867
pub const fn checked_ilog10(self) -> Option<u32> {
868+
// FIXME: Simply use `NonZero::new` once it is actually generic.
867869
if let Some(x) = <$NonZeroT>::new(self) {
868870
Some(x.ilog10())
869871
} else {

library/core/src/slice/iter.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::iter::{
1212
};
1313
use crate::marker::PhantomData;
1414
use crate::mem::{self, SizedTypeProperties};
15-
use crate::num::NonZeroUsize;
15+
use crate::num::{NonZero, NonZeroUsize};
1616
use crate::ptr::{self, invalid, invalid_mut, NonNull};
1717

1818
use super::{from_raw_parts, from_raw_parts_mut};
@@ -1305,12 +1305,12 @@ forward_iterator! { RSplitNMut: T, &'a mut [T] }
13051305
#[must_use = "iterators are lazy and do nothing unless consumed"]
13061306
pub struct Windows<'a, T: 'a> {
13071307
v: &'a [T],
1308-
size: NonZeroUsize,
1308+
size: NonZero<usize>,
13091309
}
13101310

13111311
impl<'a, T: 'a> Windows<'a, T> {
13121312
#[inline]
1313-
pub(super) fn new(slice: &'a [T], size: NonZeroUsize) -> Self {
1313+
pub(super) fn new(slice: &'a [T], size: NonZero<usize>) -> Self {
13141314
Self { v: slice, size }
13151315
}
13161316
}

0 commit comments

Comments
 (0)