From 832c7f5061b21657db38aa4537fb1864df2cd421 Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:10:05 +0200 Subject: [PATCH 01/18] NonZero checked_add. --- library/core/src/num/nonzero.rs | 51 +++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 6b9b435d47f..e87049450e3 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -285,6 +285,57 @@ nonzero_integers_div! { NonZeroUsize(usize); } +// A bunch of methods for unsigned nonzero types only. +macro_rules! nonzero_unsigned_operations { + ( $( $Ty: ident($Int: ty); )+ ) => { + $( + impl $Ty { + /// Add an unsigned integer to a non-zero value. + /// Return [`None`] on overflow. + /// + /// # Examples + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let one = ", stringify!($Ty), "::new(1)?;")] + #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] + #[doc = concat!("let max = ", stringify!($Ty), "::new(", + stringify!($Int), "::MAX)?;")] + /// + /// assert_eq!(Some(two), one.checked_add(1)); + /// assert_eq!(None, max.checked_add(1)); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub const fn checked_add(self, other: $Int) -> Option<$Ty> { + if let Some(result) = self.get().checked_add(other) { + // SAFETY: $Int::checked_add returns None on overflow + // so the result cannot be zero. + Some(unsafe { $Ty::new_unchecked(result) }) + } else { + None + } + } + } + )+ + } +} + +nonzero_unsigned_operations! { + NonZeroU8(u8); + NonZeroU16(u16); + NonZeroU32(u32); + NonZeroU64(u64); + NonZeroU128(u128); + NonZeroUsize(usize); +} + macro_rules! nonzero_unsigned_is_power_of_two { ( $( $Ty: ident )+ ) => { $( From a67d6054960e3568fbb6816148a17419e09fe29b Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:12:46 +0200 Subject: [PATCH 02/18] NonZero saturating_add. --- library/core/src/num/nonzero.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index e87049450e3..54c67bccdac 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -322,6 +322,35 @@ macro_rules! nonzero_unsigned_operations { None } } + + /// Add an unsigned integer to a non-zero value. + #[doc = concat!("Return [`", stringify!($Int), "::MAX`] on overflow.")] + /// + /// # Examples + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let one = ", stringify!($Ty), "::new(1)?;")] + #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] + #[doc = concat!("let max = ", stringify!($Ty), "::new(", + stringify!($Int), "::MAX)?;")] + /// + /// assert_eq!(two, one.saturating_add(1)); + /// assert_eq!(max, max.saturating_add(1)); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub const fn saturating_add(self, other: $Int) -> $Ty { + // SAFETY: $Int::saturating_add returns $Int::MAX on overflow + // so the result cannot be zero. + unsafe { $Ty::new_unchecked(self.get().saturating_add(other)) } + } } )+ } From a3e1c358b62f773616c8c7de21bc34f00702e6a8 Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:14:25 +0200 Subject: [PATCH 03/18] NonZero unchecked_add. --- library/core/src/num/nonzero.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 54c67bccdac..28bd8a90b18 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -351,6 +351,34 @@ macro_rules! nonzero_unsigned_operations { // so the result cannot be zero. unsafe { $Ty::new_unchecked(self.get().saturating_add(other)) } } + + /// Add an unsigned integer to a non-zero value, + /// assuming overflow cannot occur. + /// This results in undefined behaviour when + #[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`")] + #[doc = concat!(" or `self + rhs < ", stringify!($Int), "::MIN`.")] + /// + /// # Examples + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let one = ", stringify!($Ty), "::new(1)?;")] + #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] + /// + /// assert_eq!(two, unsafe { one.unchecked_add(1) }); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub unsafe fn unchecked_add(self, other: $Int) -> $Ty { + // SAFETY: The caller ensures there is no overflow. + unsafe { $Ty::new_unchecked(self.get().unchecked_add(other)) } + } } )+ } From f7a1a9d075d7abd4312499c13a7322e41e5c2606 Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:15:51 +0200 Subject: [PATCH 04/18] NonZero checked_next_power_of_two. --- library/core/src/num/nonzero.rs | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 28bd8a90b18..1770b42e20a 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -379,6 +379,42 @@ macro_rules! nonzero_unsigned_operations { // SAFETY: The caller ensures there is no overflow. unsafe { $Ty::new_unchecked(self.get().unchecked_add(other)) } } + + /// Returns the smallest power of two greater than or equal to n. + /// If the next power of two is greater than the type’s maximum value, + /// [`None`] is returned, otherwise the power of two is wrapped in [`Some`]. + /// + /// # Examples + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] + #[doc = concat!("let three = ", stringify!($Ty), "::new(3)?;")] + #[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")] + #[doc = concat!("let max = ", stringify!($Ty), "::new(", + stringify!($Int), "::MAX)?;")] + /// + /// assert_eq!(Some(two), two.checked_next_power_of_two() ); + /// assert_eq!(Some(four), three.checked_next_power_of_two() ); + /// assert_eq!(None, max.checked_next_power_of_two() ); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub const fn checked_next_power_of_two(self) -> Option<$Ty> { + if let Some(nz) = self.get().checked_next_power_of_two() { + // SAFETY: The next power of two is positive + // and overflow is checked. + Some(unsafe { $Ty::new_unchecked(nz) }) + } else { + None + } + } } )+ } From a433b06347bd9d6d966c302bf89c2cf6687861cc Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:18:04 +0200 Subject: [PATCH 05/18] NonZero abs. --- library/core/src/num/nonzero.rs | 45 +++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 1770b42e20a..6fa394b2ff0 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -429,6 +429,51 @@ nonzero_unsigned_operations! { NonZeroUsize(usize); } +// A bunch of methods for signed nonzero types only. +macro_rules! nonzero_signed_operations { + ( $( $Ty: ident($Int: ty) -> $Uty: ident($Uint: ty); )+ ) => { + $( + impl $Ty { + /// Computes the absolute value of self. + #[doc = concat!("See [`", stringify!($Int), "::abs`]")] + /// for documentation on overflow behaviour. + /// + /// # Example + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")] + #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")] + /// + /// assert_eq!(pos, pos.abs()); + /// assert_eq!(pos, neg.abs()); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub const fn abs(self) -> $Ty { + // SAFETY: This cannot overflow to zero. + unsafe { $Ty::new_unchecked(self.get().abs()) } + } + } + )+ + } +} + +nonzero_signed_operations! { + NonZeroI8(i8) -> NonZeroU8(u8); + NonZeroI16(i16) -> NonZeroU16(u16); + NonZeroI32(i32) -> NonZeroU32(u32); + NonZeroI64(i64) -> NonZeroU64(u64); + NonZeroI128(i128) -> NonZeroU128(u128); + NonZeroIsize(isize) -> NonZeroUsize(usize); +} + macro_rules! nonzero_unsigned_is_power_of_two { ( $( $Ty: ident )+ ) => { $( From 62f97d950fcf858ffd0230aff4f7f1364d5653ea Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:19:24 +0200 Subject: [PATCH 06/18] NonZero checked_abs. --- library/core/src/num/nonzero.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 6fa394b2ff0..a137e853782 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -460,6 +460,39 @@ macro_rules! nonzero_signed_operations { // SAFETY: This cannot overflow to zero. unsafe { $Ty::new_unchecked(self.get().abs()) } } + + /// Checked absolute value. + /// Returns [`None`] if + #[doc = concat!("`self == ", stringify!($Int), "::MIN`.")] + /// + /// # Example + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")] + #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")] + #[doc = concat!("let min = ", stringify!($Ty), "::new(", + stringify!($Int), "::MIN)?;")] + /// + /// assert_eq!(Some(pos), neg.checked_abs()); + /// assert_eq!(None, min.checked_abs()); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub const fn checked_abs(self) -> Option<$Ty> { + if let Some(nz) = self.get().checked_abs() { + // SAFETY: absolute value of nonzero cannot yield zero values. + Some(unsafe { $Ty::new_unchecked(nz) }) + } else { + None + } + } } )+ } From 6083b0ad2a145651d78813d5c1da1e0296146949 Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:21:25 +0200 Subject: [PATCH 07/18] NonZero overflowing_abs. --- library/core/src/num/nonzero.rs | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index a137e853782..9294184ca4d 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -493,6 +493,40 @@ macro_rules! nonzero_signed_operations { None } } + + /// Computes the absolute value of self, + /// with overflow information, see + #[doc = concat!("[`", stringify!($Int), "::overflowing_abs`].")] + /// + /// # Example + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")] + #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")] + #[doc = concat!("let min = ", stringify!($Ty), "::new(", + stringify!($Int), "::MIN)?;")] + /// + /// assert_eq!((pos, false), pos.overflowing_abs()); + /// assert_eq!((pos, false), neg.overflowing_abs()); + /// assert_eq!((min, true), min.overflowing_abs()); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub const fn overflowing_abs(self) -> ($Ty, bool) { + let (nz, flag) = self.get().overflowing_abs(); + ( + // SAFETY: absolute value of nonzero cannot yield zero values. + unsafe { $Ty::new_unchecked(nz) }, + flag, + ) + } } )+ } From 65e7321457912340cd424b57f58beb32659dfcfb Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:23:02 +0200 Subject: [PATCH 08/18] NonZero saturating_abs. --- library/core/src/num/nonzero.rs | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 9294184ca4d..6183cc7f011 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -527,6 +527,40 @@ macro_rules! nonzero_signed_operations { flag, ) } + + /// Saturating absolute value, see + #[doc = concat!("[`", stringify!($Int), "::saturating_abs`].")] + /// + /// # Example + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")] + #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")] + #[doc = concat!("let min = ", stringify!($Ty), "::new(", + stringify!($Int), "::MIN)?;")] + #[doc = concat!("let min_plus = ", stringify!($Ty), "::new(", + stringify!($Int), "::MIN + 1)?;")] + #[doc = concat!("let max = ", stringify!($Ty), "::new(", + stringify!($Int), "::MAX)?;")] + /// + /// assert_eq!(pos, pos.saturating_abs()); + /// assert_eq!(pos, neg.saturating_abs()); + /// assert_eq!(max, min.saturating_abs()); + /// assert_eq!(max, min_plus.saturating_abs()); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub const fn saturating_abs(self) -> $Ty { + // SAFETY: absolute value of nonzero cannot yield zero values. + unsafe { $Ty::new_unchecked(self.get().saturating_abs()) } + } } )+ } From b6589bbfa987d7999f5b6cd483d365f8b24faebb Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:25:13 +0200 Subject: [PATCH 09/18] NonZero wrapping_abs. --- library/core/src/num/nonzero.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 6183cc7f011..459d5a4caef 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -561,6 +561,39 @@ macro_rules! nonzero_signed_operations { // SAFETY: absolute value of nonzero cannot yield zero values. unsafe { $Ty::new_unchecked(self.get().saturating_abs()) } } + + /// Wrapping absolute value, see + #[doc = concat!("[`", stringify!($Int), "::wrapping_abs`].")] + /// + /// # Example + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")] + #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")] + #[doc = concat!("let min = ", stringify!($Ty), "::new(", + stringify!($Int), "::MIN)?;")] + #[doc = concat!("let max = ", stringify!($Ty), "::new(", + stringify!($Int), "::MAX)?;")] + /// + /// assert_eq!(pos, pos.wrapping_abs()); + /// assert_eq!(pos, neg.wrapping_abs()); + /// assert_eq!(min, min.wrapping_abs()); + /// # // FIXME: add once Neg is implemented? + /// # // assert_eq!(max, (-max).wrapping_abs()); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub const fn wrapping_abs(self) -> $Ty { + // SAFETY: absolute value of nonzero cannot yield zero values. + unsafe { $Ty::new_unchecked(self.get().wrapping_abs()) } + } } )+ } From 7e7b3161633109b80efa21d07f0352168ef6276e Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:26:39 +0200 Subject: [PATCH 10/18] NonZero unsigned_abs. --- library/core/src/num/nonzero.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 459d5a4caef..280955ecd1d 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -594,6 +594,39 @@ macro_rules! nonzero_signed_operations { // SAFETY: absolute value of nonzero cannot yield zero values. unsafe { $Ty::new_unchecked(self.get().wrapping_abs()) } } + + /// Computes the absolute value of self + /// without any wrapping or panicking. + /// + /// # Example + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + #[doc = concat!("# use std::num::", stringify!($Uty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let u_pos = ", stringify!($Uty), "::new(1)?;")] + #[doc = concat!("let i_pos = ", stringify!($Ty), "::new(1)?;")] + #[doc = concat!("let i_neg = ", stringify!($Ty), "::new(-1)?;")] + #[doc = concat!("let i_min = ", stringify!($Ty), "::new(", + stringify!($Int), "::MIN)?;")] + #[doc = concat!("let u_max = ", stringify!($Uty), "::new(", + stringify!($Uint), "::MAX / 2 + 1)?;")] + /// + /// assert_eq!(u_pos, i_pos.unsigned_abs()); + /// assert_eq!(u_pos, i_neg.unsigned_abs()); + /// assert_eq!(u_max, i_min.unsigned_abs()); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub const fn unsigned_abs(self) -> $Uty { + // SAFETY: absolute value of nonzero cannot yield zero values. + unsafe { $Uty::new_unchecked(self.get().unsigned_abs()) } + } } )+ } From ac3eb90d59c347f7fde2876b8124cfefbf75eb23 Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:28:45 +0200 Subject: [PATCH 11/18] NonZero checked_mul. --- library/core/src/num/nonzero.rs | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 280955ecd1d..d48299c0715 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -641,6 +641,64 @@ nonzero_signed_operations! { NonZeroIsize(isize) -> NonZeroUsize(usize); } +// A bunch of methods for both signed and unsigned nonzero types. +macro_rules! nonzero_unsigned_signed_operations { + ( $( $Ty: ident($Int: ty); )+ ) => { + $( + impl $Ty { + /// Multiply two non-zero integers together. + /// Return [`None`] on overflow. + /// + /// # Examples + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] + #[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")] + #[doc = concat!("let max = ", stringify!($Ty), "::new(", + stringify!($Int), "::MAX)?;")] + /// + /// assert_eq!(Some(four), two.checked_mul(two)); + /// assert_eq!(None, max.checked_mul(two)); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub const fn checked_mul(self, other: $Ty) -> Option<$Ty> { + if let Some(result) = self.get().checked_mul(other.get()) { + // SAFETY: checked_mul returns None on overflow + // and `other` is also non-null + // so the result cannot be zero. + Some(unsafe { $Ty::new_unchecked(result) }) + } else { + None + } + } + } + )+ + } +} + +nonzero_unsigned_signed_operations! { + NonZeroU8(u8); + NonZeroU16(u16); + NonZeroU32(u32); + NonZeroU64(u64); + NonZeroU128(u128); + NonZeroUsize(usize); + NonZeroI8(i8); + NonZeroI16(i16); + NonZeroI32(i32); + NonZeroI64(i64); + NonZeroI128(i128); + NonZeroIsize(isize); +} + macro_rules! nonzero_unsigned_is_power_of_two { ( $( $Ty: ident )+ ) => { $( From 7e0b9a8bd0452a027582f26cd139fa92fba618f7 Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:30:13 +0200 Subject: [PATCH 12/18] NonZero saturating_mul. --- library/core/src/num/nonzero.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index d48299c0715..7a1b4d6c719 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -679,6 +679,36 @@ macro_rules! nonzero_unsigned_signed_operations { None } } + + /// Multiply two non-zero integers together. + #[doc = concat!("Return [`", stringify!($Int), "::MAX`] on overflow.")] + /// + /// # Examples + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] + #[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")] + #[doc = concat!("let max = ", stringify!($Ty), "::new(", + stringify!($Int), "::MAX)?;")] + /// + /// assert_eq!(four, two.saturating_mul(two)); + /// assert_eq!(max, four.saturating_mul(max)); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub const fn saturating_mul(self, other: $Ty) -> $Ty { + // SAFETY: saturating_mul returns u*::MAX on overflow + // and `other` is also non-null + // so the result cannot be zero. + unsafe { $Ty::new_unchecked(self.get().saturating_mul(other.get())) } + } } )+ } From 6979bb40f8dd1a67e7508779873e7525441de0ce Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:31:32 +0200 Subject: [PATCH 13/18] NonZero unchecked_mul. --- library/core/src/num/nonzero.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 7a1b4d6c719..fdb874bc723 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -709,6 +709,34 @@ macro_rules! nonzero_unsigned_signed_operations { // so the result cannot be zero. unsafe { $Ty::new_unchecked(self.get().saturating_mul(other.get())) } } + + /// Multiply two non-zero integers together, + /// assuming overflow cannot occur. + /// This results in undefined behavior when + #[doc = concat!("`self * rhs > ", stringify!($Int), "::MAX`, ")] + #[doc = concat!("or `self * rhs < ", stringify!($Int), "::MIN`.")] + /// + /// # Examples + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] + #[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")] + /// + /// assert_eq!(four, unsafe { two.unchecked_mul(two) }); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub unsafe fn unchecked_mul(self, other: $Ty) -> $Ty { + // SAFETY: The caller ensures there is no overflow. + unsafe { $Ty::new_unchecked(self.get().unchecked_mul(other.get())) } + } } )+ } From 7b37800b45e6cf5d06caf2ee21066797516ff43c Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:32:48 +0200 Subject: [PATCH 14/18] NonZero checked_pow. --- library/core/src/num/nonzero.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index fdb874bc723..ceb2e461387 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -737,6 +737,39 @@ macro_rules! nonzero_unsigned_signed_operations { // SAFETY: The caller ensures there is no overflow. unsafe { $Ty::new_unchecked(self.get().unchecked_mul(other.get())) } } + + /// Raise non-zero value to an integer power. + /// Return [`None`] on overflow. + /// + /// # Examples + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let three = ", stringify!($Ty), "::new(3)?;")] + #[doc = concat!("let twenty_seven = ", stringify!($Ty), "::new(27)?;")] + #[doc = concat!("let half_max = ", stringify!($Ty), "::new(", + stringify!($Int), "::MAX / 2)?;")] + /// + /// assert_eq!(Some(twenty_seven), three.checked_pow(3)); + /// assert_eq!(None, half_max.checked_pow(3)); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub const fn checked_pow(self, other: u32) -> Option<$Ty> { + if let Some(result) = self.get().checked_pow(other) { + // SAFETY: checked_pow returns None on overflow + // so the result cannot be zero. + Some(unsafe { $Ty::new_unchecked(result) }) + } else { + None + } + } } )+ } From b8056d8e29b1ee82158bfa2a2fc65e09799ba1d6 Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Thu, 15 Apr 2021 12:34:03 +0200 Subject: [PATCH 15/18] NonZero saturating_pow. --- library/core/src/num/nonzero.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index ceb2e461387..90d87edb25d 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -770,6 +770,35 @@ macro_rules! nonzero_unsigned_signed_operations { None } } + + /// Raise non-zero value to an integer power. + #[doc = concat!("Return [`", stringify!($Int), "::MAX`] on overflow.")] + /// + /// # Examples + /// + /// ``` + /// #![feature(nonzero_ops)] + /// # #![feature(try_trait)] + #[doc = concat!("# use std::num::", stringify!($Ty), ";")] + /// + /// # fn main() -> Result<(), std::option::NoneError> { + #[doc = concat!("let three = ", stringify!($Ty), "::new(3)?;")] + #[doc = concat!("let twenty_seven = ", stringify!($Ty), "::new(27)?;")] + #[doc = concat!("let max = ", stringify!($Ty), "::new(", + stringify!($Int), "::MAX)?;")] + /// + /// assert_eq!(twenty_seven, three.saturating_pow(3)); + /// assert_eq!(max, max.saturating_pow(3)); + /// # Ok(()) + /// # } + /// ``` + #[unstable(feature = "nonzero_ops", issue = "84186")] + #[inline] + pub const fn saturating_pow(self, other: u32) -> $Ty { + // SAFETY: saturating_pow returns u*::MAX on overflow + // so the result cannot be zero. + unsafe { $Ty::new_unchecked(self.get().saturating_pow(other)) } + } } )+ } From 3c168b0dc63e782f4ee866052d9b1c2220abaa1e Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Wed, 28 Apr 2021 10:55:48 +0200 Subject: [PATCH 16/18] Explicit what `check` means on concerned method. --- library/core/src/num/nonzero.rs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 90d87edb25d..d7e28d24507 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -42,7 +42,8 @@ macro_rules! nonzero_integers { pub struct $Ty($Int); impl $Ty { - /// Creates a non-zero without checking the value. + /// Creates a non-zero without checking whether the value is non-zero. + /// This results in undefined behaviour if the value is zero. /// /// # Safety /// @@ -291,7 +292,9 @@ macro_rules! nonzero_unsigned_operations { $( impl $Ty { /// Add an unsigned integer to a non-zero value. - /// Return [`None`] on overflow. + /// Check for overflow and return [`None`] on overflow + /// As a consequence, the result cannot wrap to zero. + /// /// /// # Examples /// @@ -354,7 +357,9 @@ macro_rules! nonzero_unsigned_operations { /// Add an unsigned integer to a non-zero value, /// assuming overflow cannot occur. - /// This results in undefined behaviour when + /// Overflow is unchecked, and it is undefined behaviour to overflow + /// *even if the result would wrap to a non-zero value*. + /// The behaviour is undefined as soon as #[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`")] #[doc = concat!(" or `self + rhs < ", stringify!($Int), "::MIN`.")] /// @@ -381,8 +386,9 @@ macro_rules! nonzero_unsigned_operations { } /// Returns the smallest power of two greater than or equal to n. - /// If the next power of two is greater than the type’s maximum value, - /// [`None`] is returned, otherwise the power of two is wrapped in [`Some`]. + /// Check for overflow and return [`None`] + /// if the next power of two is greater than the type’s maximum value. + /// As a consequence, the result cannot wrap to zero. /// /// # Examples /// @@ -462,8 +468,9 @@ macro_rules! nonzero_signed_operations { } /// Checked absolute value. - /// Returns [`None`] if + /// Check for overflow and returns [`None`] if #[doc = concat!("`self == ", stringify!($Int), "::MIN`.")] + /// The result cannot be zero. /// /// # Example /// @@ -647,7 +654,8 @@ macro_rules! nonzero_unsigned_signed_operations { $( impl $Ty { /// Multiply two non-zero integers together. - /// Return [`None`] on overflow. + /// Check for overflow and return [`None`] on overflow. + /// As a consequence, the result cannot wrap to zero. /// /// # Examples /// @@ -712,7 +720,9 @@ macro_rules! nonzero_unsigned_signed_operations { /// Multiply two non-zero integers together, /// assuming overflow cannot occur. - /// This results in undefined behavior when + /// Overflow is unchecked, and it is undefined behaviour to overflow + /// *even if the result would wrap to a non-zero value*. + /// The behaviour is undefined as soon as #[doc = concat!("`self * rhs > ", stringify!($Int), "::MAX`, ")] #[doc = concat!("or `self * rhs < ", stringify!($Int), "::MIN`.")] /// @@ -739,7 +749,8 @@ macro_rules! nonzero_unsigned_signed_operations { } /// Raise non-zero value to an integer power. - /// Return [`None`] on overflow. + /// Check for overflow and return [`None`] on overflow. + /// As a consequence, the result cannot wrap to zero. /// /// # Examples /// From d442c104eadfaf01d8a12ff6e646ccaec4c7948b Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Mon, 7 Jun 2021 16:19:54 +0200 Subject: [PATCH 17/18] Fix diverging doc regarding signedness. --- library/core/src/num/nonzero.rs | 62 +++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index d7e28d24507..3eb887cd377 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -360,8 +360,7 @@ macro_rules! nonzero_unsigned_operations { /// Overflow is unchecked, and it is undefined behaviour to overflow /// *even if the result would wrap to a non-zero value*. /// The behaviour is undefined as soon as - #[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`")] - #[doc = concat!(" or `self + rhs < ", stringify!($Int), "::MIN`.")] + #[doc = concat!("`self + rhs > ", stringify!($Int), "::MAX`.")] /// /// # Examples /// @@ -650,7 +649,7 @@ nonzero_signed_operations! { // A bunch of methods for both signed and unsigned nonzero types. macro_rules! nonzero_unsigned_signed_operations { - ( $( $Ty: ident($Int: ty); )+ ) => { + ( $( $signedness:ident $Ty: ident($Int: ty); )+ ) => { $( impl $Ty { /// Multiply two non-zero integers together. @@ -723,8 +722,16 @@ macro_rules! nonzero_unsigned_signed_operations { /// Overflow is unchecked, and it is undefined behaviour to overflow /// *even if the result would wrap to a non-zero value*. /// The behaviour is undefined as soon as - #[doc = concat!("`self * rhs > ", stringify!($Int), "::MAX`, ")] - #[doc = concat!("or `self * rhs < ", stringify!($Int), "::MIN`.")] + #[doc = sign_dependent_expr!{ + $signedness ? + if signed { + concat!("`self * rhs > ", stringify!($Int), "::MAX`, ", + "or `self * rhs < ", stringify!($Int), "::MIN`.") + } + if unsigned { + concat!("`self * rhs > ", stringify!($Int), "::MAX`.") + } + }] /// /// # Examples /// @@ -783,7 +790,16 @@ macro_rules! nonzero_unsigned_signed_operations { } /// Raise non-zero value to an integer power. - #[doc = concat!("Return [`", stringify!($Int), "::MAX`] on overflow.")] + #[doc = sign_dependent_expr!{ + $signedness ? + if signed { + concat!("Return [`", stringify!($Int), "::MIN`] ", + "or [`", stringify!($Int), "::MAX`] on overflow.") + } + if unsigned { + concat!("Return [`", stringify!($Int), "::MAX`] on overflow.") + } + }] /// /// # Examples /// @@ -815,19 +831,29 @@ macro_rules! nonzero_unsigned_signed_operations { } } +// Use this when the generated code should differ between signed and unsigned types. +macro_rules! sign_dependent_expr { + (signed ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => { + $signed_case + }; + (unsigned ? if signed { $signed_case:expr } if unsigned { $unsigned_case:expr } ) => { + $unsigned_case + }; +} + nonzero_unsigned_signed_operations! { - NonZeroU8(u8); - NonZeroU16(u16); - NonZeroU32(u32); - NonZeroU64(u64); - NonZeroU128(u128); - NonZeroUsize(usize); - NonZeroI8(i8); - NonZeroI16(i16); - NonZeroI32(i32); - NonZeroI64(i64); - NonZeroI128(i128); - NonZeroIsize(isize); + unsigned NonZeroU8(u8); + unsigned NonZeroU16(u16); + unsigned NonZeroU32(u32); + unsigned NonZeroU64(u64); + unsigned NonZeroU128(u128); + unsigned NonZeroUsize(usize); + signed NonZeroI8(i8); + signed NonZeroI16(i16); + signed NonZeroI32(i32); + signed NonZeroI64(i64); + signed NonZeroI128(i128); + signed NonZeroIsize(isize); } macro_rules! nonzero_unsigned_is_power_of_two { From 7afdaf2c06c624e08c7533bf4c1e291df823aaba Mon Sep 17 00:00:00 2001 From: Iago-lito Date: Sat, 12 Jun 2021 10:58:37 +0200 Subject: [PATCH 18/18] Stop relying on #[feature(try_trait)] in doctests. --- library/core/src/num/nonzero.rs | 90 ++++++++++++++++----------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 3eb887cd377..dd9b9330aee 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -300,10 +300,10 @@ macro_rules! nonzero_unsigned_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let one = ", stringify!($Ty), "::new(1)?;")] #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] #[doc = concat!("let max = ", stringify!($Ty), "::new(", @@ -311,7 +311,7 @@ macro_rules! nonzero_unsigned_operations { /// /// assert_eq!(Some(two), one.checked_add(1)); /// assert_eq!(None, max.checked_add(1)); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -333,10 +333,10 @@ macro_rules! nonzero_unsigned_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let one = ", stringify!($Ty), "::new(1)?;")] #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] #[doc = concat!("let max = ", stringify!($Ty), "::new(", @@ -344,7 +344,7 @@ macro_rules! nonzero_unsigned_operations { /// /// assert_eq!(two, one.saturating_add(1)); /// assert_eq!(max, max.saturating_add(1)); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -366,15 +366,15 @@ macro_rules! nonzero_unsigned_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let one = ", stringify!($Ty), "::new(1)?;")] #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] /// /// assert_eq!(two, unsafe { one.unchecked_add(1) }); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -393,10 +393,10 @@ macro_rules! nonzero_unsigned_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] #[doc = concat!("let three = ", stringify!($Ty), "::new(3)?;")] #[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")] @@ -406,7 +406,7 @@ macro_rules! nonzero_unsigned_operations { /// assert_eq!(Some(two), two.checked_next_power_of_two() ); /// assert_eq!(Some(four), three.checked_next_power_of_two() ); /// assert_eq!(None, max.checked_next_power_of_two() ); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -447,16 +447,16 @@ macro_rules! nonzero_signed_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")] #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")] /// /// assert_eq!(pos, pos.abs()); /// assert_eq!(pos, neg.abs()); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -475,10 +475,10 @@ macro_rules! nonzero_signed_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")] #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")] #[doc = concat!("let min = ", stringify!($Ty), "::new(", @@ -486,7 +486,7 @@ macro_rules! nonzero_signed_operations { /// /// assert_eq!(Some(pos), neg.checked_abs()); /// assert_eq!(None, min.checked_abs()); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -508,10 +508,10 @@ macro_rules! nonzero_signed_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")] #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")] #[doc = concat!("let min = ", stringify!($Ty), "::new(", @@ -520,7 +520,7 @@ macro_rules! nonzero_signed_operations { /// assert_eq!((pos, false), pos.overflowing_abs()); /// assert_eq!((pos, false), neg.overflowing_abs()); /// assert_eq!((min, true), min.overflowing_abs()); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -541,10 +541,10 @@ macro_rules! nonzero_signed_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")] #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")] #[doc = concat!("let min = ", stringify!($Ty), "::new(", @@ -558,7 +558,7 @@ macro_rules! nonzero_signed_operations { /// assert_eq!(pos, neg.saturating_abs()); /// assert_eq!(max, min.saturating_abs()); /// assert_eq!(max, min_plus.saturating_abs()); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -575,10 +575,10 @@ macro_rules! nonzero_signed_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let pos = ", stringify!($Ty), "::new(1)?;")] #[doc = concat!("let neg = ", stringify!($Ty), "::new(-1)?;")] #[doc = concat!("let min = ", stringify!($Ty), "::new(", @@ -591,7 +591,7 @@ macro_rules! nonzero_signed_operations { /// assert_eq!(min, min.wrapping_abs()); /// # // FIXME: add once Neg is implemented? /// # // assert_eq!(max, (-max).wrapping_abs()); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -608,11 +608,11 @@ macro_rules! nonzero_signed_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] #[doc = concat!("# use std::num::", stringify!($Uty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let u_pos = ", stringify!($Uty), "::new(1)?;")] #[doc = concat!("let i_pos = ", stringify!($Ty), "::new(1)?;")] #[doc = concat!("let i_neg = ", stringify!($Ty), "::new(-1)?;")] @@ -624,7 +624,7 @@ macro_rules! nonzero_signed_operations { /// assert_eq!(u_pos, i_pos.unsigned_abs()); /// assert_eq!(u_pos, i_neg.unsigned_abs()); /// assert_eq!(u_max, i_min.unsigned_abs()); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -660,10 +660,10 @@ macro_rules! nonzero_unsigned_signed_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] #[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")] #[doc = concat!("let max = ", stringify!($Ty), "::new(", @@ -671,7 +671,7 @@ macro_rules! nonzero_unsigned_signed_operations { /// /// assert_eq!(Some(four), two.checked_mul(two)); /// assert_eq!(None, max.checked_mul(two)); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -694,10 +694,10 @@ macro_rules! nonzero_unsigned_signed_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] #[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")] #[doc = concat!("let max = ", stringify!($Ty), "::new(", @@ -705,7 +705,7 @@ macro_rules! nonzero_unsigned_signed_operations { /// /// assert_eq!(four, two.saturating_mul(two)); /// assert_eq!(max, four.saturating_mul(max)); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -737,15 +737,15 @@ macro_rules! nonzero_unsigned_signed_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let two = ", stringify!($Ty), "::new(2)?;")] #[doc = concat!("let four = ", stringify!($Ty), "::new(4)?;")] /// /// assert_eq!(four, unsafe { two.unchecked_mul(two) }); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -763,10 +763,10 @@ macro_rules! nonzero_unsigned_signed_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let three = ", stringify!($Ty), "::new(3)?;")] #[doc = concat!("let twenty_seven = ", stringify!($Ty), "::new(27)?;")] #[doc = concat!("let half_max = ", stringify!($Ty), "::new(", @@ -774,7 +774,7 @@ macro_rules! nonzero_unsigned_signed_operations { /// /// assert_eq!(Some(twenty_seven), three.checked_pow(3)); /// assert_eq!(None, half_max.checked_pow(3)); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")] @@ -805,10 +805,10 @@ macro_rules! nonzero_unsigned_signed_operations { /// /// ``` /// #![feature(nonzero_ops)] - /// # #![feature(try_trait)] #[doc = concat!("# use std::num::", stringify!($Ty), ";")] /// - /// # fn main() -> Result<(), std::option::NoneError> { + /// # fn main() { test().unwrap(); } + /// # fn test() -> Option<()> { #[doc = concat!("let three = ", stringify!($Ty), "::new(3)?;")] #[doc = concat!("let twenty_seven = ", stringify!($Ty), "::new(27)?;")] #[doc = concat!("let max = ", stringify!($Ty), "::new(", @@ -816,7 +816,7 @@ macro_rules! nonzero_unsigned_signed_operations { /// /// assert_eq!(twenty_seven, three.saturating_pow(3)); /// assert_eq!(max, max.saturating_pow(3)); - /// # Ok(()) + /// # Some(()) /// # } /// ``` #[unstable(feature = "nonzero_ops", issue = "84186")]