diff options
| author | Jorge Aparicio <japaricious@gmail.com> | 2014-12-31 15:45:13 -0500 |
|---|---|---|
| committer | Jorge Aparicio <japaricious@gmail.com> | 2015-01-03 16:29:19 -0500 |
| commit | 99017f82b6e41ed283199b88ddfc0990bb95d696 (patch) | |
| tree | 66cd460eb70ab440425b66bb81651820c1d58469 /src/libcore | |
| parent | 7095dd00702373dd612d61e191eb57fadce00751 (diff) | |
| download | rust-99017f82b6e41ed283199b88ddfc0990bb95d696.tar.gz rust-99017f82b6e41ed283199b88ddfc0990bb95d696.zip | |
use assoc types in binop traits
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/iter.rs | 2 | ||||
| -rw-r--r-- | src/libcore/num/mod.rs | 46 | ||||
| -rw-r--r-- | src/libcore/ops.rs | 174 |
3 files changed, 155 insertions, 67 deletions
diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 963377ff6b3..f65857b37fb 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -2453,7 +2453,7 @@ pub fn count<A>(start: A, step: A) -> Counter<A> { } #[unstable = "trait is unstable"] -impl<A: Add<A, A> + Clone> Iterator for Counter<A> { +impl<A: Add<Output=A> + Clone> Iterator for Counter<A> { type Item = A; #[inline] diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index d16478dd6cc..c642ff0c2e4 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -35,7 +35,7 @@ use str::{FromStr, from_str, StrExt}; /// Simultaneous division and remainder #[inline] #[deprecated = "use division and remainder directly"] -pub fn div_rem<T: Clone + Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) { +pub fn div_rem<T: Clone + Div<Output=T> + Rem<Output=T>>(x: T, y: T) -> (T, T) { (x.clone() / y.clone(), x % y) } @@ -53,17 +53,17 @@ pub trait Int + NumCast + PartialOrd + Ord + PartialEq + Eq - + Add<Self,Self> - + Sub<Self,Self> - + Mul<Self,Self> - + Div<Self,Self> - + Rem<Self,Self> + + Add<Output=Self> + + Sub<Output=Self> + + Mul<Output=Self> + + Div<Output=Self> + + Rem<Output=Self> + Not<Self> - + BitAnd<Self,Self> - + BitOr<Self,Self> - + BitXor<Self,Self> - + Shl<uint,Self> - + Shr<uint,Self> + + BitAnd<Output=Self> + + BitOr<Output=Self> + + BitXor<Output=Self> + + Shl<uint, Output=Self> + + Shr<uint, Output=Self> { /// Returns the `0` value of this integer type. // FIXME (#5527): Should be an associated constant @@ -1246,11 +1246,11 @@ pub trait Float + PartialOrd + PartialEq + Neg<Self> - + Add<Self,Self> - + Sub<Self,Self> - + Mul<Self,Self> - + Div<Self,Self> - + Rem<Self,Self> + + Add<Output=Self> + + Sub<Output=Self> + + Mul<Output=Self> + + Div<Output=Self> + + Rem<Output=Self> { /// Returns the NaN value. fn nan() -> Self; @@ -1719,11 +1719,11 @@ macro_rules! trait_impl { #[allow(deprecated)] pub trait Num: PartialEq + Zero + One + Neg<Self> - + Add<Self,Self> - + Sub<Self,Self> - + Mul<Self,Self> - + Div<Self,Self> - + Rem<Self,Self> {} + + Add<Output=Self> + + Sub<Output=Self> + + Mul<Output=Self> + + Div<Output=Self> + + Rem<Output=Self> {} trait_impl! { Num for uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 } #[deprecated = "Generalised unsigned numbers are no longer supported"] @@ -1737,7 +1737,7 @@ pub trait Primitive: Copy + Clone + Num + NumCast + PartialOrd {} trait_impl! { Primitive for uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 } #[deprecated = "The generic `Zero` trait will be removed soon."] -pub trait Zero: Add<Self, Self> { +pub trait Zero: Add<Output=Self> { #[deprecated = "Use `Int::zero()` or `Float::zero()`."] fn zero() -> Self; #[deprecated = "Use `x == Int::zero()` or `x == Float::zero()`."] @@ -1768,7 +1768,7 @@ zero_impl! { f32, 0.0f32 } zero_impl! { f64, 0.0f64 } #[deprecated = "The generic `One` trait will be removed soon."] -pub trait One: Mul<Self, Self> { +pub trait One: Mul<Output=Self> { #[deprecated = "Use `Int::one()` or `Float::one()`."] fn one() -> Self; } diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index c5441359ad0..1bf22d65ffb 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -25,6 +25,8 @@ //! demonstrates adding and subtracting two `Point`s. //! //! ```rust +//! #![feature(associated_types)] +//! //! use std::ops::{Add, Sub}; //! //! #[deriving(Show)] @@ -33,13 +35,17 @@ //! y: int //! } //! -//! impl Add<Point, Point> for Point { +//! impl Add for Point { +//! type Output = Point; +//! //! fn add(self, other: Point) -> Point { //! Point {x: self.x + other.x, y: self.y + other.y} //! } //! } //! -//! impl Sub<Point, Point> for Point { +//! impl Sub for Point { +//! type Output = Point; +//! //! fn sub(self, other: Point) -> Point { //! Point {x: self.x - other.x, y: self.y - other.y} //! } @@ -93,12 +99,16 @@ pub trait Drop { /// calling `add`, and therefore, `main` prints `Adding!`. /// /// ```rust +/// #![feature(associated_types)] +/// /// use std::ops::Add; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl Add<Foo, Foo> for Foo { +/// impl Add for Foo { +/// type Output = Foo; +/// /// fn add(self, _rhs: Foo) -> Foo { /// println!("Adding!"); /// self @@ -110,14 +120,18 @@ pub trait Drop { /// } /// ``` #[lang="add"] -pub trait Add<RHS, Result> { +pub trait Add<RHS=Self> { + type Output; + /// The method for the `+` operator - fn add(self, rhs: RHS) -> Result; + fn add(self, rhs: RHS) -> Self::Output; } macro_rules! add_impl { ($($t:ty)*) => ($( - impl Add<$t, $t> for $t { + impl Add for $t { + type Output = $t; + #[inline] fn add(self, other: $t) -> $t { self + other } } @@ -134,12 +148,16 @@ add_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 } /// calling `sub`, and therefore, `main` prints `Subtracting!`. /// /// ```rust +/// #![feature(associated_types)] +/// /// use std::ops::Sub; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl Sub<Foo, Foo> for Foo { +/// impl Sub for Foo { +/// type Output = Foo; +/// /// fn sub(self, _rhs: Foo) -> Foo { /// println!("Subtracting!"); /// self @@ -151,14 +169,18 @@ add_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 } /// } /// ``` #[lang="sub"] -pub trait Sub<RHS, Result> { +pub trait Sub<RHS=Self> { + type Output; + /// The method for the `-` operator - fn sub(self, rhs: RHS) -> Result; + fn sub(self, rhs: RHS) -> Self::Output; } macro_rules! sub_impl { ($($t:ty)*) => ($( - impl Sub<$t, $t> for $t { + impl Sub for $t { + type Output = $t; + #[inline] fn sub(self, other: $t) -> $t { self - other } } @@ -175,12 +197,16 @@ sub_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 } /// calling `mul`, and therefore, `main` prints `Multiplying!`. /// /// ```rust +/// #![feature(associated_types)] +/// /// use std::ops::Mul; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl Mul<Foo, Foo> for Foo { +/// impl Mul for Foo { +/// type Output = Foo; +/// /// fn mul(self, _rhs: Foo) -> Foo { /// println!("Multiplying!"); /// self @@ -192,14 +218,18 @@ sub_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 } /// } /// ``` #[lang="mul"] -pub trait Mul<RHS, Result> { +pub trait Mul<RHS=Self> { + type Output; + /// The method for the `*` operator - fn mul(self, rhs: RHS) -> Result; + fn mul(self, rhs: RHS) -> Self::Output; } macro_rules! mul_impl { ($($t:ty)*) => ($( - impl Mul<$t, $t> for $t { + impl Mul for $t { + type Output = $t; + #[inline] fn mul(self, other: $t) -> $t { self * other } } @@ -216,12 +246,16 @@ mul_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 } /// calling `div`, and therefore, `main` prints `Dividing!`. /// /// ``` +/// #![feature(associated_types)] +/// /// use std::ops::Div; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl Div<Foo, Foo> for Foo { +/// impl Div for Foo { +/// type Output = Foo; +/// /// fn div(self, _rhs: Foo) -> Foo { /// println!("Dividing!"); /// self @@ -233,14 +267,18 @@ mul_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 } /// } /// ``` #[lang="div"] -pub trait Div<RHS, Result> { +pub trait Div<RHS=Self> { + type Output; + /// The method for the `/` operator - fn div(self, rhs: RHS) -> Result; + fn div(self, rhs: RHS) -> Self::Output; } macro_rules! div_impl { ($($t:ty)*) => ($( - impl Div<$t, $t> for $t { + impl Div for $t { + type Output = $t; + #[inline] fn div(self, other: $t) -> $t { self / other } } @@ -257,12 +295,16 @@ div_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 } /// calling `rem`, and therefore, `main` prints `Remainder-ing!`. /// /// ``` +/// #![feature(associated_types)] +/// /// use std::ops::Rem; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl Rem<Foo, Foo> for Foo { +/// impl Rem for Foo { +/// type Output = Foo; +/// /// fn rem(self, _rhs: Foo) -> Foo { /// println!("Remainder-ing!"); /// self @@ -274,14 +316,18 @@ div_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64 } /// } /// ``` #[lang="rem"] -pub trait Rem<RHS, Result> { +pub trait Rem<RHS=Self> { + type Output = Self; + /// The method for the `%` operator - fn rem(self, rhs: RHS) -> Result; + fn rem(self, rhs: RHS) -> Self::Output; } macro_rules! rem_impl { ($($t:ty)*) => ($( - impl Rem<$t, $t> for $t { + impl Rem for $t { + type Output = $t; + #[inline] fn rem(self, other: $t) -> $t { self % other } } @@ -290,7 +336,9 @@ macro_rules! rem_impl { macro_rules! rem_float_impl { ($t:ty, $fmod:ident) => { - impl Rem<$t, $t> for $t { + impl Rem for $t { + type Output = $t; + #[inline] fn rem(self, other: $t) -> $t { extern { fn $fmod(a: $t, b: $t) -> $t; } @@ -412,12 +460,16 @@ not_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 } /// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`. /// /// ``` +/// #![feature(associated_types)] +/// /// use std::ops::BitAnd; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl BitAnd<Foo, Foo> for Foo { +/// impl BitAnd for Foo { +/// type Output = Foo; +/// /// fn bitand(self, _rhs: Foo) -> Foo { /// println!("Bitwise And-ing!"); /// self @@ -429,14 +481,18 @@ not_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 } /// } /// ``` #[lang="bitand"] -pub trait BitAnd<RHS, Result> { +pub trait BitAnd<RHS=Self> { + type Output; + /// The method for the `&` operator - fn bitand(self, rhs: RHS) -> Result; + fn bitand(self, rhs: RHS) -> Self::Output; } macro_rules! bitand_impl { ($($t:ty)*) => ($( - impl BitAnd<$t, $t> for $t { + impl BitAnd for $t { + type Output = $t; + #[inline] fn bitand(self, rhs: $t) -> $t { self & rhs } } @@ -453,12 +509,16 @@ bitand_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 } /// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`. /// /// ``` +/// #![feature(associated_types)] +/// /// use std::ops::BitOr; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl BitOr<Foo, Foo> for Foo { +/// impl BitOr for Foo { +/// type Output = Foo; +/// /// fn bitor(self, _rhs: Foo) -> Foo { /// println!("Bitwise Or-ing!"); /// self @@ -470,14 +530,18 @@ bitand_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 } /// } /// ``` #[lang="bitor"] -pub trait BitOr<RHS, Result> { +pub trait BitOr<RHS=Self> { + type Output; + /// The method for the `|` operator - fn bitor(self, rhs: RHS) -> Result; + fn bitor(self, rhs: RHS) -> Self::Output; } macro_rules! bitor_impl { ($($t:ty)*) => ($( - impl BitOr<$t,$t> for $t { + impl BitOr for $t { + type Output = $t; + #[inline] fn bitor(self, rhs: $t) -> $t { self | rhs } } @@ -494,12 +558,16 @@ bitor_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 } /// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`. /// /// ``` +/// #![feature(associated_types)] +/// /// use std::ops::BitXor; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl BitXor<Foo, Foo> for Foo { +/// impl BitXor for Foo { +/// type Output = Foo; +/// /// fn bitxor(self, _rhs: Foo) -> Foo { /// println!("Bitwise Xor-ing!"); /// self @@ -511,14 +579,18 @@ bitor_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 } /// } /// ``` #[lang="bitxor"] -pub trait BitXor<RHS, Result> { +pub trait BitXor<RHS=Self> { + type Output; + /// The method for the `^` operator - fn bitxor(self, rhs: RHS) -> Result; + fn bitxor(self, rhs: RHS) -> Self::Output; } macro_rules! bitxor_impl { ($($t:ty)*) => ($( - impl BitXor<$t, $t> for $t { + impl BitXor for $t { + type Output = $t; + #[inline] fn bitxor(self, other: $t) -> $t { self ^ other } } @@ -535,12 +607,16 @@ bitxor_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 } /// calling `shl`, and therefore, `main` prints `Shifting left!`. /// /// ``` +/// #![feature(associated_types)] +/// /// use std::ops::Shl; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl Shl<Foo, Foo> for Foo { +/// impl Shl<Foo> for Foo { +/// type Output = Foo; +/// /// fn shl(self, _rhs: Foo) -> Foo { /// println!("Shifting left!"); /// self @@ -552,14 +628,18 @@ bitxor_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 } /// } /// ``` #[lang="shl"] -pub trait Shl<RHS, Result> { +pub trait Shl<RHS> { + type Output; + /// The method for the `<<` operator - fn shl(self, rhs: RHS) -> Result; + fn shl(self, rhs: RHS) -> Self::Output; } macro_rules! shl_impl { ($($t:ty)*) => ($( - impl Shl<uint, $t> for $t { + impl Shl<uint> for $t { + type Output = $t; + #[inline] fn shl(self, other: uint) -> $t { self << other @@ -578,12 +658,16 @@ shl_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 } /// calling `shr`, and therefore, `main` prints `Shifting right!`. /// /// ``` +/// #![feature(associated_types)] +/// /// use std::ops::Shr; /// /// #[deriving(Copy)] /// struct Foo; /// -/// impl Shr<Foo, Foo> for Foo { +/// impl Shr<Foo> for Foo { +/// type Output = Foo; +/// /// fn shr(self, _rhs: Foo) -> Foo { /// println!("Shifting right!"); /// self @@ -595,14 +679,18 @@ shl_impl! { uint u8 u16 u32 u64 int i8 i16 i32 i64 } /// } /// ``` #[lang="shr"] -pub trait Shr<RHS, Result> { +pub trait Shr<RHS> { + type Output; + /// The method for the `>>` operator - fn shr(self, rhs: RHS) -> Result; + fn shr(self, rhs: RHS) -> Self::Output; } macro_rules! shr_impl { ($($t:ty)*) => ($( - impl Shr<uint, $t> for $t { + impl Shr<uint> for $t { + type Output = $t; + #[inline] fn shr(self, other: uint) -> $t { self >> other } } |
