diff options
| author | bors <bors@rust-lang.org> | 2015-10-29 18:55:12 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2015-10-29 18:55:12 +0000 |
| commit | 4d11db650137a8b515fd6cbef60bccd9bbe37869 (patch) | |
| tree | 19604ad7399bd745877503264f04fcf6383459d7 /src/libcore/num | |
| parent | a18e0b27077c763df4f9e06b42192717025f0bfc (diff) | |
| parent | 1a19f9877ad1d582ac80f8e765eff879e00222ea (diff) | |
| download | rust-4d11db650137a8b515fd6cbef60bccd9bbe37869.tar.gz rust-4d11db650137a8b515fd6cbef60bccd9bbe37869.zip | |
Auto merge of #29129 - cuviper:impl-from-for-floats, r=alexcrichton
This is a spiritual successor to #28921, completing the "upcast" idea from rust-num/num#97.
Diffstat (limited to 'src/libcore/num')
| -rw-r--r-- | src/libcore/num/mod.rs | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index de56cf902a4..801315d86db 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -1473,14 +1473,14 @@ impl fmt::Display for ParseIntError { pub use num::dec2flt::ParseFloatError; -// Conversion traits for primitive integer types +// Conversion traits for primitive integer and float types // Conversions T -> T are covered by a blanket impl and therefore excluded // Some conversions from and to usize/isize are not implemented due to portability concerns macro_rules! impl_from { ($Small: ty, $Large: ty) => { - #[stable(feature = "lossless_int_conv", since = "1.5.0")] + #[stable(feature = "lossless_prim_conv", since = "1.5.0")] impl From<$Small> for $Large { - #[stable(feature = "lossless_int_conv", since = "1.5.0")] + #[stable(feature = "lossless_prim_conv", since = "1.5.0")] #[inline] fn from(small: $Small) -> $Large { small as $Large @@ -1514,3 +1514,24 @@ impl_from! { u8, i64 } impl_from! { u16, i32 } impl_from! { u16, i64 } impl_from! { u32, i64 } + +// Note: integers can only be represented with full precision in a float if +// they fit in the significand, which is 24 bits in f32 and 53 bits in f64. +// Lossy float conversions are not implemented at this time. + +// Signed -> Float +impl_from! { i8, f32 } +impl_from! { i8, f64 } +impl_from! { i16, f32 } +impl_from! { i16, f64 } +impl_from! { i32, f64 } + +// Unsigned -> Float +impl_from! { u8, f32 } +impl_from! { u8, f64 } +impl_from! { u16, f32 } +impl_from! { u16, f64 } +impl_from! { u32, f64 } + +// Float -> Float +impl_from! { f32, f64 } |
