Skip to content

Commit 7ea092a

Browse files
committed
wip add try from methods
1 parent 2ad63fb commit 7ea092a

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

src/lib.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,104 @@ from_primitive_integer!(u64, approximate_float_unsigned);
12441244
from_primitive_integer!(u128, approximate_float_unsigned);
12451245
from_primitive_integer!(usize, approximate_float_unsigned);
12461246

1247+
macro_rules! try_from_impl {
1248+
($typ:ty, $approx:ident) => {
1249+
// impl TryFrom<i32> for Ratio<$typ> {
1250+
// type Error = ();
1251+
// fn try_from(n: i32) -> Result<Self, ()> {
1252+
// <$typ as FromPrimitive>::from_i32(n)
1253+
// .map(Ratio::from_integer)
1254+
// .ok_or(())
1255+
// }
1256+
// }
1257+
1258+
impl TryFrom<i64> for Ratio<$typ> {
1259+
type Error = ();
1260+
fn try_from(n: i64) -> Result<Self, ()> {
1261+
<$typ as FromPrimitive>::from_i64(n)
1262+
.map(Ratio::from_integer)
1263+
.ok_or(())
1264+
}
1265+
}
1266+
1267+
impl TryFrom<i128> for Ratio<$typ> {
1268+
type Error = ();
1269+
fn try_from(n: i128) -> Result<Self, ()> {
1270+
<$typ as FromPrimitive>::from_i128(n)
1271+
.map(Ratio::from_integer)
1272+
.ok_or(())
1273+
}
1274+
}
1275+
1276+
// impl TryFrom<u32> for Ratio<$typ> {
1277+
// type Error = ();
1278+
// fn try_from(n: u32) -> Result<Self, ()> {
1279+
// <$typ as FromPrimitive>::from_u32(n)
1280+
// .map(Ratio::from_integer)
1281+
// .ok_or(())
1282+
// }
1283+
// }
1284+
1285+
impl TryFrom<u64> for Ratio<$typ> {
1286+
type Error = ();
1287+
fn try_from(n: u64) -> Result<Self, ()> {
1288+
<$typ as FromPrimitive>::from_u64(n)
1289+
.map(Ratio::from_integer)
1290+
.ok_or(())
1291+
}
1292+
}
1293+
1294+
impl TryFrom<u128> for Ratio<$typ> {
1295+
type Error = ();
1296+
fn try_from(n: u128) -> Result<Self, ()> {
1297+
<$typ as FromPrimitive>::from_u128(n)
1298+
.map(Ratio::from_integer)
1299+
.ok_or(())
1300+
}
1301+
}
1302+
1303+
impl TryFrom<f32> for Ratio<$typ> {
1304+
type Error = ();
1305+
fn try_from(n: f32) -> Result<Self, ()> {
1306+
$approx(n, 10e-20, 30).ok_or(())
1307+
}
1308+
}
1309+
1310+
impl TryFrom<f64> for Ratio<$typ> {
1311+
type Error = ();
1312+
fn try_from(n: f64) -> Result<Self, ()> {
1313+
$approx(n, 10e-20, 30).ok_or(())
1314+
}
1315+
}
1316+
};
1317+
}
1318+
use std::convert::TryFrom;
1319+
1320+
// TODO
1321+
// need to exclude generation of the version for the same value
1322+
try_from_impl!(i8, approximate_float);
1323+
try_from_impl!(i16, approximate_float);
1324+
try_from_impl!(i32, approximate_float);
1325+
try_from_impl!(i64, approximate_float);
1326+
// try_from_impl!(i128, approximate_float);
1327+
try_from_impl!(isize, approximate_float);
1328+
1329+
try_from_impl!(u8, approximate_float_unsigned);
1330+
try_from_impl!(u16, approximate_float_unsigned);
1331+
try_from_impl!(u32, approximate_float_unsigned);
1332+
// try_from_impl!(u64, approximate_float_unsigned);
1333+
// try_from_impl!(u128, approximate_float_unsigned);
1334+
try_from_impl!(usize, approximate_float_unsigned);
1335+
1336+
#[test]
1337+
fn try_from_impl() {
1338+
debug_assert_eq!(Err(()), Ratio::<i8>::try_from(1000000i64));
1339+
debug_assert_eq!(
1340+
Ok(Ratio::<i8>::from_integer(11)),
1341+
Ratio::<i8>::try_from(11i64)
1342+
);
1343+
}
1344+
12471345
impl<T: Integer + Signed + Bounded + NumCast + Clone> Ratio<T> {
12481346
pub fn approximate_float<F: FloatCore + NumCast>(f: F) -> Option<Ratio<T>> {
12491347
// 1/10e-20 < 1/2**32 which seems like a good default, and 30 seems

0 commit comments

Comments
 (0)