From 9d7ab663b113da85b97c1abdf87d8486449bc479 Mon Sep 17 00:00:00 2001 From: dnsl48 Date: Sat, 10 Dec 2016 09:19:02 +1300 Subject: [PATCH] From<(T,T)> implementation --- rational/src/lib.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rational/src/lib.rs b/rational/src/lib.rs index e63ddfb..6ba54db 100644 --- a/rational/src/lib.rs +++ b/rational/src/lib.rs @@ -268,6 +268,14 @@ impl From for Ratio where T: Clone + Integer { } } + +// From pair (through the `new` constructor) +impl From<(T, T)> for Ratio where T: Clone + Integer { + fn from(pair: (T, T)) -> Ratio { + Ratio::new(pair.0, pair.1) + } +} + // Comparisons // Mathematically, comparing a/b and c/d is the same as comparing a*d and b*c, but it's very easy @@ -1157,4 +1165,12 @@ mod test { assert_eq! ((-2, 1), _NEG2.into()); assert_eq! ((1, -2), _1_NEG2.into()); } + + #[test] + fn test_from_pair() { + assert_eq! (_0, Ratio::from ((0, 1))); + assert_eq! (_1, Ratio::from ((1, 1))); + assert_eq! (_NEG2, Ratio::from ((-2, 1))); + assert_eq! (_1_NEG2, Ratio::from ((1, -2))); + } }