|
1 | 1 | //! Definitions of integer that is known not to equal zero.
|
2 | 2 |
|
3 | 3 | use crate::fmt;
|
4 |
| -use crate::ops::{BitOr, BitOrAssign, Div, Rem}; |
| 4 | +use crate::ops::{BitOr, BitOrAssign, Div, Neg, Rem}; |
5 | 5 | use crate::str::FromStr;
|
6 | 6 |
|
7 | 7 | use super::from_str_radix;
|
@@ -664,8 +664,7 @@ macro_rules! nonzero_signed_operations {
|
664 | 664 | /// assert_eq!(pos, pos.wrapping_abs());
|
665 | 665 | /// assert_eq!(pos, neg.wrapping_abs());
|
666 | 666 | /// assert_eq!(min, min.wrapping_abs());
|
667 |
| - /// # // FIXME: add once Neg is implemented? |
668 |
| - /// # // assert_eq!(max, (-max).wrapping_abs()); |
| 667 | + /// assert_eq!(max, (-max).wrapping_abs()); |
669 | 668 | /// # Some(())
|
670 | 669 | /// # }
|
671 | 670 | /// ```
|
@@ -868,6 +867,20 @@ macro_rules! nonzero_signed_operations {
|
868 | 867 | unsafe { $Ty::new_unchecked(result) }
|
869 | 868 | }
|
870 | 869 | }
|
| 870 | + |
| 871 | + #[stable(feature = "signed_nonzero_neg", since = "CURRENT_RUSTC_VERSION")] |
| 872 | + impl Neg for $Ty { |
| 873 | + type Output = $Ty; |
| 874 | + |
| 875 | + #[inline] |
| 876 | + fn neg(self) -> $Ty { |
| 877 | + // SAFETY: negation of nonzero cannot yield zero values. |
| 878 | + unsafe { $Ty::new_unchecked(self.get().neg()) } |
| 879 | + } |
| 880 | + } |
| 881 | + |
| 882 | + forward_ref_unop! { impl Neg, neg for $Ty, |
| 883 | + #[stable(feature = "signed_nonzero_neg", since = "CURRENT_RUSTC_VERSION")] } |
871 | 884 | )+
|
872 | 885 | }
|
873 | 886 | }
|
|
0 commit comments