From 69d10d31f290662016f36cb90a9e895f13c4d701 Mon Sep 17 00:00:00 2001 From: Nathan West Date: Mon, 6 Apr 2020 16:06:07 -0400 Subject: [PATCH 1/5] Add PartialEq and PartialOrd for NonZeroInt to Int --- src/libcore/num/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 7ba4004d8609c..2254c465dd3ba 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -110,6 +110,18 @@ assert_eq!(size_of::>(), size_of::<", s } } + impl PartialEq<$Int> for $Ty { + fn eq(&self, rhs: &$Int) -> bool { + self.0 == rhs + } + } + + impl PartialOrd<$Int> for $Ty { + fn partial_cmp(&self, rhs: &$Int) -> Option { + Some(self.0.cmp(rhs)) + } + } + impl_nonzero_fmt! { #[$stability] (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty } From 2f27c31d104af533a528d90f42e2eb297fc77061 Mon Sep 17 00:00:00 2001 From: Nathan West Date: Mon, 6 Apr 2020 16:23:00 -0400 Subject: [PATCH 2/5] Added tests --- src/libcore/tests/nonzero.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/libcore/tests/nonzero.rs b/src/libcore/tests/nonzero.rs index 6c5d19845e406..f8a7e091e94f2 100644 --- a/src/libcore/tests/nonzero.rs +++ b/src/libcore/tests/nonzero.rs @@ -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); +} \ No newline at end of file From 5481b37ba72f51dfa63f79934a3927494cfb714e Mon Sep 17 00:00:00 2001 From: Nathan West Date: Mon, 6 Apr 2020 18:54:14 -0400 Subject: [PATCH 3/5] Added missing import; fixed deref --- src/libcore/num/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 2254c465dd3ba..8c425a4649210 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -9,6 +9,7 @@ use crate::fmt; use crate::intrinsics; use crate::mem; use crate::str::FromStr; +use crate::cmp::Ordering; // Used because the `?` operator is not allowed in a const context. macro_rules! try_opt { @@ -112,7 +113,7 @@ assert_eq!(size_of::>(), size_of::<", s impl PartialEq<$Int> for $Ty { fn eq(&self, rhs: &$Int) -> bool { - self.0 == rhs + self.0 == *rhs } } From 7d071afaf1e4772399c88c8006085e931ce7d9bd Mon Sep 17 00:00:00 2001 From: Nathan West Date: Wed, 8 Apr 2020 13:53:07 -0400 Subject: [PATCH 4/5] Added stability attributes --- src/libcore/num/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 8c425a4649210..a59c62d120332 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -111,12 +111,14 @@ assert_eq!(size_of::>(), size_of::<", s } } + #[unstable(feature = "nonzero_cmp_to_int", issue = "70855")] 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 { Some(self.0.cmp(rhs)) From 881f9a97b26d7220d03d3e9abeb5f975c0f777b9 Mon Sep 17 00:00:00 2001 From: Nathan West Date: Wed, 8 Apr 2020 19:37:04 -0400 Subject: [PATCH 5/5] tidy & rustfmt fixes --- src/libcore/num/mod.rs | 2 +- src/libcore/tests/nonzero.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index a59c62d120332..f3525d6c6376d 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -4,12 +4,12 @@ #![stable(feature = "rust1", since = "1.0.0")] +use crate::cmp::Ordering; use crate::convert::Infallible; use crate::fmt; use crate::intrinsics; use crate::mem; use crate::str::FromStr; -use crate::cmp::Ordering; // Used because the `?` operator is not allowed in a const context. macro_rules! try_opt { diff --git a/src/libcore/tests/nonzero.rs b/src/libcore/tests/nonzero.rs index f8a7e091e94f2..d6953fae1b97f 100644 --- a/src/libcore/tests/nonzero.rs +++ b/src/libcore/tests/nonzero.rs @@ -163,4 +163,4 @@ fn test_compare_regular_int() { assert!(nonzero >= 125); assert!(nonzero >= 0); assert!(nonzero >= -100); -} \ No newline at end of file +}