Skip to content

Commit ab80b7a

Browse files
authored
Rollup merge of rust-lang#111186 - jmillikin:nonzero-is-positive, r=dtolnay
Add `is_positive` method for signed non-zero integers. ACP: rust-lang/libs-team#105
2 parents c0ca84b + 70523fb commit ab80b7a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

library/core/src/num/nonzero.rs

+26
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,32 @@ macro_rules! nonzero_signed_operations {
713713
unsafe { $Uty::new_unchecked(self.get().unsigned_abs()) }
714714
}
715715

716+
/// Returns `true` if `self` is positive and `false` if the
717+
/// number is negative.
718+
///
719+
/// # Example
720+
///
721+
/// ```
722+
/// #![feature(nonzero_negation_ops)]
723+
///
724+
#[doc = concat!("# use std::num::", stringify!($Ty), ";")]
725+
/// # fn main() { test().unwrap(); }
726+
/// # fn test() -> Option<()> {
727+
#[doc = concat!("let pos_five = ", stringify!($Ty), "::new(5)?;")]
728+
#[doc = concat!("let neg_five = ", stringify!($Ty), "::new(-5)?;")]
729+
///
730+
/// assert!(pos_five.is_positive());
731+
/// assert!(!neg_five.is_positive());
732+
/// # Some(())
733+
/// # }
734+
/// ```
735+
#[must_use]
736+
#[inline]
737+
#[unstable(feature = "nonzero_negation_ops", issue = "102443")]
738+
pub const fn is_positive(self) -> bool {
739+
self.get().is_positive()
740+
}
741+
716742
/// Returns `true` if `self` is negative and `false` if the
717743
/// number is positive.
718744
///

0 commit comments

Comments
 (0)