|
1 | 1 | //! Definitions of integer that is known not to equal zero.
|
2 | 2 |
|
| 3 | +use crate::cmp::Ordering; |
3 | 4 | use crate::fmt;
|
| 5 | +use crate::hash::{Hash, Hasher}; |
| 6 | +use crate::marker::StructuralPartialEq; |
4 | 7 | use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem};
|
5 | 8 | use crate::str::FromStr;
|
6 | 9 |
|
@@ -31,13 +34,6 @@ pub trait ZeroablePrimitive: Sized + Copy + private::Sealed {
|
31 | 34 | type NonZero;
|
32 | 35 | }
|
33 | 36 |
|
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 | 37 | macro_rules! impl_zeroable_primitive {
|
42 | 38 | ($NonZero:ident ( $primitive:ty )) => {
|
43 | 39 | #[unstable(
|
@@ -71,6 +67,107 @@ impl_zeroable_primitive!(NonZeroI64(i64));
|
71 | 67 | impl_zeroable_primitive!(NonZeroI128(i128));
|
72 | 68 | impl_zeroable_primitive!(NonZeroIsize(isize));
|
73 | 69 |
|
| 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; |
| 76 | + |
| 77 | +macro_rules! impl_nonzero_traits { |
| 78 | + (#[$stability:meta] $Ty:ty) => { |
| 79 | + #[$stability] |
| 80 | + impl Clone for $Ty { |
| 81 | + #[inline] |
| 82 | + fn clone(&self) -> Self { |
| 83 | + // SAFETY: The contained value is non-zero. |
| 84 | + unsafe { Self(self.get()) } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + #[$stability] |
| 89 | + impl PartialEq for $Ty { |
| 90 | + #[inline] |
| 91 | + fn eq(&self, other: &Self) -> bool { |
| 92 | + self.get() == other.get() |
| 93 | + } |
| 94 | + |
| 95 | + #[inline] |
| 96 | + fn ne(&self, other: &Self) -> bool { |
| 97 | + self.get() != other.get() |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + #[unstable(feature = "structural_match", issue = "31434")] |
| 102 | + impl StructuralPartialEq for $Ty {} |
| 103 | + |
| 104 | + #[$stability] |
| 105 | + impl PartialOrd for $Ty { |
| 106 | + #[inline] |
| 107 | + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 108 | + self.get().partial_cmp(&other.get()) |
| 109 | + } |
| 110 | + |
| 111 | + #[inline] |
| 112 | + fn lt(&self, other: &Self) -> bool { |
| 113 | + self.get() < other.get() |
| 114 | + } |
| 115 | + |
| 116 | + #[inline] |
| 117 | + fn le(&self, other: &Self) -> bool { |
| 118 | + self.get() <= other.get() |
| 119 | + } |
| 120 | + |
| 121 | + #[inline] |
| 122 | + fn gt(&self, other: &Self) -> bool { |
| 123 | + self.get() > other.get() |
| 124 | + } |
| 125 | + |
| 126 | + #[inline] |
| 127 | + fn ge(&self, other: &Self) -> bool { |
| 128 | + self.get() >= other.get() |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + #[$stability] |
| 133 | + impl Ord for $Ty { |
| 134 | + #[inline] |
| 135 | + fn cmp(&self, other: &Self) -> Ordering { |
| 136 | + self.get().cmp(&other.get()) |
| 137 | + } |
| 138 | + |
| 139 | + #[inline] |
| 140 | + fn max(self, other: Self) -> Self { |
| 141 | + // SAFETY: The maximum of two non-zero values is still non-zero. |
| 142 | + unsafe { Self(self.get().max(other.get())) } |
| 143 | + } |
| 144 | + |
| 145 | + #[inline] |
| 146 | + fn min(self, other: Self) -> Self { |
| 147 | + // SAFETY: The minimum of two non-zero values is still non-zero. |
| 148 | + unsafe { Self(self.get().min(other.get())) } |
| 149 | + } |
| 150 | + |
| 151 | + #[inline] |
| 152 | + fn clamp(self, min: Self, max: Self) -> Self { |
| 153 | + // SAFETY: A non-zero value clamped between two non-zero values is still non-zero. |
| 154 | + unsafe { Self(self.get().clamp(min.get(), max.get())) } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + #[$stability] |
| 159 | + impl Hash for $Ty { |
| 160 | + #[inline] |
| 161 | + fn hash<H>(&self, state: &mut H) |
| 162 | + where |
| 163 | + H: Hasher, |
| 164 | + { |
| 165 | + self.get().hash(state) |
| 166 | + } |
| 167 | + } |
| 168 | + }; |
| 169 | +} |
| 170 | + |
74 | 171 | macro_rules! impl_nonzero_fmt {
|
75 | 172 | ( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
|
76 | 173 | $(
|
@@ -128,13 +225,15 @@ macro_rules! nonzero_integer {
|
128 | 225 | ///
|
129 | 226 | /// [null pointer optimization]: crate::option#representation
|
130 | 227 | #[$stability]
|
131 |
| - #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 228 | + #[derive(Copy, Eq)] |
132 | 229 | #[repr(transparent)]
|
133 | 230 | #[rustc_layout_scalar_valid_range_start(1)]
|
134 | 231 | #[rustc_nonnull_optimization_guaranteed]
|
135 | 232 | #[rustc_diagnostic_item = stringify!($Ty)]
|
136 | 233 | pub struct $Ty($Int);
|
137 | 234 |
|
| 235 | + impl_nonzero_traits!(#[$stability] $Ty); |
| 236 | + |
138 | 237 | impl $Ty {
|
139 | 238 | /// Creates a non-zero without checking whether the value is non-zero.
|
140 | 239 | /// This results in undefined behaviour if the value is zero.
|
|
0 commit comments