Skip to content

Commit

Permalink
Support the same conversions from integer types as f64 and f32. (#…
Browse files Browse the repository at this point in the history
…135)

The standard `f64` and `f32` types support conversion from integer types
for which the conversion will not lose precision, as well as from `bool`.
This commit adds that same support to `OrderedFloat`.
  • Loading branch information
blp committed Aug 17, 2023
1 parent e57fd99 commit d7768e7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,38 @@ impl<T: Float> From<T> for OrderedFloat<T> {
}
}

impl From<bool> for OrderedFloat<f32> {
fn from(val: bool) -> Self {
OrderedFloat(val as u8 as f32)
}
}

impl From<bool> for OrderedFloat<f64> {
fn from(val: bool) -> Self {
OrderedFloat(val as u8 as f64)
}
}

macro_rules! impl_ordered_float_from {
($dst:ty, $src:ty) => {
impl From<$src> for OrderedFloat<$dst> {
fn from(val: $src) -> Self {
OrderedFloat(val.into())
}
}
};
}
impl_ordered_float_from! {f64, i8}
impl_ordered_float_from! {f64, i16}
impl_ordered_float_from! {f64, i32}
impl_ordered_float_from! {f64, u8}
impl_ordered_float_from! {f64, u16}
impl_ordered_float_from! {f64, u32}
impl_ordered_float_from! {f32, i8}
impl_ordered_float_from! {f32, i16}
impl_ordered_float_from! {f32, u8}
impl_ordered_float_from! {f32, u16}

impl<T: Float> Deref for OrderedFloat<T> {
type Target = T;

Expand Down

0 comments on commit d7768e7

Please sign in to comment.