about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-05-04 19:18:22 +0200
committerGitHub <noreply@github.com>2023-05-04 19:18:22 +0200
commitab80b7a53d93bda4d33f3469a3442eee2984baf3 (patch)
tree9b01b569d44edbc25e95eedfa94a73563f9b8a65
parentc0ca84b006d86cfb76c9bc6c5e2c3805de073f5f (diff)
parent70523fb0b1eac3903d3be05e839534507f448aad (diff)
downloadrust-ab80b7a53d93bda4d33f3469a3442eee2984baf3.tar.gz
rust-ab80b7a53d93bda4d33f3469a3442eee2984baf3.zip
Rollup merge of #111186 - jmillikin:nonzero-is-positive, r=dtolnay
Add `is_positive` method for signed non-zero integers.

ACP: https://github.com/rust-lang/libs-team/issues/105
-rw-r--r--library/core/src/num/nonzero.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs
index b80bfe1c92d..74a325b89d4 100644
--- a/library/core/src/num/nonzero.rs
+++ b/library/core/src/num/nonzero.rs
@@ -713,6 +713,32 @@ macro_rules! nonzero_signed_operations {
                     unsafe { $Uty::new_unchecked(self.get().unsigned_abs()) }
                 }
 
+                /// Returns `true` if `self` is positive and `false` if the
+                /// number is negative.
+                ///
+                /// # Example
+                ///
+                /// ```
+                /// #![feature(nonzero_negation_ops)]
+                ///
+                #[doc = concat!("# use std::num::", stringify!($Ty), ";")]
+                /// # fn main() { test().unwrap(); }
+                /// # fn test() -> Option<()> {
+                #[doc = concat!("let pos_five = ", stringify!($Ty), "::new(5)?;")]
+                #[doc = concat!("let neg_five = ", stringify!($Ty), "::new(-5)?;")]
+                ///
+                /// assert!(pos_five.is_positive());
+                /// assert!(!neg_five.is_positive());
+                /// # Some(())
+                /// # }
+                /// ```
+                #[must_use]
+                #[inline]
+                #[unstable(feature = "nonzero_negation_ops", issue = "102443")]
+                pub const fn is_positive(self) -> bool {
+                    self.get().is_positive()
+                }
+
                 /// Returns `true` if `self` is negative and `false` if the
                 /// number is positive.
                 ///