diff options
| author | Jimmy Cuadra <jimmy@jimmycuadra.com> | 2017-08-30 04:42:22 -0700 |
|---|---|---|
| committer | Jimmy Cuadra <jimmy@jimmycuadra.com> | 2017-08-30 04:42:25 -0700 |
| commit | b0edfce9505f8fdbfb0b6ea035b2949bc36ece5b (patch) | |
| tree | 6d150f37b5fb4904b40ef39a81deab9352289b46 /src | |
| parent | 80e3f8941d86bad0bd8d5551e6d066741ade1fc2 (diff) | |
| download | rust-b0edfce9505f8fdbfb0b6ea035b2949bc36ece5b.tar.gz rust-b0edfce9505f8fdbfb0b6ea035b2949bc36ece5b.zip | |
Implement TryFrom explicitly for infallible numeric conversions.
See https://github.com/rust-lang/rust/pull/44174#discussion_r135982787
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/num/mod.rs | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 4777f9d72cc..4372646a569 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<Self, Self::Error> { + 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); |
