Skip to content

Commit d0afa6d

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

File tree

5 files changed

+98
-31
lines changed

5 files changed

+98
-31
lines changed

library/core/src/ffi/mod.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ macro_rules! type_alias {
5454
type_alias! { "c_char.md", c_char = c_char_definition::c_char, NonZero_c_char = c_char_definition::NonZero_c_char;
5555
#[doc(cfg(all()))] }
5656

57-
type_alias! { "c_schar.md", c_schar = i8, NonZero_c_schar = NonZeroI8; }
58-
type_alias! { "c_uchar.md", c_uchar = u8, NonZero_c_uchar = NonZeroU8; }
59-
type_alias! { "c_short.md", c_short = i16, NonZero_c_short = NonZeroI16; }
60-
type_alias! { "c_ushort.md", c_ushort = u16, NonZero_c_ushort = NonZeroU16; }
57+
type_alias! { "c_schar.md", c_schar = i8, NonZero_c_schar = NonZero<i8>; }
58+
type_alias! { "c_uchar.md", c_uchar = u8, NonZero_c_uchar = NonZero<u8>; }
59+
type_alias! { "c_short.md", c_short = i16, NonZero_c_short = NonZero<i16>; }
60+
type_alias! { "c_ushort.md", c_ushort = u16, NonZero_c_ushort = NonZero<u16>; }
6161

6262
type_alias! { "c_int.md", c_int = c_int_definition::c_int, NonZero_c_int = c_int_definition::NonZero_c_int;
6363
#[doc(cfg(all()))] }
@@ -69,8 +69,8 @@ type_alias! { "c_long.md", c_long = c_long_definition::c_long, NonZero_c_long =
6969
type_alias! { "c_ulong.md", c_ulong = c_long_definition::c_ulong, NonZero_c_ulong = c_long_definition::NonZero_c_ulong;
7070
#[doc(cfg(all()))] }
7171

72-
type_alias! { "c_longlong.md", c_longlong = i64, NonZero_c_longlong = NonZeroI64; }
73-
type_alias! { "c_ulonglong.md", c_ulonglong = u64, NonZero_c_ulonglong = NonZeroU64; }
72+
type_alias! { "c_longlong.md", c_longlong = i64, NonZero_c_longlong = NonZero<i64>; }
73+
type_alias! { "c_ulonglong.md", c_ulonglong = u64, NonZero_c_ulonglong = NonZero<u64>; }
7474

7575
type_alias_no_nz! { "c_float.md", c_float = f32; }
7676
type_alias_no_nz! { "c_double.md", c_double = f64; }
@@ -152,11 +152,11 @@ mod c_char_definition {
152152
target_os = "horizon"
153153
))] {
154154
pub type c_char = u8;
155-
pub type NonZero_c_char = crate::num::NonZeroU8;
155+
pub type NonZero_c_char = crate::num::NonZero<u8>;
156156
} else {
157157
// On every other target, c_char is signed.
158158
pub type c_char = i8;
159-
pub type NonZero_c_char = crate::num::NonZeroI8;
159+
pub type NonZero_c_char = crate::num::NonZero<i8>;
160160
}
161161
}
162162
}
@@ -165,14 +165,14 @@ mod c_int_definition {
165165
cfg_if! {
166166
if #[cfg(any(target_arch = "avr", target_arch = "msp430"))] {
167167
pub type c_int = i16;
168-
pub type NonZero_c_int = crate::num::NonZeroI16;
168+
pub type NonZero_c_int = crate::num::NonZero<i16>;
169169
pub type c_uint = u16;
170-
pub type NonZero_c_uint = crate::num::NonZeroU16;
170+
pub type NonZero_c_uint = crate::num::NonZero<u16>;
171171
} else {
172172
pub type c_int = i32;
173-
pub type NonZero_c_int = crate::num::NonZeroI32;
173+
pub type NonZero_c_int = crate::num::NonZero<i32>;
174174
pub type c_uint = u32;
175-
pub type NonZero_c_uint = crate::num::NonZeroU32;
175+
pub type NonZero_c_uint = crate::num::NonZero<u32>;
176176
}
177177
}
178178
}
@@ -181,15 +181,15 @@ mod c_long_definition {
181181
cfg_if! {
182182
if #[cfg(all(target_pointer_width = "64", not(windows)))] {
183183
pub type c_long = i64;
184-
pub type NonZero_c_long = crate::num::NonZeroI64;
184+
pub type NonZero_c_long = crate::num::NonZero<i64>;
185185
pub type c_ulong = u64;
186-
pub type NonZero_c_ulong = crate::num::NonZeroU64;
186+
pub type NonZero_c_ulong = crate::num::NonZero<u64>;
187187
} else {
188188
// The minimal size of `long` in the C standard is 32 bits
189189
pub type c_long = i32;
190-
pub type NonZero_c_long = crate::num::NonZeroI32;
190+
pub type NonZero_c_long = crate::num::NonZero<i32>;
191191
pub type c_ulong = u32;
192-
pub type NonZero_c_ulong = crate::num::NonZeroU32;
192+
pub type NonZero_c_ulong = crate::num::NonZero<u32>;
193193
}
194194
}
195195
}

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)