Simplify Integer dependant traits

- Integer only needs to require Ord explicitly, and then PartialOrd, Eq,
  and PartialEq come transitively.
- Generics on Integer can implicitly use all of those comparison traits.

This should not be a breaking change, as it doesn't actually change any
effective trait requirements -- only what's explicit for simplicity.
This commit is contained in:
Josh Stone 2016-02-22 15:28:26 -08:00
parent 2f63d3bdc3
commit 5b2cb8df97
3 changed files with 18 additions and 20 deletions

View File

@ -13,9 +13,7 @@
use {Num, Signed}; use {Num, Signed};
pub trait Integer pub trait Integer
: Sized : Sized + Num + Ord
+ Num
+ PartialOrd + Ord + Eq
{ {
/// Floored integer division. /// Floored integer division.
/// ///

View File

@ -96,7 +96,7 @@ impl<A> Iterator for Range<A>
/// `Integer` is required to ensure the range will be the same regardless of /// `Integer` is required to ensure the range will be the same regardless of
/// the direction it is consumed. /// the direction it is consumed.
impl<A> DoubleEndedIterator for Range<A> impl<A> DoubleEndedIterator for Range<A>
where A: Integer + PartialOrd + Clone + ToPrimitive where A: Integer + Clone + ToPrimitive
{ {
#[inline] #[inline]
fn next_back(&mut self) -> Option<A> { fn next_back(&mut self) -> Option<A> {
@ -161,7 +161,7 @@ impl<A> Iterator for RangeInclusive<A>
} }
impl<A> DoubleEndedIterator for RangeInclusive<A> impl<A> DoubleEndedIterator for RangeInclusive<A>
where A: Sub<A, Output = A> + Integer + PartialOrd + Clone + ToPrimitive where A: Sub<A, Output = A> + Integer + Clone + ToPrimitive
{ {
#[inline] #[inline]
fn next_back(&mut self) -> Option<A> { fn next_back(&mut self) -> Option<A> {

View File

@ -41,7 +41,7 @@ pub type Rational64 = Ratio<i64>;
/// Alias for arbitrary precision rationals. /// Alias for arbitrary precision rationals.
pub type BigRational = Ratio<BigInt>; pub type BigRational = Ratio<BigInt>;
impl<T: Clone + Integer + PartialOrd> Ratio<T> { impl<T: Clone + Integer> Ratio<T> {
/// Creates a ratio representing the integer `t`. /// Creates a ratio representing the integer `t`.
#[inline] #[inline]
pub fn from_integer(t: T) -> Ratio<T> { pub fn from_integer(t: T) -> Ratio<T> {
@ -187,7 +187,7 @@ impl<T: Clone + Integer + PartialOrd> Ratio<T> {
} }
} }
impl<T: Clone + Integer + PartialOrd + PrimInt> Ratio<T> { impl<T: Clone + Integer + PrimInt> Ratio<T> {
/// Raises the ratio to the power of an exponent /// Raises the ratio to the power of an exponent
#[inline] #[inline]
pub fn pow(&self, expon: i32) -> Ratio<T> { pub fn pow(&self, expon: i32) -> Ratio<T> {
@ -254,7 +254,7 @@ cmp_impl!(impl Ord, cmp -> cmp::Ordering);
macro_rules! forward_val_val_binop { macro_rules! forward_val_val_binop {
(impl $imp:ident, $method:ident) => { (impl $imp:ident, $method:ident) => {
impl<T: Clone + Integer + PartialOrd> $imp<Ratio<T>> for Ratio<T> { impl<T: Clone + Integer> $imp<Ratio<T>> for Ratio<T> {
type Output = Ratio<T>; type Output = Ratio<T>;
#[inline] #[inline]
@ -268,7 +268,7 @@ macro_rules! forward_val_val_binop {
macro_rules! forward_ref_val_binop { macro_rules! forward_ref_val_binop {
(impl $imp:ident, $method:ident) => { (impl $imp:ident, $method:ident) => {
impl<'a, T> $imp<Ratio<T>> for &'a Ratio<T> where impl<'a, T> $imp<Ratio<T>> for &'a Ratio<T> where
T: Clone + Integer + PartialOrd T: Clone + Integer
{ {
type Output = Ratio<T>; type Output = Ratio<T>;
@ -283,7 +283,7 @@ macro_rules! forward_ref_val_binop {
macro_rules! forward_val_ref_binop { macro_rules! forward_val_ref_binop {
(impl $imp:ident, $method:ident) => { (impl $imp:ident, $method:ident) => {
impl<'a, T> $imp<&'a Ratio<T>> for Ratio<T> where impl<'a, T> $imp<&'a Ratio<T>> for Ratio<T> where
T: Clone + Integer + PartialOrd T: Clone + Integer
{ {
type Output = Ratio<T>; type Output = Ratio<T>;
@ -307,7 +307,7 @@ macro_rules! forward_all_binop {
forward_all_binop!(impl Mul, mul); forward_all_binop!(impl Mul, mul);
// a/b * c/d = (a*c)/(b*d) // a/b * c/d = (a*c)/(b*d)
impl<'a, 'b, T> Mul<&'b Ratio<T>> for &'a Ratio<T> impl<'a, 'b, T> Mul<&'b Ratio<T>> for &'a Ratio<T>
where T: Clone + Integer + PartialOrd where T: Clone + Integer
{ {
type Output = Ratio<T>; type Output = Ratio<T>;
@ -320,7 +320,7 @@ impl<'a, 'b, T> Mul<&'b Ratio<T>> for &'a Ratio<T>
forward_all_binop!(impl Div, div); forward_all_binop!(impl Div, div);
// (a/b) / (c/d) = (a*d)/(b*c) // (a/b) / (c/d) = (a*d)/(b*c)
impl<'a, 'b, T> Div<&'b Ratio<T>> for &'a Ratio<T> impl<'a, 'b, T> Div<&'b Ratio<T>> for &'a Ratio<T>
where T: Clone + Integer + PartialOrd where T: Clone + Integer
{ {
type Output = Ratio<T>; type Output = Ratio<T>;
@ -334,7 +334,7 @@ impl<'a, 'b, T> Div<&'b Ratio<T>> for &'a Ratio<T>
macro_rules! arith_impl { macro_rules! arith_impl {
(impl $imp:ident, $method:ident) => { (impl $imp:ident, $method:ident) => {
forward_all_binop!(impl $imp, $method); forward_all_binop!(impl $imp, $method);
impl<'a, 'b, T: Clone + Integer + PartialOrd> impl<'a, 'b, T: Clone + Integer>
$imp<&'b Ratio<T>> for &'a Ratio<T> { $imp<&'b Ratio<T>> for &'a Ratio<T> {
type Output = Ratio<T>; type Output = Ratio<T>;
#[inline] #[inline]
@ -356,7 +356,7 @@ arith_impl!(impl Sub, sub);
arith_impl!(impl Rem, rem); arith_impl!(impl Rem, rem);
impl<T> Neg for Ratio<T> impl<T> Neg for Ratio<T>
where T: Clone + Integer + PartialOrd + Neg<Output = T> where T: Clone + Integer + Neg<Output = T>
{ {
type Output = Ratio<T>; type Output = Ratio<T>;
@ -365,7 +365,7 @@ impl<T> Neg for Ratio<T>
} }
impl<'a, T> Neg for &'a Ratio<T> impl<'a, T> Neg for &'a Ratio<T>
where T: Clone + Integer + PartialOrd + Neg<Output = T> where T: Clone + Integer + Neg<Output = T>
{ {
type Output = Ratio<T>; type Output = Ratio<T>;
@ -376,7 +376,7 @@ impl<'a, T> Neg for &'a Ratio<T>
} }
/* Constants */ /* Constants */
impl<T: Clone + Integer + PartialOrd> impl<T: Clone + Integer>
Zero for Ratio<T> { Zero for Ratio<T> {
#[inline] #[inline]
fn zero() -> Ratio<T> { fn zero() -> Ratio<T> {
@ -389,7 +389,7 @@ impl<T: Clone + Integer + PartialOrd>
} }
} }
impl<T: Clone + Integer + PartialOrd> impl<T: Clone + Integer>
One for Ratio<T> { One for Ratio<T> {
#[inline] #[inline]
fn one() -> Ratio<T> { fn one() -> Ratio<T> {
@ -397,7 +397,7 @@ impl<T: Clone + Integer + PartialOrd>
} }
} }
impl<T: Clone + Integer + PartialOrd> Num for Ratio<T> { impl<T: Clone + Integer> Num for Ratio<T> {
type FromStrRadixErr = ParseRatioError; type FromStrRadixErr = ParseRatioError;
/// Parses `numer/denom` where the numbers are in base `radix`. /// Parses `numer/denom` where the numbers are in base `radix`.
@ -423,7 +423,7 @@ impl<T: Clone + Integer + PartialOrd> Num for Ratio<T> {
} }
} }
impl<T: Clone + Integer + PartialOrd + Signed> Signed for Ratio<T> { impl<T: Clone + Integer + Signed> Signed for Ratio<T> {
#[inline] #[inline]
fn abs(&self) -> Ratio<T> { fn abs(&self) -> Ratio<T> {
if self.is_negative() { -self.clone() } else { self.clone() } if self.is_negative() { -self.clone() } else { self.clone() }
@ -466,7 +466,7 @@ impl<T> fmt::Display for Ratio<T> where
} }
} }
impl<T: FromStr + Clone + Integer + PartialOrd> FromStr for Ratio<T> { impl<T: FromStr + Clone + Integer> FromStr for Ratio<T> {
type Err = ParseRatioError; type Err = ParseRatioError;
/// Parses `numer/denom` or just `numer`. /// Parses `numer/denom` or just `numer`.