From 680bec2e5b5eedd9cd56716eb9e43a3760f1cba8 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Tue, 4 Feb 2020 14:46:34 +0000 Subject: [PATCH 1/5] impl TryFrom<{integer}> for NonZero{integer} --- src/libcore/num/mod.rs | 16 +++++++++++++++- src/test/ui/numeric/numeric-cast-3.rs | 6 ++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/numeric/numeric-cast-3.rs diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 39c7d6d24ed04..3462f4364e40a 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -4,7 +4,7 @@ #![stable(feature = "rust1", since = "1.0.0")] -use crate::convert::Infallible; +use crate::convert::{Infallible, TryFrom}; use crate::fmt; use crate::intrinsics; use crate::mem; @@ -100,6 +100,20 @@ assert_eq!(size_of::>(), size_of::<", s } } + #[stable(feature = "try_from_nonzero", since = "1.43.0")] + impl TryFrom<$Int> for $Ty { + type Error = (); + doc_comment! { + concat!( +"Converts a `", stringify!($Ty), "` into an `", stringify!($Int), +"` if it is non-zero"), + fn try_from(int: $Int) -> Result { + Self::new(int).ok_or(()) + } + } + + } + impl_nonzero_fmt! { #[$stability] (Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty } diff --git a/src/test/ui/numeric/numeric-cast-3.rs b/src/test/ui/numeric/numeric-cast-3.rs new file mode 100644 index 0000000000000..8498132c08c62 --- /dev/null +++ b/src/test/ui/numeric/numeric-cast-3.rs @@ -0,0 +1,6 @@ +use std::num::NonZeroUsize; + +fn main() { + let a: usize = 1.try_into().unwrap(); + #[should_panic] let b: usize = 0.try_into().unwrap(); +} From 673caa4be1def71108d95bad7609b5652d179b59 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Tue, 4 Feb 2020 16:44:36 +0000 Subject: [PATCH 2/5] make the tests compile --- src/test/ui/numeric/numeric-cast-3.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/ui/numeric/numeric-cast-3.rs b/src/test/ui/numeric/numeric-cast-3.rs index 8498132c08c62..0307cfedd1f09 100644 --- a/src/test/ui/numeric/numeric-cast-3.rs +++ b/src/test/ui/numeric/numeric-cast-3.rs @@ -1,6 +1,7 @@ use std::num::NonZeroUsize; +use std::convert::TryInto; fn main() { - let a: usize = 1.try_into().unwrap(); - #[should_panic] let b: usize = 0.try_into().unwrap(); + let a: NonZeroUsize = 1_usize.try_into().unwrap(); + let b: NonZeroUsize = 0_usize.try_into().unwrap(); } From 7d703cb5bf1656d163ba84ef4c134eb67c09fb8c Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Tue, 4 Feb 2020 12:37:25 -0500 Subject: [PATCH 3/5] mark that the test should pass --- src/test/ui/numeric/numeric-cast-3.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/ui/numeric/numeric-cast-3.rs b/src/test/ui/numeric/numeric-cast-3.rs index 0307cfedd1f09..a9e92143d3f1f 100644 --- a/src/test/ui/numeric/numeric-cast-3.rs +++ b/src/test/ui/numeric/numeric-cast-3.rs @@ -1,3 +1,6 @@ +// make sure that TryInto is implemented for NonZero{integer} +// +// build-pass use std::num::NonZeroUsize; use std::convert::TryInto; From 86cb1223d6f545588f2de996fbc63eff8e81bac0 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Tue, 4 Feb 2020 12:38:25 -0500 Subject: [PATCH 4/5] don't build test unnecessarily --- src/test/ui/numeric/numeric-cast-3.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/numeric/numeric-cast-3.rs b/src/test/ui/numeric/numeric-cast-3.rs index a9e92143d3f1f..28e30574014da 100644 --- a/src/test/ui/numeric/numeric-cast-3.rs +++ b/src/test/ui/numeric/numeric-cast-3.rs @@ -1,6 +1,6 @@ // make sure that TryInto is implemented for NonZero{integer} // -// build-pass +// check-pass use std::num::NonZeroUsize; use std::convert::TryInto; From dec1f3ca56593c674a82977dc99b3321ad318b79 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Tue, 4 Feb 2020 13:20:14 -0500 Subject: [PATCH 5/5] fix doc comment We're going {integer} -> NonZero{integer}, not the reverse --- src/libcore/num/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 3462f4364e40a..cb7f83f9fa0d3 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -105,7 +105,7 @@ assert_eq!(size_of::>(), size_of::<", s type Error = (); doc_comment! { concat!( -"Converts a `", stringify!($Ty), "` into an `", stringify!($Int), +"Converts a `", stringify!($Int), "` into an `", stringify!($Ty), "` if it is non-zero"), fn try_from(int: $Int) -> Result { Self::new(int).ok_or(())