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