@@ -147,22 +147,42 @@ impl_from!(i16 => isize, #[stable(feature = "lossless_iusize_conv", since = "1.2
147
147
// https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-951.pdf
148
148
149
149
// 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
151
155
// 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).
152
159
153
160
// signed integer -> float
161
+ impl_from ! ( i8 => f16, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
154
162
impl_from ! ( i8 => f32 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
155
163
impl_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" ) ] ) ;
156
165
impl_from ! ( i16 => f32 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
157
166
impl_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" ) ] ) ;
158
168
impl_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")]);
159
172
160
173
// unsigned integer -> float
174
+ impl_from ! ( u8 => f16, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
161
175
impl_from ! ( u8 => f32 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
162
176
impl_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" ) ] ) ;
163
179
impl_from ! ( u16 => f32 , #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
164
180
impl_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" ) ] ) ;
165
182
impl_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")]);
166
186
167
187
// float -> float
168
188
// 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
174
194
impl_from ! ( f64 => f128, #[ stable( feature = "lossless_float_conv" , since = "1.6.0" ) ] ) ;
175
195
176
196
macro_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
+ ) => {
178
203
#[ stable( feature = "float_from_bool" , since = "1.68.0" ) ]
179
204
impl From <bool > for $float {
180
205
#[ doc = concat!( "Converts a [`bool`] to [`" , stringify!( $float) , "`] losslessly." ) ]
181
206
/// The resulting value is positive `0.0` for `false` and `1.0` for `true` values.
182
207
///
183
208
/// # Examples
184
209
/// ```
210
+ $( $( #[ doc = $doctest_prefix] ) * ) ?
185
211
#[ doc = concat!( "let x: " , stringify!( $float) , " = false.into();" ) ]
186
212
/// assert_eq!(x, 0.0);
187
213
/// assert!(x.is_sign_positive());
188
214
///
189
215
#[ doc = concat!( "let y: " , stringify!( $float) , " = true.into();" ) ]
190
216
/// assert_eq!(y, 1.0);
217
+ $( $( #[ doc = $doctest_suffix] ) * ) ?
191
218
/// ```
192
219
#[ inline]
193
220
fn from( small: bool ) -> Self {
@@ -198,8 +225,27 @@ macro_rules! impl_float_from_bool {
198
225
}
199
226
200
227
// 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
+ ) ;
201
238
impl_float_from_bool ! ( f32 ) ;
202
239
impl_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
+ ) ;
203
249
204
250
// no possible bounds violation
205
251
macro_rules! impl_try_from_unbounded {
0 commit comments