Implement TryFrom explicitly for infallible numeric conversions.

See https://github.com/rust-lang/rust/pull/44174#discussion_r135982787
This commit is contained in:
Jimmy Cuadra
2017-08-30 04:42:22 -07:00
parent 80e3f8941d
commit b0edfce950

View File

@@ -2507,10 +2507,12 @@ impl fmt::Display for TryFromIntError {
macro_rules! try_from_unbounded { macro_rules! try_from_unbounded {
($source:ty, $($target:ty),*) => {$( ($source:ty, $($target:ty),*) => {$(
#[unstable(feature = "try_from", issue = "33417")] #[unstable(feature = "try_from", issue = "33417")]
impl From<$source> for $target { impl TryFrom<$source> for $target {
type Error = Infallible;
#[inline] #[inline]
fn from(value: $source) -> $target { fn try_from(value: $source) -> Result<Self, Self::Error> {
value as $target Ok(value as $target)
} }
} }
)*} )*}
@@ -2617,7 +2619,7 @@ try_from_lower_bounded!(isize, usize);
#[cfg(target_pointer_width = "16")] #[cfg(target_pointer_width = "16")]
mod ptr_try_from_impls { mod ptr_try_from_impls {
use super::TryFromIntError; use super::TryFromIntError;
use convert::TryFrom; use convert::{Infallible, TryFrom};
try_from_upper_bounded!(usize, u8); try_from_upper_bounded!(usize, u8);
try_from_unbounded!(usize, u16, u32, u64, u128); try_from_unbounded!(usize, u16, u32, u64, u128);
@@ -2643,7 +2645,7 @@ mod ptr_try_from_impls {
#[cfg(target_pointer_width = "32")] #[cfg(target_pointer_width = "32")]
mod ptr_try_from_impls { mod ptr_try_from_impls {
use super::TryFromIntError; use super::TryFromIntError;
use convert::TryFrom; use convert::{Infallible, TryFrom};
try_from_upper_bounded!(usize, u8, u16); try_from_upper_bounded!(usize, u8, u16);
try_from_unbounded!(usize, u32, u64, u128); try_from_unbounded!(usize, u32, u64, u128);
@@ -2669,7 +2671,7 @@ mod ptr_try_from_impls {
#[cfg(target_pointer_width = "64")] #[cfg(target_pointer_width = "64")]
mod ptr_try_from_impls { mod ptr_try_from_impls {
use super::TryFromIntError; use super::TryFromIntError;
use convert::TryFrom; use convert::{Infallible, TryFrom};
try_from_upper_bounded!(usize, u8, u16, u32); try_from_upper_bounded!(usize, u8, u16, u32);
try_from_unbounded!(usize, u64, u128); try_from_unbounded!(usize, u64, u128);