Skip to content

Commit

Permalink
add From and TryFrom methods
Browse files Browse the repository at this point in the history
  • Loading branch information
benmkw committed Nov 5, 2020
1 parent 2ad63fb commit eec17f4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ edition = "2018"
features = ["std", "num-bigint-std", "serde"]

[dependencies]
paste = "1.0"

[dependencies.num-bigint]
optional = true
Expand Down
77 changes: 77 additions & 0 deletions src/from.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::{approximate_float, approximate_float_unsigned, FromPrimitive, Ratio};
use std::convert::TryFrom;

macro_rules! impl_try_from {
( $($name:ty),* => $into:ty ; $approx:ident) => {
$(
impl TryFrom<$name> for Ratio<$into> {
type Error = ();
paste::paste! {
fn try_from(n: $name) -> Result<Self, ()> {
<$into as FromPrimitive>::[< from_ $name>](n)
.map(Ratio::from_integer)
.ok_or(())
}
}
}
)*

impl TryFrom<f32> for Ratio<$into> {
type Error = ();
fn try_from(n: f32) -> Result<Self, ()> {
$approx(n, 10e-20, 30).ok_or(())
}
}

impl TryFrom<f64> for Ratio<$into> {
type Error = ();
fn try_from(n: f64) -> Result<Self, ()> {
$approx(n, 10e-20, 30).ok_or(())
}
}

};
}

impl_try_from!(i8, u16, i16, u32, i32, u64, i64, u128, i128 => u8 ; approximate_float_unsigned);
impl_try_from!(u8, u16, i16, u32, i32, u64, i64, u128, i128 => i8 ; approximate_float);

impl_try_from!(i16, u32, i32, u64, i64, u128, i128 => u16 ; approximate_float_unsigned);
impl_try_from!(u16, u32, i32, u64, i64, u128, i128 => i16 ; approximate_float);

impl_try_from!(i32, u64, i64, u128, i128 => u32 ; approximate_float_unsigned);
impl_try_from!(u32, u64, i64, u128, i128 => i32 ; approximate_float);

impl_try_from!(i64, u128, i128 => u64 ; approximate_float_unsigned);
impl_try_from!(u64, u128, i128 => i64 ; approximate_float);

impl_try_from!(i128 => u128 ; approximate_float_unsigned);
impl_try_from!(u128 => i128 ; approximate_float);

macro_rules! impl_from {
( $($name:ty),* => $into:ty) => {
$(
impl From<$name> for Ratio<$into> {
paste::paste! {
fn from(n: $name) -> Self {
<$into as FromPrimitive>::[< from_ $name>](n)
.map(Ratio::from_integer)
.unwrap()
}
}
}
)*
};
}

impl_from!(u8, u16, u32, u64 => u128);
impl_from!(u8, i8, u16, i16, u32, i32, u64, i64 => i128);

impl_from!(u8, u16, u32 => u64);
impl_from!(u8, i8, u16, i16, u32, i32 => i64);

impl_from!(u8, u16 => u32);
impl_from!(u8, i8, u16, i16 => i32);

impl_from!(u8 => u16);
impl_from!(u8, i8 => i16);
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use num_traits::{
Pow, Signed, Zero,
};

mod from;
mod pow;

/// Represents the ratio between two numbers.
Expand Down

0 comments on commit eec17f4

Please sign in to comment.