|
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,13 @@ 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 | + |
74 | 77 | macro_rules! impl_nonzero_fmt {
|
75 | 78 | ( #[$stability: meta] ( $( $Trait: ident ),+ ) for $Ty: ident ) => {
|
76 | 79 | $(
|
@@ -128,7 +131,7 @@ macro_rules! nonzero_integer {
|
128 | 131 | ///
|
129 | 132 | /// [null pointer optimization]: crate::option#representation
|
130 | 133 | #[$stability]
|
131 |
| - #[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 134 | + #[derive(Copy, Eq)] |
132 | 135 | #[repr(transparent)]
|
133 | 136 | #[rustc_layout_scalar_valid_range_start(1)]
|
134 | 137 | #[rustc_nonnull_optimization_guaranteed]
|
@@ -494,6 +497,96 @@ macro_rules! nonzero_integer {
|
494 | 497 | }
|
495 | 498 | }
|
496 | 499 |
|
| 500 | + #[$stability] |
| 501 | + impl Clone for $Ty { |
| 502 | + #[inline] |
| 503 | + fn clone(&self) -> Self { |
| 504 | + // SAFETY: The contained value is non-zero. |
| 505 | + unsafe { Self(self.0) } |
| 506 | + } |
| 507 | + } |
| 508 | + |
| 509 | + #[$stability] |
| 510 | + impl PartialEq for $Ty { |
| 511 | + #[inline] |
| 512 | + fn eq(&self, other: &Self) -> bool { |
| 513 | + self.0 == other.0 |
| 514 | + } |
| 515 | + |
| 516 | + #[inline] |
| 517 | + fn ne(&self, other: &Self) -> bool { |
| 518 | + self.0 != other.0 |
| 519 | + } |
| 520 | + } |
| 521 | + |
| 522 | + #[unstable(feature = "structural_match", issue = "31434")] |
| 523 | + impl StructuralPartialEq for $Ty {} |
| 524 | + |
| 525 | + #[$stability] |
| 526 | + impl PartialOrd for $Ty { |
| 527 | + #[inline] |
| 528 | + fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
| 529 | + self.0.partial_cmp(&other.0) |
| 530 | + } |
| 531 | + |
| 532 | + #[inline] |
| 533 | + fn lt(&self, other: &Self) -> bool { |
| 534 | + self.0 < other.0 |
| 535 | + } |
| 536 | + |
| 537 | + #[inline] |
| 538 | + fn le(&self, other: &Self) -> bool { |
| 539 | + self.0 <= other.0 |
| 540 | + } |
| 541 | + |
| 542 | + #[inline] |
| 543 | + fn gt(&self, other: &Self) -> bool { |
| 544 | + self.0 > other.0 |
| 545 | + } |
| 546 | + |
| 547 | + #[inline] |
| 548 | + fn ge(&self, other: &Self) -> bool { |
| 549 | + self.0 >= other.0 |
| 550 | + } |
| 551 | + } |
| 552 | + |
| 553 | + #[$stability] |
| 554 | + impl Ord for $Ty { |
| 555 | + #[inline] |
| 556 | + fn cmp(&self, other: &Self) -> Ordering { |
| 557 | + self.0.cmp(&other.0) |
| 558 | + } |
| 559 | + |
| 560 | + #[inline] |
| 561 | + fn max(self, other: Self) -> Self { |
| 562 | + // SAFETY: The maximum of two non-zero values is still non-zero. |
| 563 | + unsafe { Self(self.0.max(other.0)) } |
| 564 | + } |
| 565 | + |
| 566 | + #[inline] |
| 567 | + fn min(self, other: Self) -> Self { |
| 568 | + // SAFETY: The minimum of two non-zero values is still non-zero. |
| 569 | + unsafe { Self(self.0.min(other.0)) } |
| 570 | + } |
| 571 | + |
| 572 | + #[inline] |
| 573 | + fn clamp(self, min: Self, max: Self) -> Self { |
| 574 | + // SAFETY: A non-zero value clamped between two non-zero values is still non-zero. |
| 575 | + unsafe { Self(self.0.clamp(min.0, max.0)) } |
| 576 | + } |
| 577 | + } |
| 578 | + |
| 579 | + #[$stability] |
| 580 | + impl Hash for $Ty { |
| 581 | + #[inline] |
| 582 | + fn hash<H>(&self, state: &mut H) |
| 583 | + where |
| 584 | + H: Hasher, |
| 585 | + { |
| 586 | + self.0.hash(state) |
| 587 | + } |
| 588 | + } |
| 589 | + |
497 | 590 | #[stable(feature = "from_nonzero", since = "1.31.0")]
|
498 | 591 | impl From<$Ty> for $Int {
|
499 | 592 | #[doc = concat!("Converts a `", stringify!($Ty), "` into an `", stringify!($Int), "`")]
|
|
0 commit comments