Skip to content

Commit ecc48c5

Browse files
Add TryFrom implementation for bool, f32 and f64
1 parent b2c0707 commit ecc48c5

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

src/libcore/num/mod.rs

+113
Original file line numberDiff line numberDiff line change
@@ -2504,6 +2504,53 @@ impl fmt::Display for TryFromIntError {
25042504
}
25052505
}
25062506

2507+
/// This error will actually never be returned.
2508+
#[unstable(feature = "try_from", issue = "33417")]
2509+
#[derive(Debug, Copy, Clone)]
2510+
pub struct TryFromBoolError {}
2511+
2512+
impl TryFromBoolError {
2513+
#[unstable(feature = "bool_error_internals",
2514+
reason = "available through Error trait and this method should \
2515+
not be exposed publicly",
2516+
issue = "0")]
2517+
#[doc(hidden)]
2518+
pub fn __description(&self) -> &str {
2519+
"should not be used"
2520+
}
2521+
}
2522+
2523+
#[unstable(feature = "try_from", issue = "33417")]
2524+
impl fmt::Display for TryFromBoolError {
2525+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
2526+
self.__description().fmt(fmt)
2527+
}
2528+
}
2529+
2530+
#[unstable(feature = "try_from", issue = "33417")]
2531+
impl TryFrom<bool> for bool {
2532+
type Error = TryFromBoolError;
2533+
2534+
fn try_from(u: bool) -> Result<bool, TryFromBoolError> {
2535+
Ok(u)
2536+
}
2537+
}
2538+
2539+
macro_rules! try_from_for_bool {
2540+
($($source:ty),*) => {$(
2541+
#[unstable(feature = "try_from", issue = "33417")]
2542+
impl TryFrom<$source> for bool {
2543+
type Error = TryFromBoolError;
2544+
2545+
fn try_from(u: $source) -> Result<bool, TryFromBoolError> {
2546+
Ok(u != 0)
2547+
}
2548+
}
2549+
)*}
2550+
}
2551+
2552+
try_from_for_bool!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128);
2553+
25072554
macro_rules! same_sign_try_from_int_impl {
25082555
($storage:ty, $target:ty, $($source:ty),*) => {$(
25092556
#[unstable(feature = "try_from", issue = "33417")]
@@ -2536,6 +2583,19 @@ same_sign_try_from_int_impl!(i128, i128, i8, i16, i32, i64, i128, isize);
25362583
same_sign_try_from_int_impl!(u128, usize, u8, u16, u32, u64, u128, usize);
25372584
same_sign_try_from_int_impl!(i128, isize, i8, i16, i32, i64, i128, isize);
25382585

2586+
same_sign_try_from_int_impl!(f64, usize, f32, f64);
2587+
same_sign_try_from_int_impl!(f64, isize, f32, f64);
2588+
same_sign_try_from_int_impl!(f64, i128, f32, f64);
2589+
same_sign_try_from_int_impl!(f64, u128, f32, f64);
2590+
same_sign_try_from_int_impl!(f64, i64, f32, f64);
2591+
same_sign_try_from_int_impl!(f64, u64, f32, f64);
2592+
same_sign_try_from_int_impl!(f64, i32, f32, f64);
2593+
same_sign_try_from_int_impl!(f64, u32, f32, f64);
2594+
same_sign_try_from_int_impl!(f64, i16, f32, f64);
2595+
same_sign_try_from_int_impl!(f64, u16, f32, f64);
2596+
same_sign_try_from_int_impl!(f64, i8, f32, f64);
2597+
same_sign_try_from_int_impl!(f64, u8, f32, f64);
2598+
25392599
macro_rules! cross_sign_from_int_impl {
25402600
($unsigned:ty, $($signed:ty),*) => {$(
25412601
#[unstable(feature = "try_from", issue = "33417")]
@@ -2575,6 +2635,59 @@ cross_sign_from_int_impl!(u64, i8, i16, i32, i64, i128, isize);
25752635
cross_sign_from_int_impl!(u128, i8, i16, i32, i64, i128, isize);
25762636
cross_sign_from_int_impl!(usize, i8, i16, i32, i64, i128, isize);
25772637

2638+
/// The error type returned when a checked floating type conversion fails.
2639+
#[unstable(feature = "try_from", issue = "33417")]
2640+
#[derive(Debug, Copy, Clone)]
2641+
pub struct TryFromFloatError(());
2642+
2643+
impl TryFromFloatError {
2644+
#[unstable(feature = "float_error_internals",
2645+
reason = "available through Error trait and this method should \
2646+
not be exposed publicly",
2647+
issue = "0")]
2648+
#[doc(hidden)]
2649+
pub fn __description(&self) -> &str {
2650+
"out of range floating type conversion attempted"
2651+
}
2652+
}
2653+
2654+
#[unstable(feature = "try_from", issue = "33417")]
2655+
impl fmt::Display for TryFromFloatError {
2656+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
2657+
self.__description().fmt(fmt)
2658+
}
2659+
}
2660+
2661+
macro_rules! try_from_float_impl {
2662+
($storage:ty, $target:ident, $($source:ty),*) => {$(
2663+
#[unstable(feature = "try_from", issue = "33417")]
2664+
impl TryFrom<$source> for $target {
2665+
type Error = TryFromFloatError;
2666+
2667+
fn try_from(u: $source) -> Result<$target, TryFromFloatError> {
2668+
use $target;
2669+
2670+
if u.is_nan() {
2671+
Ok($target::NAN)
2672+
} else if u.is_infinite() {
2673+
Ok($target::INFINITY)
2674+
} else {
2675+
let min = $target::MIN as $storage;
2676+
let max = $target::MAX as $storage;
2677+
if u as $storage < min || u as $storage > max {
2678+
Err(TryFromFloatError(()))
2679+
} else {
2680+
Ok(u as $target)
2681+
}
2682+
}
2683+
}
2684+
}
2685+
)*}
2686+
}
2687+
2688+
try_from_float_impl!(f64, f32, f32, f64);
2689+
try_from_float_impl!(f64, f64, f32, f64);
2690+
25782691
#[doc(hidden)]
25792692
trait FromStrRadixHelper: PartialOrd + Copy {
25802693
fn min_value() -> Self;

0 commit comments

Comments
 (0)