Skip to content

Commit eec17f4

Browse files
committed
add From and TryFrom methods
1 parent 2ad63fb commit eec17f4

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ edition = "2018"
1717
features = ["std", "num-bigint-std", "serde"]
1818

1919
[dependencies]
20+
paste = "1.0"
2021

2122
[dependencies.num-bigint]
2223
optional = true

Diff for: src/from.rs

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use crate::{approximate_float, approximate_float_unsigned, FromPrimitive, Ratio};
2+
use std::convert::TryFrom;
3+
4+
macro_rules! impl_try_from {
5+
( $($name:ty),* => $into:ty ; $approx:ident) => {
6+
$(
7+
impl TryFrom<$name> for Ratio<$into> {
8+
type Error = ();
9+
paste::paste! {
10+
fn try_from(n: $name) -> Result<Self, ()> {
11+
<$into as FromPrimitive>::[< from_ $name>](n)
12+
.map(Ratio::from_integer)
13+
.ok_or(())
14+
}
15+
}
16+
}
17+
)*
18+
19+
impl TryFrom<f32> for Ratio<$into> {
20+
type Error = ();
21+
fn try_from(n: f32) -> Result<Self, ()> {
22+
$approx(n, 10e-20, 30).ok_or(())
23+
}
24+
}
25+
26+
impl TryFrom<f64> for Ratio<$into> {
27+
type Error = ();
28+
fn try_from(n: f64) -> Result<Self, ()> {
29+
$approx(n, 10e-20, 30).ok_or(())
30+
}
31+
}
32+
33+
};
34+
}
35+
36+
impl_try_from!(i8, u16, i16, u32, i32, u64, i64, u128, i128 => u8 ; approximate_float_unsigned);
37+
impl_try_from!(u8, u16, i16, u32, i32, u64, i64, u128, i128 => i8 ; approximate_float);
38+
39+
impl_try_from!(i16, u32, i32, u64, i64, u128, i128 => u16 ; approximate_float_unsigned);
40+
impl_try_from!(u16, u32, i32, u64, i64, u128, i128 => i16 ; approximate_float);
41+
42+
impl_try_from!(i32, u64, i64, u128, i128 => u32 ; approximate_float_unsigned);
43+
impl_try_from!(u32, u64, i64, u128, i128 => i32 ; approximate_float);
44+
45+
impl_try_from!(i64, u128, i128 => u64 ; approximate_float_unsigned);
46+
impl_try_from!(u64, u128, i128 => i64 ; approximate_float);
47+
48+
impl_try_from!(i128 => u128 ; approximate_float_unsigned);
49+
impl_try_from!(u128 => i128 ; approximate_float);
50+
51+
macro_rules! impl_from {
52+
( $($name:ty),* => $into:ty) => {
53+
$(
54+
impl From<$name> for Ratio<$into> {
55+
paste::paste! {
56+
fn from(n: $name) -> Self {
57+
<$into as FromPrimitive>::[< from_ $name>](n)
58+
.map(Ratio::from_integer)
59+
.unwrap()
60+
}
61+
}
62+
}
63+
)*
64+
};
65+
}
66+
67+
impl_from!(u8, u16, u32, u64 => u128);
68+
impl_from!(u8, i8, u16, i16, u32, i32, u64, i64 => i128);
69+
70+
impl_from!(u8, u16, u32 => u64);
71+
impl_from!(u8, i8, u16, i16, u32, i32 => i64);
72+
73+
impl_from!(u8, u16 => u32);
74+
impl_from!(u8, i8, u16, i16 => i32);
75+
76+
impl_from!(u8 => u16);
77+
impl_from!(u8, i8 => i16);

Diff for: src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use num_traits::{
4444
Pow, Signed, Zero,
4545
};
4646

47+
mod from;
4748
mod pow;
4849

4950
/// Represents the ratio between two numbers.

0 commit comments

Comments
 (0)