From 32dee9a0c8259ee3846a644b3925bf981e261ada Mon Sep 17 00:00:00 2001 From: Michael Lamparski Date: Sun, 24 Jul 2016 14:58:43 -0400 Subject: [PATCH] rational: small additional tweaks Move `new` to top to make it more visible in the docs. Replace instances of $x == Zero::zero()$ with $x.is_zero()$, partially addressing #11. --- rational/src/lib.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/rational/src/lib.rs b/rational/src/lib.rs index ec2ee15..7aa036f 100644 --- a/rational/src/lib.rs +++ b/rational/src/lib.rs @@ -56,6 +56,17 @@ pub type Rational64 = Ratio; pub type BigRational = Ratio; impl Ratio { + /// Creates a new `Ratio`. Fails if `denom` is zero. + #[inline] + pub fn new(numer: T, denom: T) -> Ratio { + if denom.is_zero() { + panic!("denominator == 0"); + } + let mut ret = Ratio::new_raw(numer, denom); + ret.reduce(); + ret + } + /// Creates a `Ratio` representing the integer `t`. #[inline] pub fn from_integer(t: T) -> Ratio { @@ -71,17 +82,6 @@ impl Ratio { } } - /// Creates a new `Ratio`. Fails if `denom == 0`. - #[inline] - pub fn new(numer: T, denom: T) -> Ratio { - if denom == Zero::zero() { - panic!("denominator == 0"); - } - let mut ret = Ratio::new_raw(numer, denom); - ret.reduce(); - ret - } - /// Converts to an integer, rounding towards zero. #[inline] pub fn to_integer(&self) -> T { @@ -605,7 +605,7 @@ impl serde::Deserialize for Ratio where D: serde::Deserializer { let (numer, denom) = try!(serde::Deserialize::deserialize(deserializer)); - if denom == Zero::zero() { + if denom.is_zero() { Err(serde::de::Error::invalid_value("denominator is zero")) } else { Ok(Ratio::new_raw(numer, denom))