From 8c3bdcc35a96d42cb089405530a0016b1113358f Mon Sep 17 00:00:00 2001 From: Clar Charr Date: Tue, 8 May 2018 21:38:22 -0400 Subject: [PATCH 1/3] Add From for int types --- src/libcore/num/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 013d0334d4164..06447dcdce998 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -4454,6 +4454,20 @@ macro_rules! impl_from { } } +// Bool -> Any +impl_from! { bool, u8, #[stable(feature = "from_bool", since = "1.28.0")] } +impl_from! { bool, u16, #[stable(feature = "from_bool", since = "1.28.0")] } +impl_from! { bool, u32, #[stable(feature = "from_bool", since = "1.28.0")] } +impl_from! { bool, u64, #[stable(feature = "from_bool", since = "1.28.0")] } +impl_from! { bool, u128, #[stable(feature = "from_bool", since = "1.28.0")] } +impl_from! { bool, usize, #[stable(feature = "from_bool", since = "1.28.0")] } +impl_from! { bool, i8, #[stable(feature = "from_bool", since = "1.28.0")] } +impl_from! { bool, i16, #[stable(feature = "from_bool", since = "1.28.0")] } +impl_from! { bool, i32, #[stable(feature = "from_bool", since = "1.28.0")] } +impl_from! { bool, i64, #[stable(feature = "from_bool", since = "1.28.0")] } +impl_from! { bool, i128, #[stable(feature = "from_bool", since = "1.28.0")] } +impl_from! { bool, isize, #[stable(feature = "from_bool", since = "1.28.0")] } + // Unsigned -> Unsigned impl_from! { u8, u16, #[stable(feature = "lossless_int_conv", since = "1.5.0")] } impl_from! { u8, u32, #[stable(feature = "lossless_int_conv", since = "1.5.0")] } From 7fe56e81b74e90b78d268e1749ee0de3e2ff54cd Mon Sep 17 00:00:00 2001 From: Clar Charr Date: Sun, 20 May 2018 14:52:08 -0400 Subject: [PATCH 2/3] Fix ambiguity in Result test --- src/libcore/tests/result.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libcore/tests/result.rs b/src/libcore/tests/result.rs index ce41bde8342ed..d9927ce4487e9 100644 --- a/src/libcore/tests/result.rs +++ b/src/libcore/tests/result.rs @@ -220,13 +220,15 @@ fn test_try() { assert_eq!(try_result_none(), None); fn try_result_ok() -> Result { - let val = Ok(1)?; + let result: Result = Ok(1); + let val = result?; Ok(val) } assert_eq!(try_result_ok(), Ok(1)); fn try_result_err() -> Result { - let val = Err(1)?; + let result: Result = Err(1); + let val = result?; Ok(val) } assert_eq!(try_result_err(), Err(1)); From b1797d57ffb42a063a8ecc8cc5f9d2b625708c72 Mon Sep 17 00:00:00 2001 From: Clar Charr Date: Sun, 20 May 2018 14:56:46 -0400 Subject: [PATCH 3/3] Add @ithinuel's tests from #50597 --- src/libcore/tests/num/mod.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/libcore/tests/num/mod.rs b/src/libcore/tests/num/mod.rs index f3439890fce50..b5e6a019a228c 100644 --- a/src/libcore/tests/num/mod.rs +++ b/src/libcore/tests/num/mod.rs @@ -134,6 +134,15 @@ fn test_infallible_try_from_int_error() { } macro_rules! test_impl_from { + ($fn_name:ident, bool, $target: ty) => { + #[test] + fn $fn_name() { + let one: $target = 1; + let zero: $target = 0; + assert_eq!(one, <$target>::from(true)); + assert_eq!(zero, <$target>::from(false)); + } + }; ($fn_name: ident, $Small: ty, $Large: ty) => { #[test] fn $fn_name() { @@ -173,6 +182,18 @@ test_impl_from! { test_u16i32, u16, i32 } test_impl_from! { test_u16i64, u16, i64 } test_impl_from! { test_u32i64, u32, i64 } +// Bool -> Integer +test_impl_from! { test_boolu8, bool, u8 } +test_impl_from! { test_boolu16, bool, u16 } +test_impl_from! { test_boolu32, bool, u32 } +test_impl_from! { test_boolu64, bool, u64 } +test_impl_from! { test_boolu128, bool, u128 } +test_impl_from! { test_booli8, bool, i8 } +test_impl_from! { test_booli16, bool, i16 } +test_impl_from! { test_booli32, bool, i32 } +test_impl_from! { test_booli64, bool, i64 } +test_impl_from! { test_booli128, bool, i128 } + // Signed -> Float test_impl_from! { test_i8f32, i8, f32 } test_impl_from! { test_i8f64, i8, f64 }