@@ -156,22 +156,42 @@ impl_from!(i16 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.2
156156// https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-951.pdf
157157
158158// Note: integers can only be represented with full precision in a float if
159- // they fit in the significand, which is 24 bits in f32 and 53 bits in f64.
159+ // they fit in the significand, which is:
160+ // * 11 bits in f16
161+ // * 24 bits in f32
162+ // * 53 bits in f64
163+ // * 113 bits in f128
160164// Lossy float conversions are not implemented at this time.
165+ // FIXME(f16_f128): The `f16`/`f128` impls `#[stable]` attributes should be changed to reference
166+ // `f16`/`f128` when they are stabilised (trait impls have to have a `#[stable]` attribute, but none
167+ // of the `f16`/`f128` impls can be used on stable as the `f16` and `f128` types are unstable).
161168
162169// signed integer -> float
170+ impl_from ! ( i8 => f16, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
163171impl_from ! ( i8 => f32 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
164172impl_from ! ( i8 => f64 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
173+ impl_from ! ( i8 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
165174impl_from ! ( i16 => f32 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
166175impl_from ! ( i16 => f64 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
176+ impl_from ! ( i16 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
167177impl_from ! ( i32 => f64 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
178+ impl_from ! ( i32 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
179+ // FIXME(f16_f128): This impl would allow using `f128` on stable before it is stabilised.
180+ // impl_from!(i64 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
168181
169182// unsigned integer -> float
183+ impl_from ! ( u8 => f16, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
170184impl_from ! ( u8 => f32 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
171185impl_from ! ( u8 => f64 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
186+ impl_from ! ( u8 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
187+ impl_from ! ( u16 => f16, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
172188impl_from ! ( u16 => f32 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
173189impl_from ! ( u16 => f64 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
190+ impl_from ! ( u16 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
174191impl_from ! ( u32 => f64 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
192+ impl_from ! ( u32 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
193+ // FIXME(f16_f128): This impl would allow using `f128` on stable before it is stabilised.
194+ // impl_from!(u64 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0")]);
175195
176196// float -> float
177197// FIXME(f16_f128): adding additional `From<{float}>` impls to `f32` breaks inference. See
@@ -183,20 +203,27 @@ impl_from!(f32 => f128, #[stable(feature = "lossless_float_conv", since = "1.6.0
183203impl_from ! ( f64 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
184204
185205macro_rules! impl_float_from_bool {
186- ( $float: ty) => {
206+ (
207+ $float: ty $( ;
208+ doctest_prefix: $( #[ doc = $doctest_prefix: literal] ) *
209+ doctest_suffix: $( #[ doc = $doctest_suffix: literal] ) *
210+ ) ?
211+ ) => {
187212 #[ stable( feature = "float_from_bool" , since = "1.68.0" ) ]
188213 impl From <bool > for $float {
189214 #[ doc = concat!( "Converts a [`bool`] to [`" , stringify!( $float) , "`] losslessly." ) ]
190215 /// The resulting value is positive `0.0` for `false` and `1.0` for `true` values.
191216 ///
192217 /// # Examples
193218 /// ```
219+ $( $( #[ doc = $doctest_prefix] ) * ) ?
194220 #[ doc = concat!( "let x: " , stringify!( $float) , " = false.into();" ) ]
195221 /// assert_eq!(x, 0.0);
196222 /// assert!(x.is_sign_positive());
197223 ///
198224 #[ doc = concat!( "let y: " , stringify!( $float) , " = true.into();" ) ]
199225 /// assert_eq!(y, 1.0);
226+ $( $( #[ doc = $doctest_suffix] ) * ) ?
200227 /// ```
201228 #[ inline]
202229 fn from( small: bool ) -> Self {
@@ -207,8 +234,27 @@ macro_rules! impl_float_from_bool {
207234}
208235
209236// boolean -> float
237+ impl_float_from_bool ! (
238+ f16;
239+ doctest_prefix:
240+ // rustdoc doesn't remove the conventional space after the `///`
241+ ///#![feature(f16)]
242+ ///# #[cfg(all(target_arch = "x86_64", target_os = "linux"))] {
243+ ///
244+ doctest_suffix:
245+ ///# }
246+ ) ;
210247impl_float_from_bool ! ( f32 ) ;
211248impl_float_from_bool ! ( f64 ) ;
249+ impl_float_from_bool ! (
250+ f128;
251+ doctest_prefix:
252+ ///#![feature(f128)]
253+ ///# #[cfg(all(target_arch = "x86_64", target_os = "linux"))] {
254+ ///
255+ doctest_suffix:
256+ ///# }
257+ ) ;
212258
213259// no possible bounds violation
214260macro_rules! impl_try_from_unbounded {
0 commit comments