Skip to content

Commit 988772d

Browse files
committed
Add private NonZero<T> type alias.
1 parent e6ce562 commit 988772d

File tree

6 files changed

+97
-29
lines changed

6 files changed

+97
-29
lines changed

library/core/src/convert/num.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use super::{From, TryFrom};
21
use crate::num::TryFromIntError;
32

3+
use super::{From, TryFrom};
4+
45
mod private {
56
/// This trait being unreachable from outside the crate
67
/// prevents other implementations of the `FloatToInt` trait,

library/core/src/ffi/mod.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ type_alias! { "c_char.md", c_char = c_char_definition::c_char, NonZero_c_char =
5959
#[cfg(all())]
6060
#[doc(cfg(all()))] }
6161

62-
type_alias! { "c_schar.md", c_schar = i8, NonZero_c_schar = NonZeroI8; }
63-
type_alias! { "c_uchar.md", c_uchar = u8, NonZero_c_uchar = NonZeroU8; }
64-
type_alias! { "c_short.md", c_short = i16, NonZero_c_short = NonZeroI16; }
65-
type_alias! { "c_ushort.md", c_ushort = u16, NonZero_c_ushort = NonZeroU16; }
62+
type_alias! { "c_schar.md", c_schar = i8, NonZero_c_schar = NonZero<i8>; }
63+
type_alias! { "c_uchar.md", c_uchar = u8, NonZero_c_uchar = NonZero<u8>; }
64+
type_alias! { "c_short.md", c_short = i16, NonZero_c_short = NonZero<i16>; }
65+
type_alias! { "c_ushort.md", c_ushort = u16, NonZero_c_ushort = NonZero<u16>; }
6666

6767
type_alias! { "c_int.md", c_int = c_int_definition::c_int, NonZero_c_int = c_int_definition::NonZero_c_int;
6868
#[doc(cfg(all()))] }
@@ -74,8 +74,8 @@ type_alias! { "c_long.md", c_long = c_long_definition::c_long, NonZero_c_long =
7474
type_alias! { "c_ulong.md", c_ulong = c_long_definition::c_ulong, NonZero_c_ulong = c_long_definition::NonZero_c_ulong;
7575
#[doc(cfg(all()))] }
7676

77-
type_alias! { "c_longlong.md", c_longlong = i64, NonZero_c_longlong = NonZeroI64; }
78-
type_alias! { "c_ulonglong.md", c_ulonglong = u64, NonZero_c_ulonglong = NonZeroU64; }
77+
type_alias! { "c_longlong.md", c_longlong = i64, NonZero_c_longlong = NonZero<i64>; }
78+
type_alias! { "c_ulonglong.md", c_ulonglong = u64, NonZero_c_ulonglong = NonZero<u64>; }
7979

8080
type_alias_no_nz! { "c_float.md", c_float = f32; }
8181
type_alias_no_nz! { "c_double.md", c_double = f64; }
@@ -147,11 +147,11 @@ mod c_char_definition {
147147
target_os = "horizon"
148148
))] {
149149
pub type c_char = u8;
150-
pub type NonZero_c_char = crate::num::NonZeroU8;
150+
pub type NonZero_c_char = crate::num::NonZero<u8>;
151151
} else {
152152
// On every other target, c_char is signed.
153153
pub type c_char = i8;
154-
pub type NonZero_c_char = crate::num::NonZeroI8;
154+
pub type NonZero_c_char = crate::num::NonZero<i8>;
155155
}
156156
}
157157
}
@@ -160,14 +160,14 @@ mod c_int_definition {
160160
cfg_if! {
161161
if #[cfg(any(target_arch = "avr", target_arch = "msp430"))] {
162162
pub type c_int = i16;
163-
pub type NonZero_c_int = crate::num::NonZeroI16;
163+
pub type NonZero_c_int = crate::num::NonZero<i16>;
164164
pub type c_uint = u16;
165-
pub type NonZero_c_uint = crate::num::NonZeroU16;
165+
pub type NonZero_c_uint = crate::num::NonZero<u16>;
166166
} else {
167167
pub type c_int = i32;
168-
pub type NonZero_c_int = crate::num::NonZeroI32;
168+
pub type NonZero_c_int = crate::num::NonZero<i32>;
169169
pub type c_uint = u32;
170-
pub type NonZero_c_uint = crate::num::NonZeroU32;
170+
pub type NonZero_c_uint = crate::num::NonZero<u32>;
171171
}
172172
}
173173
}
@@ -176,15 +176,15 @@ mod c_long_definition {
176176
cfg_if! {
177177
if #[cfg(all(target_pointer_width = "64", not(windows)))] {
178178
pub type c_long = i64;
179-
pub type NonZero_c_long = crate::num::NonZeroI64;
179+
pub type NonZero_c_long = crate::num::NonZero<i64>;
180180
pub type c_ulong = u64;
181-
pub type NonZero_c_ulong = crate::num::NonZeroU64;
181+
pub type NonZero_c_ulong = crate::num::NonZero<u64>;
182182
} else {
183183
// The minimal size of `long` in the C standard is 32 bits
184184
pub type c_long = i32;
185-
pub type NonZero_c_long = crate::num::NonZeroI32;
185+
pub type NonZero_c_long = crate::num::NonZero<i32>;
186186
pub type c_ulong = u32;
187-
pub type NonZero_c_ulong = crate::num::NonZeroU32;
187+
pub type NonZero_c_ulong = crate::num::NonZero<u32>;
188188
}
189189
}
190190
}

library/core/src/num/mod.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ impl Error for ParseFloatError {
7070
#[stable(feature = "rust1", since = "1.0.0")]
7171
pub use error::ParseIntError;
7272

73+
pub(crate) use nonzero::NonZero;
74+
7375
#[stable(feature = "nonzero", since = "1.28.0")]
7476
pub use nonzero::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
7577

@@ -299,7 +301,7 @@ impl isize {
299301
const ASCII_CASE_MASK: u8 = 0b0010_0000;
300302

301303
impl u8 {
302-
uint_impl! { u8, u8, i8, NonZeroU8, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
304+
uint_impl! { u8, u8, i8, NonZero<u8>, 8, 255, 2, "0x82", "0xa", "0x12", "0x12", "0x48", "[0x12]",
303305
"[0x12]", "", "", "" }
304306
widening_impl! { u8, u16, 8, unsigned }
305307

@@ -884,7 +886,7 @@ impl u8 {
884886
}
885887

886888
impl u16 {
887-
uint_impl! { u16, u16, i16, NonZeroU16, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
889+
uint_impl! { u16, u16, i16, NonZero<u16>, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
888890
"[0x34, 0x12]", "[0x12, 0x34]", "", "", "" }
889891
widening_impl! { u16, u32, 16, unsigned }
890892

@@ -915,13 +917,13 @@ impl u16 {
915917
}
916918

917919
impl u32 {
918-
uint_impl! { u32, u32, i32, NonZeroU32, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
920+
uint_impl! { u32, u32, i32, NonZero<u32>, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
919921
"0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]", "", "", "" }
920922
widening_impl! { u32, u64, 32, unsigned }
921923
}
922924

923925
impl u64 {
924-
uint_impl! { u64, u64, i64, NonZeroU64, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
926+
uint_impl! { u64, u64, i64, NonZero<u64>, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
925927
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
926928
"[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
927929
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",
@@ -930,7 +932,7 @@ impl u64 {
930932
}
931933

932934
impl u128 {
933-
uint_impl! { u128, u128, i128, NonZeroU128, 128, 340282366920938463463374607431768211455, 16,
935+
uint_impl! { u128, u128, i128, NonZero<u128>, 128, 340282366920938463463374607431768211455, 16,
934936
"0x13f40000000000000000000000004f76", "0x4f7613f4", "0x12345678901234567890123456789012",
935937
"0x12907856341290785634129078563412", "0x48091e6a2c48091e6a2c48091e6a2c48",
936938
"[0x12, 0x90, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, \
@@ -942,15 +944,15 @@ impl u128 {
942944

943945
#[cfg(target_pointer_width = "16")]
944946
impl usize {
945-
uint_impl! { usize, u16, isize, NonZeroUsize, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
947+
uint_impl! { usize, u16, isize, NonZero<usize>, 16, 65535, 4, "0xa003", "0x3a", "0x1234", "0x3412", "0x2c48",
946948
"[0x34, 0x12]", "[0x12, 0x34]",
947949
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
948950
" on 16-bit targets" }
949951
widening_impl! { usize, u32, 16, unsigned }
950952
}
951953
#[cfg(target_pointer_width = "32")]
952954
impl usize {
953-
uint_impl! { usize, u32, isize, NonZeroUsize, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
955+
uint_impl! { usize, u32, isize, NonZero<usize>, 32, 4294967295, 8, "0x10000b3", "0xb301", "0x12345678",
954956
"0x78563412", "0x1e6a2c48", "[0x78, 0x56, 0x34, 0x12]", "[0x12, 0x34, 0x56, 0x78]",
955957
usize_isize_to_xe_bytes_doc!(), usize_isize_from_xe_bytes_doc!(),
956958
" on 32-bit targets" }
@@ -959,7 +961,7 @@ impl usize {
959961

960962
#[cfg(target_pointer_width = "64")]
961963
impl usize {
962-
uint_impl! { usize, u64, isize, NonZeroUsize, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
964+
uint_impl! { usize, u64, isize, NonZero<usize>, 64, 18446744073709551615, 12, "0xaa00000000006e1", "0x6e10aa",
963965
"0x1234567890123456", "0x5634129078563412", "0x6a2c48091e6a2c48",
964966
"[0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x34, 0x12]",
965967
"[0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56]",

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
@@ -1,5 +1,5 @@
11
macro_rules! uint_impl {
2-
($SelfT:ty, $ActualT:ident, $SignedT:ident, $NonZeroT:ident,
2+
($SelfT:ty, $ActualT:ident, $SignedT:ident, $NonZeroT:ty,
33
$BITS:expr, $MaxV:expr,
44
$rot:expr, $rot_op:expr, $rot_result:expr, $swap_op:expr, $swapped:expr,
55
$reversed:expr, $le_bytes:expr, $be_bytes:expr,
@@ -808,6 +808,7 @@ macro_rules! uint_impl {
808808
without modifying the original"]
809809
#[inline]
810810
pub const fn checked_ilog2(self) -> Option<u32> {
811+
// FIXME: Simply use `NonZero::new` once it is actually generic.
811812
if let Some(x) = <$NonZeroT>::new(self) {
812813
Some(x.ilog2())
813814
} else {
@@ -830,6 +831,7 @@ macro_rules! uint_impl {
830831
without modifying the original"]
831832
#[inline]
832833
pub const fn checked_ilog10(self) -> Option<u32> {
834+
// FIXME: Simply use `NonZero::new` once it is actually generic.
833835
if let Some(x) = <$NonZeroT>::new(self) {
834836
Some(x.ilog10())
835837
} else {

library/core/src/slice/iter.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::intrinsics::{assume, exact_div, unchecked_sub};
1010
use crate::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
1111
use crate::marker::{PhantomData, Send, Sized, Sync};
1212
use crate::mem::{self, SizedTypeProperties};
13-
use crate::num::NonZeroUsize;
13+
use crate::num::NonZero;
1414
use crate::ptr::NonNull;
1515

1616
use super::{from_raw_parts, from_raw_parts_mut};
@@ -1297,12 +1297,12 @@ forward_iterator! { RSplitNMut: T, &'a mut [T] }
12971297
#[must_use = "iterators are lazy and do nothing unless consumed"]
12981298
pub struct Windows<'a, T: 'a> {
12991299
v: &'a [T],
1300-
size: NonZeroUsize,
1300+
size: NonZero<usize>,
13011301
}
13021302

13031303
impl<'a, T: 'a> Windows<'a, T> {
13041304
#[inline]
1305-
pub(super) fn new(slice: &'a [T], size: NonZeroUsize) -> Self {
1305+
pub(super) fn new(slice: &'a [T], size: NonZero<usize>) -> Self {
13061306
Self { v: slice, size }
13071307
}
13081308
}

0 commit comments

Comments
 (0)