From 76ecce0e0c1e307d0c387b3cc219d8410fe7a6f6 Mon Sep 17 00:00:00 2001 From: gifnksm Date: Tue, 14 May 2013 21:31:43 +0900 Subject: libstd: `Ratio` requires `Clone` instead of `Copy` This allows creating `Ratio` which `T` is non-implicitly copyable types such as `BigInt`. --- src/libstd/num/rational.rs | 51 +++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'src/libstd/num') diff --git a/src/libstd/num/rational.rs b/src/libstd/num/rational.rs index c9596291981..0ec6f7457d6 100644 --- a/src/libstd/num/rational.rs +++ b/src/libstd/num/rational.rs @@ -30,7 +30,7 @@ pub type Rational64 = Ratio; /// Alias for arbitrary precision rationals. pub type BigRational = Ratio; -impl +impl Ratio { /// Create a ratio representing the integer `t`. #[inline(always)] @@ -59,8 +59,12 @@ impl fn reduce(&mut self) { let g : T = self.numer.gcd(&self.denom); - self.numer /= g; - self.denom /= g; + // FIXME(#6050): overloaded operators force moves with generic types + // self.numer /= g; + self.numer = self.numer / g; + // FIXME(#6050): overloaded operators force moves with generic types + // self.denom /= g; + self.denom = self.denom / g; // keep denom positive! if self.denom < Zero::zero() { @@ -68,9 +72,10 @@ impl self.denom = -self.denom; } } + /// Return a `reduce`d copy of self. fn reduced(&self) -> Ratio { - let mut ret = copy *self; + let mut ret = self.clone(); ret.reduce(); ret } @@ -105,7 +110,7 @@ cmp_impl!(impl TotalOrd, cmp -> cmp::Ordering) /* Arithmetic */ // a/b * c/d = (a*c)/(b*d) -impl +impl Mul,Ratio> for Ratio { #[inline] fn mul(&self, rhs: &Ratio) -> Ratio { @@ -114,7 +119,7 @@ impl } // (a/b) / (c/d) = (a*d)/(b*c) -impl +impl Div,Ratio> for Ratio { #[inline] fn div(&self, rhs: &Ratio) -> Ratio { @@ -125,7 +130,7 @@ impl // Abstracts the a/b `op` c/d = (a*d `op` b*d) / (b*d) pattern macro_rules! arith_impl { (impl $imp:ident, $method:ident) => { - impl + impl $imp,Ratio> for Ratio { #[inline] fn $method(&self, rhs: &Ratio) -> Ratio { @@ -145,7 +150,7 @@ arith_impl!(impl Sub, sub) // a/b % c/d = (a*d % b*c)/(b*d) arith_impl!(impl Rem, rem) -impl +impl Neg> for Ratio { #[inline] fn neg(&self) -> Ratio { @@ -154,7 +159,7 @@ impl } /* Constants */ -impl +impl Zero for Ratio { #[inline] fn zero() -> Ratio { @@ -167,7 +172,7 @@ impl } } -impl +impl One for Ratio { #[inline] fn one() -> Ratio { @@ -175,11 +180,11 @@ impl } } -impl +impl Num for Ratio {} /* Utils */ -impl +impl Round for Ratio { fn floor(&self) -> Ratio { @@ -213,14 +218,14 @@ impl } fn fract(&self) -> Ratio { - Ratio::new_raw(self.numer % self.denom, self.denom) + Ratio::new_raw(self.numer % self.denom, self.denom.clone()) } } -impl Fractional for Ratio { +impl Fractional for Ratio { #[inline] fn recip(&self) -> Ratio { - Ratio::new_raw(self.denom, self.numer) + Ratio::new_raw(self.denom.clone(), self.numer.clone()) } } @@ -238,7 +243,7 @@ impl ToStrRadix for Ratio { } } -impl +impl FromStr for Ratio { /// Parses `numer/denom`. fn from_str(s: &str) -> Option> { @@ -248,14 +253,14 @@ impl } }); if split.len() < 2 { return None; } - do FromStr::from_str(split[0]).chain |a| { - do FromStr::from_str(split[1]).chain |b| { - Some(Ratio::new(a,b)) + do FromStr::from_str::(split[0]).chain |a| { + do FromStr::from_str::(split[1]).chain |b| { + Some(Ratio::new(a.clone(), b.clone())) } } } } -impl +impl FromStrRadix for Ratio { /// Parses `numer/denom` where the numbers are in base `radix`. fn from_str_radix(s: &str, radix: uint) -> Option> { @@ -266,9 +271,9 @@ impl }); if split.len() < 2 { None } else { - do FromStrRadix::from_str_radix(split[0], radix).chain |a| { - do FromStrRadix::from_str_radix(split[1], radix).chain |b| { - Some(Ratio::new(a,b)) + do FromStrRadix::from_str_radix::(split[0], radix).chain |a| { + do FromStrRadix::from_str_radix::(split[1], radix).chain |b| { + Some(Ratio::new(a.clone(), b.clone())) } } } -- cgit 1.4.1-3-g733a5