Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#![stable(feature = "rust1", since = "1.0.0")]

use crate::cmp::Ordering;
use crate::convert::Infallible;
use crate::fmt;
use crate::intrinsics;
Expand Down Expand Up @@ -110,6 +111,20 @@ assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", s
}
}

#[unstable(feature = "nonzero_cmp_to_int", issue = "70855")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trait implementations are always insta-stable. Use #[stable] instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated to stable; it wants a "since" attribute

impl PartialEq<$Int> for $Ty {
fn eq(&self, rhs: &$Int) -> bool {
self.0 == *rhs
}
}

#[unstable(feature = "nonzero_cmp_to_int", issue = "70855")]
impl PartialOrd<$Int> for $Ty {
fn partial_cmp(&self, rhs: &$Int) -> Option<Ordering> {
Some(self.0.cmp(rhs))
}
}

impl_nonzero_fmt! {
#[$stability] (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
}
Expand Down
23 changes: 23 additions & 0 deletions src/libcore/tests/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,26 @@ fn test_from_str() {
Some(IntErrorKind::Overflow)
);
}

#[test]
fn test_compare_regular_int() {
let nonzero = NonZeroI32::new(125).unwrap();

assert_eq!(nonzero, 125);
assert_ne!(nonzero, 0);
assert_ne!(nonzero, -125);

assert!(nonzero == 125);
assert!(nonzero != 0);
assert!(nonzero != -125);

assert!(nonzero < 200);
assert!(nonzero <= 125);
assert!(nonzero <= 200);

assert!(nonzero > 0);
assert!(nonzero > -200);
assert!(nonzero >= 125);
assert!(nonzero >= 0);
assert!(nonzero >= -100);
}