about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-12-21 14:27:57 +0000
committerbors <bors@rust-lang.org>2022-12-21 14:27:57 +0000
commit49143814e145c711e9807fac6467c14090663796 (patch)
tree6150bfe7e4adf1ed094791edad23f37119523259
parent1d12c3cea30b8ba4a09650a9e9c46fe9fbe25f0b (diff)
parentb134d1108ffc92bfb5081d31e5e41141f918e942 (diff)
downloadrust-49143814e145c711e9807fac6467c14090663796.tar.gz
rust-49143814e145c711e9807fac6467c14090663796.zip
Auto merge of #100390 - jhpratt:float-from-bool, r=dtolnay
Implement `From<bool>` for f32, f64

As is required for trait implementations, these are insta-stable. Given there is a release tomorrow and this needs FCP, I set 1.65 as the stable version.

`@rustbot` label +A-floating-point +C-feature-request +needs-fcp +relnotes +S-waiting-on-review +T-libs-api -T-libs
-rw-r--r--library/core/src/convert/num.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/library/core/src/convert/num.rs b/library/core/src/convert/num.rs
index 9c0d7e9a1e8..45e2f711c6c 100644
--- a/library/core/src/convert/num.rs
+++ b/library/core/src/convert/num.rs
@@ -168,6 +168,26 @@ impl_from! { u32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0"
 // Float -> Float
 impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
 
+// bool -> Float
+#[stable(feature = "float_from_bool", since = "CURRENT_RUSTC_VERSION")]
+#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
+impl const From<bool> for f32 {
+    /// Converts `bool` to `f32` losslessly.
+    #[inline]
+    fn from(small: bool) -> Self {
+        small as u8 as Self
+    }
+}
+#[stable(feature = "float_from_bool", since = "CURRENT_RUSTC_VERSION")]
+#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
+impl const From<bool> for f64 {
+    /// Converts `bool` to `f64` losslessly.
+    #[inline]
+    fn from(small: bool) -> Self {
+        small as u8 as Self
+    }
+}
+
 // no possible bounds violation
 macro_rules! try_from_unbounded {
     ($source:ty, $($target:ty),*) => {$(