diff --git a/src/bigint.rs b/src/bigint.rs index d988959..5af89d4 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -189,7 +189,9 @@ impl Num for BigUint {} macro_rules! forward_val_val_binop { (impl $imp:ident for $res:ty, $method:ident) => { - impl $imp<$res, $res> for $res { + impl $imp<$res> for $res { + type Output = $res; + #[inline] fn $method(self, other: $res) -> $res { (&self).$method(&other) @@ -200,7 +202,9 @@ macro_rules! forward_val_val_binop { macro_rules! forward_ref_val_binop { (impl $imp:ident for $res:ty, $method:ident) => { - impl<'a> $imp<$res, $res> for &'a $res { + impl<'a> $imp<$res> for &'a $res { + type Output = $res; + #[inline] fn $method(self, other: $res) -> $res { self.$method(&other) @@ -211,7 +215,9 @@ macro_rules! forward_ref_val_binop { macro_rules! forward_val_ref_binop { (impl $imp:ident for $res:ty, $method:ident) => { - impl<'a> $imp<&'a $res, $res> for $res { + impl<'a> $imp<&'a $res> for $res { + type Output = $res; + #[inline] fn $method(self, other: &$res) -> $res { (&self).$method(other) @@ -230,7 +236,9 @@ macro_rules! forward_all_binop { forward_all_binop!(impl BitAnd for BigUint, bitand); -impl<'a, 'b> BitAnd<&'b BigUint, BigUint> for &'a BigUint { +impl<'a, 'b> BitAnd<&'b BigUint> for &'a BigUint { + type Output = BigUint; + #[inline] fn bitand(self, other: &BigUint) -> BigUint { BigUint::new(self.data.iter().zip(other.data.iter()).map(|(ai, bi)| *ai & *bi).collect()) @@ -239,7 +247,9 @@ impl<'a, 'b> BitAnd<&'b BigUint, BigUint> for &'a BigUint { forward_all_binop!(impl BitOr for BigUint, bitor); -impl<'a, 'b> BitOr<&'b BigUint, BigUint> for &'a BigUint { +impl<'a, 'b> BitOr<&'b BigUint> for &'a BigUint { + type Output = BigUint; + fn bitor(self, other: &BigUint) -> BigUint { let zeros = ZERO_VEC.iter().cycle(); let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) }; @@ -252,7 +262,9 @@ impl<'a, 'b> BitOr<&'b BigUint, BigUint> for &'a BigUint { forward_all_binop!(impl BitXor for BigUint, bitxor); -impl<'a, 'b> BitXor<&'b BigUint, BigUint> for &'a BigUint { +impl<'a, 'b> BitXor<&'b BigUint> for &'a BigUint { + type Output = BigUint; + fn bitxor(self, other: &BigUint) -> BigUint { let zeros = ZERO_VEC.iter().cycle(); let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) }; @@ -263,12 +275,16 @@ impl<'a, 'b> BitXor<&'b BigUint, BigUint> for &'a BigUint { } } -impl Shl for BigUint { +impl Shl for BigUint { + type Output = BigUint; + #[inline] fn shl(self, rhs: uint) -> BigUint { (&self) << rhs } } -impl<'a> Shl for &'a BigUint { +impl<'a> Shl for &'a BigUint { + type Output = BigUint; + #[inline] fn shl(self, rhs: uint) -> BigUint { let n_unit = rhs / BigDigit::BITS; @@ -277,12 +293,16 @@ impl<'a> Shl for &'a BigUint { } } -impl Shr for BigUint { +impl Shr for BigUint { + type Output = BigUint; + #[inline] fn shr(self, rhs: uint) -> BigUint { (&self) >> rhs } } -impl<'a> Shr for &'a BigUint { +impl<'a> Shr for &'a BigUint { + type Output = BigUint; + #[inline] fn shr(self, rhs: uint) -> BigUint { let n_unit = rhs / BigDigit::BITS; @@ -308,7 +328,9 @@ impl Unsigned for BigUint {} forward_all_binop!(impl Add for BigUint, add); -impl<'a, 'b> Add<&'b BigUint, BigUint> for &'a BigUint { +impl<'a, 'b> Add<&'b BigUint> for &'a BigUint { + type Output = BigUint; + fn add(self, other: &BigUint) -> BigUint { let zeros = ZERO_VEC.iter().cycle(); let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) }; @@ -327,7 +349,9 @@ impl<'a, 'b> Add<&'b BigUint, BigUint> for &'a BigUint { forward_all_binop!(impl Sub for BigUint, sub); -impl<'a, 'b> Sub<&'b BigUint, BigUint> for &'a BigUint { +impl<'a, 'b> Sub<&'b BigUint> for &'a BigUint { + type Output = BigUint; + fn sub(self, other: &BigUint) -> BigUint { let new_len = cmp::max(self.data.len(), other.data.len()); let zeros = ZERO_VEC.iter().cycle(); @@ -358,7 +382,9 @@ impl<'a, 'b> Sub<&'b BigUint, BigUint> for &'a BigUint { forward_all_binop!(impl Mul for BigUint, mul); -impl<'a, 'b> Mul<&'b BigUint, BigUint> for &'a BigUint { +impl<'a, 'b> Mul<&'b BigUint> for &'a BigUint { + type Output = BigUint; + fn mul(self, other: &BigUint) -> BigUint { if self.is_zero() || other.is_zero() { return Zero::zero(); } @@ -427,7 +453,9 @@ impl<'a, 'b> Mul<&'b BigUint, BigUint> for &'a BigUint { forward_all_binop!(impl Div for BigUint, div); -impl<'a, 'b> Div<&'b BigUint, BigUint> for &'a BigUint { +impl<'a, 'b> Div<&'b BigUint> for &'a BigUint { + type Output = BigUint; + #[inline] fn div(self, other: &BigUint) -> BigUint { let (q, _) = self.div_rem(other); @@ -437,7 +465,9 @@ impl<'a, 'b> Div<&'b BigUint, BigUint> for &'a BigUint { forward_all_binop!(impl Rem for BigUint, rem); -impl<'a, 'b> Rem<&'b BigUint, BigUint> for &'a BigUint { +impl<'a, 'b> Rem<&'b BigUint> for &'a BigUint { + type Output = BigUint; + #[inline] fn rem(self, other: &BigUint) -> BigUint { let (_, r) = self.div_rem(other); @@ -445,12 +475,16 @@ impl<'a, 'b> Rem<&'b BigUint, BigUint> for &'a BigUint { } } -impl Neg for BigUint { +impl Neg for BigUint { + type Output = BigUint; + #[inline] fn neg(self) -> BigUint { panic!() } } -impl<'a> Neg for &'a BigUint { +impl<'a> Neg for &'a BigUint { + type Output = BigUint; + #[inline] fn neg(self) -> BigUint { panic!() } } @@ -818,14 +852,14 @@ impl FromStrRadix for BigUint { } } -impl> AdditiveIterator for T { +impl> AdditiveIterator for T { fn sum(self) -> BigUint { let init: BigUint = Zero::zero(); self.fold(init, |acc, x| acc + x) } } -impl> MultiplicativeIterator for T { +impl> MultiplicativeIterator for T { fn product(self) -> BigUint { let init: BigUint = One::one(); self.fold(init, |acc, x| acc * x) @@ -939,7 +973,9 @@ fn get_radix_base(radix: uint) -> (DoubleBigDigit, uint) { #[derive(PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Show, RustcEncodable, RustcDecodable)] pub enum Sign { Minus, NoSign, Plus } -impl Neg for Sign { +impl Neg for Sign { + type Output = Sign; + /// Negate Sign value. #[inline] fn neg(self) -> Sign { @@ -1015,24 +1051,32 @@ impl FromStr for BigInt { impl Num for BigInt {} -impl Shl for BigInt { +impl Shl for BigInt { + type Output = BigInt; + #[inline] fn shl(self, rhs: uint) -> BigInt { (&self) << rhs } } -impl<'a> Shl for &'a BigInt { +impl<'a> Shl for &'a BigInt { + type Output = BigInt; + #[inline] fn shl(self, rhs: uint) -> BigInt { BigInt::from_biguint(self.sign, &self.data << rhs) } } -impl Shr for BigInt { +impl Shr for BigInt { + type Output = BigInt; + #[inline] fn shr(self, rhs: uint) -> BigInt { (&self) >> rhs } } -impl<'a> Shr for &'a BigInt { +impl<'a> Shr for &'a BigInt { + type Output = BigInt; + #[inline] fn shr(self, rhs: uint) -> BigInt { BigInt::from_biguint(self.sign, &self.data >> rhs) @@ -1088,7 +1132,9 @@ impl Signed for BigInt { forward_all_binop!(impl Add for BigInt, add); -impl<'a, 'b> Add<&'b BigInt, BigInt> for &'a BigInt { +impl<'a, 'b> Add<&'b BigInt> for &'a BigInt { + type Output = BigInt; + #[inline] fn add(self, other: &BigInt) -> BigInt { match (self.sign, other.sign) { @@ -1104,7 +1150,9 @@ impl<'a, 'b> Add<&'b BigInt, BigInt> for &'a BigInt { forward_all_binop!(impl Sub for BigInt, sub); -impl<'a, 'b> Sub<&'b BigInt, BigInt> for &'a BigInt { +impl<'a, 'b> Sub<&'b BigInt> for &'a BigInt { + type Output = BigInt; + #[inline] fn sub(self, other: &BigInt) -> BigInt { match (self.sign, other.sign) { @@ -1124,7 +1172,9 @@ impl<'a, 'b> Sub<&'b BigInt, BigInt> for &'a BigInt { forward_all_binop!(impl Mul for BigInt, mul); -impl<'a, 'b> Mul<&'b BigInt, BigInt> for &'a BigInt { +impl<'a, 'b> Mul<&'b BigInt> for &'a BigInt { + type Output = BigInt; + #[inline] fn mul(self, other: &BigInt) -> BigInt { match (self.sign, other.sign) { @@ -1141,7 +1191,9 @@ impl<'a, 'b> Mul<&'b BigInt, BigInt> for &'a BigInt { forward_all_binop!(impl Div for BigInt, div); -impl<'a, 'b> Div<&'b BigInt, BigInt> for &'a BigInt { +impl<'a, 'b> Div<&'b BigInt> for &'a BigInt { + type Output = BigInt; + #[inline] fn div(self, other: &BigInt) -> BigInt { let (q, _) = self.div_rem(other); @@ -1151,7 +1203,9 @@ impl<'a, 'b> Div<&'b BigInt, BigInt> for &'a BigInt { forward_all_binop!(impl Rem for BigInt, rem); -impl<'a, 'b> Rem<&'b BigInt, BigInt> for &'a BigInt { +impl<'a, 'b> Rem<&'b BigInt> for &'a BigInt { + type Output = BigInt; + #[inline] fn rem(self, other: &BigInt) -> BigInt { let (_, r) = self.div_rem(other); @@ -1159,12 +1213,16 @@ impl<'a, 'b> Rem<&'b BigInt, BigInt> for &'a BigInt { } } -impl Neg for BigInt { +impl Neg for BigInt { + type Output = BigInt; + #[inline] fn neg(self) -> BigInt { -&self } } -impl<'a> Neg for &'a BigInt { +impl<'a> Neg for &'a BigInt { + type Output = BigInt; + #[inline] fn neg(self) -> BigInt { BigInt::from_biguint(self.sign.neg(), self.data.clone()) @@ -1496,14 +1554,14 @@ impl RandBigInt for R { } } -impl> AdditiveIterator for T { +impl> AdditiveIterator for T { fn sum(self) -> BigInt { let init: BigInt = Zero::zero(); self.fold(init, |acc, x| acc + x) } } -impl> MultiplicativeIterator for T { +impl> MultiplicativeIterator for T { fn product(self) -> BigInt { let init: BigInt = One::one(); self.fold(init, |acc, x| acc * x) diff --git a/src/complex.rs b/src/complex.rs index e3872d7..b079a20 100644 --- a/src/complex.rs +++ b/src/complex.rs @@ -105,7 +105,9 @@ impl Complex { macro_rules! forward_val_val_binop { (impl $imp:ident, $method:ident) => { - impl $imp, Complex> for Complex { + impl $imp> for Complex { + type Output = Complex; + #[inline] fn $method(self, other: Complex) -> Complex { (&self).$method(&other) @@ -116,7 +118,9 @@ macro_rules! forward_val_val_binop { macro_rules! forward_ref_val_binop { (impl $imp:ident, $method:ident) => { - impl<'a, T: Clone + Num> $imp, Complex> for &'a Complex { + impl<'a, T: Clone + Num> $imp> for &'a Complex { + type Output = Complex; + #[inline] fn $method(self, other: Complex) -> Complex { self.$method(&other) @@ -127,7 +131,9 @@ macro_rules! forward_ref_val_binop { macro_rules! forward_val_ref_binop { (impl $imp:ident, $method:ident) => { - impl<'a, T: Clone + Num> $imp<&'a Complex, Complex> for Complex { + impl<'a, T: Clone + Num> $imp<&'a Complex> for Complex { + type Output = Complex; + #[inline] fn $method(self, other: &Complex) -> Complex { (&self).$method(other) @@ -148,7 +154,9 @@ macro_rules! forward_all_binop { forward_all_binop!(impl Add, add); // (a + i b) + (c + i d) == (a + c) + i (b + d) -impl<'a, 'b, T: Clone + Num> Add<&'b Complex, Complex> for &'a Complex { +impl<'a, 'b, T: Clone + Num> Add<&'b Complex> for &'a Complex { + type Output = Complex; + #[inline] fn add(self, other: &Complex) -> Complex { Complex::new(self.re.clone() + other.re.clone(), @@ -159,7 +167,9 @@ impl<'a, 'b, T: Clone + Num> Add<&'b Complex, Complex> for &'a Complex forward_all_binop!(impl Sub, sub); // (a + i b) - (c + i d) == (a - c) + i (b - d) -impl<'a, 'b, T: Clone + Num> Sub<&'b Complex, Complex> for &'a Complex { +impl<'a, 'b, T: Clone + Num> Sub<&'b Complex> for &'a Complex { + type Output = Complex; + #[inline] fn sub(self, other: &Complex) -> Complex { Complex::new(self.re.clone() - other.re.clone(), @@ -170,7 +180,9 @@ impl<'a, 'b, T: Clone + Num> Sub<&'b Complex, Complex> for &'a Complex forward_all_binop!(impl Mul, mul); // (a + i b) * (c + i d) == (a*c - b*d) + i (a*d + b*c) -impl<'a, 'b, T: Clone + Num> Mul<&'b Complex, Complex> for &'a Complex { +impl<'a, 'b, T: Clone + Num> Mul<&'b Complex> for &'a Complex { + type Output = Complex; + #[inline] fn mul(self, other: &Complex) -> Complex { Complex::new(self.re.clone() * other.re.clone() - self.im.clone() * other.im.clone(), @@ -182,7 +194,9 @@ forward_all_binop!(impl Div, div); // (a + i b) / (c + i d) == [(a + i b) * (c - i d)] / (c*c + d*d) // == [(a*c + b*d) / (c*c + d*d)] + i [(b*c - a*d) / (c*c + d*d)] -impl<'a, 'b, T: Clone + Num> Div<&'b Complex, Complex> for &'a Complex { +impl<'a, 'b, T: Clone + Num> Div<&'b Complex> for &'a Complex { + type Output = Complex; + #[inline] fn div(self, other: &Complex) -> Complex { let norm_sqr = other.norm_sqr(); @@ -191,12 +205,16 @@ impl<'a, 'b, T: Clone + Num> Div<&'b Complex, Complex> for &'a Complex } } -impl Neg> for Complex { +impl Neg for Complex { + type Output = Complex; + #[inline] fn neg(self) -> Complex { -&self } } -impl<'a, T: Clone + Num> Neg> for &'a Complex { +impl<'a, T: Clone + Num> Neg for &'a Complex { + type Output = Complex; + #[inline] fn neg(self) -> Complex { Complex::new(-self.re.clone(), -self.im.clone()) @@ -234,14 +252,18 @@ impl fmt::Show for Complex { } } -impl>> AdditiveIterator> for T { +impl AdditiveIterator> for T + where A: Clone + Num, T: Iterator> +{ fn sum(self) -> Complex { let init: Complex = Zero::zero(); self.fold(init, |acc, x| acc + x) } } -impl>> MultiplicativeIterator> for T { +impl MultiplicativeIterator> for T + where A: Clone + Num, T: Iterator> +{ fn product(self) -> Complex { let init: Complex = One::one(); self.fold(init, |acc, x| acc * x) diff --git a/src/integer.rs b/src/integer.rs index 006ea3c..a44b552 100644 --- a/src/integer.rs +++ b/src/integer.rs @@ -15,8 +15,7 @@ use std::ops::{Div, Rem}; use {Num, Signed}; pub trait Integer: Sized + Num + PartialOrd - + Div - + Rem { + + Div + Rem { /// Floored integer division. /// /// # Examples diff --git a/src/iter.rs b/src/iter.rs index fd75721..ecf0fd2 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -36,12 +36,18 @@ pub struct Range { /// } /// ``` #[inline] -pub fn range + PartialOrd + Clone + One>(start: A, stop: A) -> Range { +pub fn range(start: A, stop: A) -> Range + where A: Add + PartialOrd + Clone + One +{ Range{state: start, stop: stop, one: One::one()} } // FIXME: rust-lang/rust#10414: Unfortunate type bound -impl + PartialOrd + Clone + ToPrimitive> Iterator for Range { +impl Iterator for Range + where A: Add + PartialOrd + Clone + ToPrimitive +{ + type Item = A; + #[inline] fn next(&mut self) -> Option { if self.state < self.stop { @@ -88,7 +94,9 @@ impl + PartialOrd + Clone + ToPrimitive> Iterator for Range { /// `Integer` is required to ensure the range will be the same regardless of /// the direction it is consumed. -impl DoubleEndedIterator for Range { +impl DoubleEndedIterator for Range + where A: Integer + PartialOrd + Clone + ToPrimitive +{ #[inline] fn next_back(&mut self) -> Option { if self.stop > self.state { @@ -109,12 +117,17 @@ pub struct RangeInclusive { /// Return an iterator over the range [start, stop] #[inline] -pub fn range_inclusive + PartialOrd + Clone + One>(start: A, stop: A) - -> RangeInclusive { +pub fn range_inclusive(start: A, stop: A) -> RangeInclusive + where A: Add + PartialOrd + Clone + One +{ RangeInclusive{range: range(start, stop), done: false} } -impl + PartialOrd + Clone + ToPrimitive> Iterator for RangeInclusive { +impl Iterator for RangeInclusive + where A: Add + PartialOrd + Clone + ToPrimitive +{ + type Item = A; + #[inline] fn next(&mut self) -> Option { match self.range.next() { @@ -146,8 +159,9 @@ impl + PartialOrd + Clone + ToPrimitive> Iterator for RangeInclu } } -impl + Integer + PartialOrd + Clone + ToPrimitive> DoubleEndedIterator - for RangeInclusive { +impl DoubleEndedIterator for RangeInclusive + where A: Sub + Integer + PartialOrd + Clone + ToPrimitive +{ #[inline] fn next_back(&mut self) -> Option { if self.range.stop > self.range.state { @@ -174,13 +188,18 @@ pub struct RangeStep { /// Return an iterator over the range [start, stop) by `step`. It handles overflow by stopping. #[inline] -pub fn range_step(start: A, stop: A, step: A) -> RangeStep { +pub fn range_step(start: A, stop: A, step: A) -> RangeStep + where A: CheckedAdd + PartialOrd + Clone + Zero +{ let rev = step < Zero::zero(); RangeStep{state: start, stop: stop, step: step, rev: rev} } -impl Iterator for RangeStep { +impl Iterator for RangeStep + where A: CheckedAdd + PartialOrd + Clone +{ + type Item = A; + #[inline] fn next(&mut self) -> Option { if (self.rev && self.state > self.stop) || (!self.rev && self.state < self.stop) { @@ -208,13 +227,18 @@ pub struct RangeStepInclusive { /// Return an iterator over the range [start, stop] by `step`. It handles overflow by stopping. #[inline] -pub fn range_step_inclusive(start: A, stop: A, - step: A) -> RangeStepInclusive { +pub fn range_step_inclusive(start: A, stop: A, step: A) -> RangeStepInclusive + where A: CheckedAdd + PartialOrd + Clone + Zero +{ let rev = step < Zero::zero(); RangeStepInclusive{state: start, stop: stop, step: step, rev: rev, done: false} } -impl Iterator for RangeStepInclusive { +impl Iterator for RangeStepInclusive + where A: CheckedAdd + PartialOrd + Clone + PartialEq +{ + type Item = A; + #[inline] fn next(&mut self) -> Option { if !self.done && ((self.rev && self.state >= self.stop) || @@ -249,7 +273,9 @@ mod tests { fn to_u64(&self) -> Option { None } } - impl Add for Foo { + impl Add for Foo { + type Output = Foo; + fn add(self, _: Foo) -> Foo { Foo } @@ -273,7 +299,9 @@ mod tests { } } - impl Mul for Foo { + impl Mul for Foo { + type Output = Foo; + fn mul(self, _: Foo) -> Foo { Foo } diff --git a/src/lib.rs b/src/lib.rs index 58ef857..fd5106f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,6 +45,7 @@ //! //! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method +#![feature(associated_types)] #![feature(macro_rules)] #![feature(default_type_params)] #![feature(slicing_syntax)] @@ -123,7 +124,7 @@ pub fn abs_sub(x: T, y: T) -> T { /// assert_eq!(num::pow(2i, 4), 16); /// ``` #[inline] -pub fn pow>(mut base: T, mut exp: uint) -> T { +pub fn pow>(mut base: T, mut exp: uint) -> T { if exp == 1 { base } else { let mut acc = one::(); diff --git a/src/rational.rs b/src/rational.rs index 7bb587b..8210d97 100644 --- a/src/rational.rs +++ b/src/rational.rs @@ -216,7 +216,7 @@ macro_rules! cmp_impl { }; // return something other than a Ratio (impl $imp:ident, $($method:ident -> $res:ty),*) => { - impl + $imp> $imp for Ratio { + impl + $imp> $imp for Ratio { $( #[inline] fn $method(&self, other: &Ratio) -> $res { @@ -234,7 +234,9 @@ cmp_impl!(impl Ord, cmp -> cmp::Ordering); macro_rules! forward_val_val_binop { (impl $imp:ident, $method:ident) => { - impl $imp, Ratio> for Ratio { + impl $imp> for Ratio { + type Output = Ratio; + #[inline] fn $method(self, other: Ratio) -> Ratio { (&self).$method(&other) @@ -245,7 +247,9 @@ macro_rules! forward_val_val_binop { macro_rules! forward_ref_val_binop { (impl $imp:ident, $method:ident) => { - impl<'a, T: Clone + Integer + PartialOrd> $imp, Ratio> for &'a Ratio { + impl<'a, T: Clone + Integer + PartialOrd> $imp> for &'a Ratio { + type Output = Ratio; + #[inline] fn $method(self, other: Ratio) -> Ratio { self.$method(&other) @@ -256,7 +260,9 @@ macro_rules! forward_ref_val_binop { macro_rules! forward_val_ref_binop { (impl $imp:ident, $method:ident) => { - impl<'a, T: Clone + Integer + PartialOrd> $imp<&'a Ratio, Ratio> for Ratio { + impl<'a, T: Clone + Integer + PartialOrd> $imp<&'a Ratio> for Ratio { + type Output = Ratio; + #[inline] fn $method(self, other: &Ratio) -> Ratio { (&self).$method(other) @@ -276,8 +282,11 @@ macro_rules! forward_all_binop { /* Arithmetic */ forward_all_binop!(impl Mul, mul); // a/b * c/d = (a*c)/(b*d) -impl<'a, 'b, T: Clone + Integer + PartialOrd> - Mul<&'b Ratio, Ratio> for &'a Ratio { +impl<'a, 'b, T> Mul<&'b Ratio> for &'a Ratio + where T: Clone + Integer + PartialOrd +{ + + type Output = Ratio; #[inline] fn mul(self, rhs: &Ratio) -> Ratio { Ratio::new(self.numer.clone() * rhs.numer.clone(), self.denom.clone() * rhs.denom.clone()) @@ -286,8 +295,11 @@ impl<'a, 'b, T: Clone + Integer + PartialOrd> forward_all_binop!(impl Div, div); // (a/b) / (c/d) = (a*d)/(b*c) -impl<'a, 'b, T: Clone + Integer + PartialOrd> - Div<&'b Ratio, Ratio> for &'a Ratio { +impl<'a, 'b, T> Div<&'b Ratio> for &'a Ratio + where T: Clone + Integer + PartialOrd +{ + type Output = Ratio; + #[inline] fn div(self, rhs: &Ratio) -> Ratio { Ratio::new(self.numer.clone() * rhs.denom.clone(), self.denom.clone() * rhs.numer.clone()) @@ -299,7 +311,8 @@ macro_rules! arith_impl { (impl $imp:ident, $method:ident) => { forward_all_binop!(impl $imp, $method); impl<'a, 'b, T: Clone + Integer + PartialOrd> - $imp<&'b Ratio,Ratio> for &'a Ratio { + $imp<&'b Ratio> for &'a Ratio { + type Output = Ratio; #[inline] fn $method(self, rhs: &Ratio) -> Ratio { Ratio::new((self.numer.clone() * rhs.denom.clone()).$method(self.denom.clone() * rhs.numer.clone()), @@ -318,14 +331,20 @@ arith_impl!(impl Sub, sub); // a/b % c/d = (a*d % b*c)/(b*d) arith_impl!(impl Rem, rem); -impl - Neg> for Ratio { +impl Neg for Ratio + where T: Clone + Integer + PartialOrd +{ + type Output = Ratio; + #[inline] fn neg(self) -> Ratio { -&self } } -impl<'a, T: Clone + Integer + PartialOrd> - Neg> for &'a Ratio { +impl<'a, T> Neg for &'a Ratio + where T: Clone + Integer + PartialOrd +{ + type Output = Ratio; + #[inline] fn neg(self) -> Ratio { Ratio::new_raw(-self.numer.clone(), self.denom.clone()) @@ -437,14 +456,18 @@ impl } } -impl>> AdditiveIterator> for T { +impl AdditiveIterator> for T + where A: Clone + Integer + PartialOrd, T: Iterator> +{ fn sum(self) -> Ratio { let init: Ratio = Zero::zero(); self.fold(init, |acc, x| acc + x) } } -impl>> MultiplicativeIterator> for T { +impl MultiplicativeIterator> for T + where A: Clone + Integer + PartialOrd, T: Iterator> +{ fn product(self) -> Ratio { let init: Ratio = One::one(); self.fold(init, |acc, x| acc * x) diff --git a/src/traits.rs b/src/traits.rs index a0b77fb..c76380a 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -18,12 +18,8 @@ use std::{f32, f64}; /// The base trait for numeric types pub trait Num: PartialEq + Zero + One - + Neg - + Add - + Sub - + Mul - + Div - + Rem {} + + Neg + Add + Sub + + Mul + Div + Rem {} macro_rules! trait_impl { ($name:ident for $($t:ty)*) => ($( @@ -40,7 +36,7 @@ trait_impl!(Num for uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64); /// This trait can be automatically be derived using `#[deriving(Zero)]` /// attribute. If you choose to use this, make sure that the laws outlined in /// the documentation for `Zero::zero` still hold. -pub trait Zero: Add { +pub trait Zero: Add { /// Returns the additive identity element of `Self`, `0`. /// /// # Laws @@ -90,7 +86,7 @@ zero_impl!(f32, 0.0f32); zero_impl!(f64, 0.0f64); /// Defines a multiplicative identity element for `Self`. -pub trait One: Mul { +pub trait One: Mul { /// Returns the multiplicative identity element of `Self`, `1`. /// /// # Laws @@ -134,7 +130,7 @@ one_impl!(f32, 1.0f32); one_impl!(f64, 1.0f64); /// Useful functions for signed numbers (i.e. numbers that can be negative). -pub trait Signed: Num + Neg { +pub trait Signed: Num + Neg { /// Computes the absolute value. /// /// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`. @@ -328,7 +324,7 @@ impl Saturating for T } /// Performs addition that returns `None` instead of wrapping around on overflow. -pub trait CheckedAdd: Add { +pub trait CheckedAdd: Add { /// Adds two numbers, checking for overflow. If overflow happens, `None` is returned. /// /// # Example @@ -389,7 +385,7 @@ checked_impl!(CheckedAdd, checked_add, i32, intrinsics::i32_add_with_overflow); checked_impl!(CheckedAdd, checked_add, i64, intrinsics::i64_add_with_overflow); /// Performs subtraction that returns `None` instead of wrapping around on underflow. -pub trait CheckedSub: Sub { +pub trait CheckedSub: Sub { /// Subtracts two numbers, checking for underflow. If underflow happens, `None` is returned. /// /// # Example @@ -424,7 +420,7 @@ checked_impl!(CheckedSub, checked_sub, i64, intrinsics::i64_sub_with_overflow); /// Performs multiplication that returns `None` instead of wrapping around on underflow or /// overflow. -pub trait CheckedMul: Mul { +pub trait CheckedMul: Mul { /// Multiplies two numbers, checking for underflow or overflow. If underflow or overflow /// happens, `None` is returned. /// @@ -460,7 +456,7 @@ checked_impl!(CheckedMul, checked_mul, i64, intrinsics::i64_mul_with_overflow); /// Performs division that returns `None` instead of panicking on division by zero and instead of /// wrapping around on underflow and overflow. -pub trait CheckedDiv: Div { +pub trait CheckedDiv: Div { /// Divides two numbers, checking for underflow, overflow and division by zero. If any of that /// happens, `None` is returned. ///