@@ -147,22 +147,42 @@ impl_from!(i16 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.2
147147// https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-951.pdf
148148
149149// Note: integers can only be represented with full precision in a float if
150- // they fit in the significand, which is 24 bits in f32 and 53 bits in f64.
150+ // they fit in the significand, which is:
151+ // * 11 bits in f16
152+ // * 24 bits in f32
153+ // * 53 bits in f64
154+ // * 113 bits in f128
151155// Lossy float conversions are not implemented at this time.
156+ // FIXME(f16_f128): The `f16`/`f128` impls `#[stable]` attributes should be changed to reference
157+ // `f16`/`f128` when they are stabilised (trait impls have to have a `#[stable]` attribute, but none
158+ // of the `f16`/`f128` impls can be used on stable as the `f16` and `f128` types are unstable).
152159
153160// signed integer -> float
161+ impl_from ! ( i8 => f16, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
154162impl_from ! ( i8 => f32 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
155163impl_from ! ( i8 => f64 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
164+ impl_from ! ( i8 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
156165impl_from ! ( i16 => f32 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
157166impl_from ! ( i16 => f64 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
167+ impl_from ! ( i16 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
158168impl_from ! ( i32 => f64 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
169+ impl_from ! ( i32 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
170+ // FIXME(f16_f128): This impl would allow using `f128` on stable before it is stabilised.
171+ // impl_from!(i64 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
159172
160173// unsigned integer -> float
174+ impl_from ! ( u8 => f16, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
161175impl_from ! ( u8 => f32 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
162176impl_from ! ( u8 => f64 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
177+ impl_from ! ( u8 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
178+ impl_from ! ( u16 => f16, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
163179impl_from ! ( u16 => f32 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
164180impl_from ! ( u16 => f64 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
181+ impl_from ! ( u16 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
165182impl_from ! ( u32 => f64 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
183+ impl_from ! ( u32 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
184+ // FIXME(f16_f128): This impl would allow using `f128` on stable before it is stabilised.
185+ // impl_from!(u64 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
166186
167187// float -> float
168188// FIXME(f16_f128): adding additional `From<{float}>` impls to `f32` breaks inference. See
@@ -174,20 +194,27 @@ impl_from!(f32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0
174194impl_from ! ( f64 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
175195
176196macro_rules! impl_float_from_bool {
177- ( $float: ty) => {
197+ (
198+ $float: ty $( ;
199+ doctest_prefix: $( #[ doc = $doctest_prefix: literal] ) *
200+ doctest_suffix: $( #[ doc = $doctest_suffix: literal] ) *
201+ ) ?
202+ ) => {
178203 #[ stable( feature = "float_from_bool" , since = "1.68.0" ) ]
179204 impl From <bool > for $float {
180205 #[ doc = concat!( "Converts a [`bool`] to [`" , stringify!( $float) , "`] losslessly." ) ]
181206 /// The resulting value is positive `0.0` for `false` and `1.0` for `true` values.
182207 ///
183208 /// # Examples
184209 /// ```
210+ $( $( #[ doc = $doctest_prefix] ) * ) ?
185211 #[ doc = concat!( "let x: " , stringify!( $float) , " = false.into();" ) ]
186212 /// assert_eq!(x, 0.0);
187213 /// assert!(x.is_sign_positive());
188214 ///
189215 #[ doc = concat!( "let y: " , stringify!( $float) , " = true.into();" ) ]
190216 /// assert_eq!(y, 1.0);
217+ $( $( #[ doc = $doctest_suffix] ) * ) ?
191218 /// ```
192219 #[ inline]
193220 fn from( small: bool ) -> Self {
@@ -198,8 +225,27 @@ macro_rules! impl_float_from_bool {
198225}
199226
200227// boolean -> float
228+ impl_float_from_bool ! (
229+ f16;
230+ doctest_prefix:
231+ // rustdoc doesn't remove the conventional space after the `///`
232+ ///#![feature(f16)]
233+ ///# #[cfg(all(target_arch = "x86_64", target_os = "linux"))] {
234+ ///
235+ doctest_suffix:
236+ ///# }
237+ ) ;
201238impl_float_from_bool ! ( f32 ) ;
202239impl_float_from_bool ! ( f64 ) ;
240+ impl_float_from_bool ! (
241+ f128;
242+ doctest_prefix:
243+ ///#![feature(f128)]
244+ ///# #[cfg(all(target_arch = "x86_64", target_os = "linux"))] {
245+ ///
246+ doctest_suffix:
247+ ///# }
248+ ) ;
203249
204250// no possible bounds violation
205251macro_rules! impl_try_from_unbounded {
0 commit comments