Skip to content

Commit

Permalink
Add examples section which demonstrates the behaviour (specifically t…
Browse files Browse the repository at this point in the history
…he sign positive aspect)
  • Loading branch information
bmoxb committed Mar 9, 2023
1 parent ea5bba5 commit 73e0833
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions core/src/convert/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ impl_from! { f32, f64, #[stable(feature = "lossless_float_conv", since = "1.6.0"
impl const From<bool> for f32 {
/// Converts `bool` to `f32` losslessly. The resulting value is positive
/// `0.0` for `false` and `1.0` for `true` values.
///
/// # Examples
/// ```
/// let x: f32 = false.into();
/// assert_eq!(x, 0.0);
/// assert!(x.is_sign_positive());
///
/// let y: f32 = true.into();
/// assert_eq!(y, 1.0);
/// ```
#[inline]
fn from(small: bool) -> Self {
small as u8 as Self
Expand All @@ -184,6 +194,16 @@ impl const From<bool> for f32 {
impl const From<bool> for f64 {
/// Converts `bool` to `f64` losslessly. The resulting value is positive
/// `0.0` for `false` and `1.0` for `true` values.
///
/// # Examples
/// ```
/// let x: f64 = false.into();
/// assert_eq!(x, 0.0);
/// assert!(x.is_sign_positive());
///
/// let y: f64 = true.into();
/// assert_eq!(y, 1.0);
/// ```
#[inline]
fn from(small: bool) -> Self {
small as u8 as Self
Expand Down

0 comments on commit 73e0833

Please sign in to comment.