Skip to content

Commit b134d11

Browse files
committed
Implement From<bool> for f32, f64
1 parent 16990de commit b134d11

File tree

1 file changed

+20
-0
lines changed
  • library/core/src/convert

1 file changed

+20
-0
lines changed

Diff for: library/core/src/convert/num.rs

+20
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,26 @@ impl_from! { u32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0"
168168
// Float -> Float
169169
impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0")] }
170170

171+
// bool -> Float
172+
#[stable(feature = "float_from_bool", since = "CURRENT_RUSTC_VERSION")]
173+
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
174+
impl const From<bool> for f32 {
175+
/// Converts `bool` to `f32` losslessly.
176+
#[inline]
177+
fn from(small: bool) -> Self {
178+
small as u8 as Self
179+
}
180+
}
181+
#[stable(feature = "float_from_bool", since = "CURRENT_RUSTC_VERSION")]
182+
#[rustc_const_unstable(feature = "const_num_from_num", issue = "87852")]
183+
impl const From<bool> for f64 {
184+
/// Converts `bool` to `f64` losslessly.
185+
#[inline]
186+
fn from(small: bool) -> Self {
187+
small as u8 as Self
188+
}
189+
}
190+
171191
// no possible bounds violation
172192
macro_rules! try_from_unbounded {
173193
($source:ty, $($target:ty),*) => {$(

0 commit comments

Comments
 (0)