From b0edfce9505f8fdbfb0b6ea035b2949bc36ece5b Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Wed, 30 Aug 2017 04:42:22 -0700 Subject: [PATCH] Implement TryFrom explicitly for infallible numeric conversions. See https://github.com/rust-lang/rust/pull/44174#discussion_r135982787 --- src/libcore/num/mod.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 4777f9d72ccd..4372646a5691 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -2507,10 +2507,12 @@ impl fmt::Display for TryFromIntError { macro_rules! try_from_unbounded { ($source:ty, $($target:ty),*) => {$( #[unstable(feature = "try_from", issue = "33417")] - impl From<$source> for $target { + impl TryFrom<$source> for $target { + type Error = Infallible; + #[inline] - fn from(value: $source) -> $target { - value as $target + fn try_from(value: $source) -> Result { + Ok(value as $target) } } )*} @@ -2617,7 +2619,7 @@ try_from_lower_bounded!(isize, usize); #[cfg(target_pointer_width = "16")] mod ptr_try_from_impls { use super::TryFromIntError; - use convert::TryFrom; + use convert::{Infallible, TryFrom}; try_from_upper_bounded!(usize, u8); try_from_unbounded!(usize, u16, u32, u64, u128); @@ -2643,7 +2645,7 @@ mod ptr_try_from_impls { #[cfg(target_pointer_width = "32")] mod ptr_try_from_impls { use super::TryFromIntError; - use convert::TryFrom; + use convert::{Infallible, TryFrom}; try_from_upper_bounded!(usize, u8, u16); try_from_unbounded!(usize, u32, u64, u128); @@ -2669,7 +2671,7 @@ mod ptr_try_from_impls { #[cfg(target_pointer_width = "64")] mod ptr_try_from_impls { use super::TryFromIntError; - use convert::TryFrom; + use convert::{Infallible, TryFrom}; try_from_upper_bounded!(usize, u8, u16, u32); try_from_unbounded!(usize, u64, u128);