Skip to content

Commit

Permalink
wip add try from methods
Browse files Browse the repository at this point in the history
  • Loading branch information
benmkw committed Oct 22, 2020
1 parent 2ad63fb commit 7ea092a
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,104 @@ from_primitive_integer!(u64, approximate_float_unsigned);
from_primitive_integer!(u128, approximate_float_unsigned);
from_primitive_integer!(usize, approximate_float_unsigned);

macro_rules! try_from_impl {
($typ:ty, $approx:ident) => {
// impl TryFrom<i32> for Ratio<$typ> {
// type Error = ();
// fn try_from(n: i32) -> Result<Self, ()> {
// <$typ as FromPrimitive>::from_i32(n)
// .map(Ratio::from_integer)
// .ok_or(())
// }
// }

impl TryFrom<i64> for Ratio<$typ> {
type Error = ();
fn try_from(n: i64) -> Result<Self, ()> {
<$typ as FromPrimitive>::from_i64(n)
.map(Ratio::from_integer)
.ok_or(())
}
}

impl TryFrom<i128> for Ratio<$typ> {
type Error = ();
fn try_from(n: i128) -> Result<Self, ()> {
<$typ as FromPrimitive>::from_i128(n)
.map(Ratio::from_integer)
.ok_or(())
}
}

// impl TryFrom<u32> for Ratio<$typ> {
// type Error = ();
// fn try_from(n: u32) -> Result<Self, ()> {
// <$typ as FromPrimitive>::from_u32(n)
// .map(Ratio::from_integer)
// .ok_or(())
// }
// }

impl TryFrom<u64> for Ratio<$typ> {
type Error = ();
fn try_from(n: u64) -> Result<Self, ()> {
<$typ as FromPrimitive>::from_u64(n)
.map(Ratio::from_integer)
.ok_or(())
}
}

impl TryFrom<u128> for Ratio<$typ> {
type Error = ();
fn try_from(n: u128) -> Result<Self, ()> {
<$typ as FromPrimitive>::from_u128(n)
.map(Ratio::from_integer)
.ok_or(())
}
}

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

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

// TODO
// need to exclude generation of the version for the same value
try_from_impl!(i8, approximate_float);
try_from_impl!(i16, approximate_float);
try_from_impl!(i32, approximate_float);
try_from_impl!(i64, approximate_float);
// try_from_impl!(i128, approximate_float);
try_from_impl!(isize, approximate_float);

try_from_impl!(u8, approximate_float_unsigned);
try_from_impl!(u16, approximate_float_unsigned);
try_from_impl!(u32, approximate_float_unsigned);
// try_from_impl!(u64, approximate_float_unsigned);
// try_from_impl!(u128, approximate_float_unsigned);
try_from_impl!(usize, approximate_float_unsigned);

#[test]
fn try_from_impl() {
debug_assert_eq!(Err(()), Ratio::<i8>::try_from(1000000i64));
debug_assert_eq!(
Ok(Ratio::<i8>::from_integer(11)),
Ratio::<i8>::try_from(11i64)
);
}

impl<T: Integer + Signed + Bounded + NumCast + Clone> Ratio<T> {
pub fn approximate_float<F: FloatCore + NumCast>(f: F) -> Option<Ratio<T>> {
// 1/10e-20 < 1/2**32 which seems like a good default, and 30 seems
Expand Down

0 comments on commit 7ea092a

Please sign in to comment.