From de91d5d9e3016895690934f434623df8f26058d9 Mon Sep 17 00:00:00 2001 From: Wictor Lund Date: Thu, 1 Dec 2016 21:37:07 +0200 Subject: [PATCH 1/2] Added trait From for Ratio where T: Clone + Integer --- rational/src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rational/src/lib.rs b/rational/src/lib.rs index 938de46..ae01b2c 100644 --- a/rational/src/lib.rs +++ b/rational/src/lib.rs @@ -261,6 +261,13 @@ impl Ratio { } } +// From integer +impl From for Ratio where T: Clone + Integer { + fn from(x: T) -> Ratio { + Ratio::from_integer(x) + } +} + // Comparisons // Mathematically, comparing a/b and c/d is the same as comparing a*d and b*c, but it's very easy From 4bdad38eee92e48a0beaf252a89cfc1e4128a4ed Mon Sep 17 00:00:00 2001 From: Wictor Lund Date: Thu, 1 Dec 2016 22:07:03 +0200 Subject: [PATCH 2/2] Added test for trait From for Ratio where T: Clone + Integer. --- rational/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/rational/src/lib.rs b/rational/src/lib.rs index ae01b2c..6349266 100644 --- a/rational/src/lib.rs +++ b/rational/src/lib.rs @@ -745,6 +745,7 @@ mod test { assert_eq!(_1_2, Ratio::new(1, 2)); assert_eq!(_3_2, Ratio::new(3, 2)); assert_eq!(_NEG1_2, Ratio::new(-1, 2)); + assert_eq!(_2, From::from(2)); } #[test]