Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4338,6 +4338,44 @@ mod ptr_try_from_impls {
rev!(try_from_both_bounded, usize, i128);
}

/// The error type returned when a checked float type conversion fails.
#[unstable(feature = "try_from_float", issue = "0")]
#[derive(Debug, Copy, Clone)]
pub struct TryFromFloatError(());

impl TryFromFloatError {
#[unstable(feature = "float_error_internals",
reason = "available through Error trait and this method should \
not be exposed publicly",
issue = "0")]
#[doc(hidden)]
pub fn __description(&self) -> &str {
"lossy float type conversion attempted"
}
}

#[unstable(feature = "try_from_float", issue = "0")]
impl fmt::Display for TryFromFloatError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.__description().fmt(fmt)
}
}

#[stable(feature = "try_from_float_impl", since = "1.28.0")]
impl TryFrom<f64> for f32 {
type Error = TryFromFloatError;

#[inline]
fn try_from(x: f64) -> Result<f32, TryFromFloatError> {
let y = x as f32;
if y as f64 == x {
Ok(y)
} else {
Err(TryFromFloatError(()))
}
}
}

#[doc(hidden)]
trait FromStrRadixHelper: PartialOrd + Copy {
fn min_value() -> Self;
Expand Down
7 changes: 7 additions & 0 deletions src/libstd/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ impl Error for num::TryFromIntError {
}
}

#[unstable(feature = "try_from_float", issue = "0")]
impl Error for num::TryFromFloatError {
fn description(&self) -> &str {
self.__description()
}
}

#[unstable(feature = "try_from", issue = "33417")]
impl Error for array::TryFromSliceError {
fn description(&self) -> &str {
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@
#![feature(external_doc)]
#![feature(fs_read_write)]
#![feature(fixed_size_array)]
#![feature(float_error_internals)]
#![feature(float_from_str_radix)]
#![cfg_attr(stage0, feature(float_internals))]
#![feature(fn_traits)]
Expand Down Expand Up @@ -308,6 +309,7 @@
#![feature(thread_local)]
#![feature(toowned_clone_into)]
#![feature(try_from)]
#![feature(try_from_float)]
#![feature(try_reserve)]
#![feature(unboxed_closures)]
#![feature(untagged_unions)]
Expand Down
2 changes: 2 additions & 0 deletions src/libstd/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#[stable(feature = "rust1", since = "1.0.0")]
pub use core::num::{FpCategory, ParseIntError, ParseFloatError, TryFromIntError};
#[unstable(feature = "try_from_float", issue = "0")]
pub use core::num::TryFromFloatError;
#[stable(feature = "rust1", since = "1.0.0")]
pub use core::num::Wrapping;

Expand Down