about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJohn Millikin <john@john-millikin.com>2023-05-04 19:37:33 +0900
committerJohn Millikin <john@john-millikin.com>2023-05-04 19:37:33 +0900
commit70523fb0b1eac3903d3be05e839534507f448aad (patch)
tree104f07ceb4478ab69ad09df2bf0ead8ac9e0cad3
parent6f8c0557e0b73c73a8a7163a15f4a5a3feca7d5c (diff)
downloadrust-70523fb0b1eac3903d3be05e839534507f448aad.tar.gz
rust-70523fb0b1eac3903d3be05e839534507f448aad.zip
Add `is_positive` method for signed non-zero integers.
-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.
                 ///