Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement From<bool> for f32, f64 #100390

Merged
merged 1 commit into from Dec 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions library/core/src/convert/num.rs
Expand Up @@ -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),*) => {$(
Expand Down