libcore: convert binop traits to by value

This commit is contained in:
Jorge Aparicio 2014-12-01 13:00:23 -05:00
parent 227435a11e
commit c73259a269

View File

@ -103,12 +103,16 @@ pub trait Drop {
/// Foo + Foo;
/// }
/// ```
// NOTE(stage0): Remove trait after a snapshot
#[cfg(stage0)]
#[lang="add"]
pub trait Add<Sized? RHS,Result> for Sized? {
/// The method for the `+` operator
fn add(&self, rhs: &RHS) -> Result;
}
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! add_impl(
($($t:ty)*) => ($(
impl Add<$t, $t> for $t {
@ -118,6 +122,44 @@ macro_rules! add_impl(
)*)
)
/// The `Add` trait is used to specify the functionality of `+`.
///
/// # Example
///
/// A trivial implementation of `Add`. When `Foo + Foo` happens, it ends up
/// calling `add`, and therefore, `main` prints `Adding!`.
///
/// ```rust
/// struct Foo;
///
/// impl Add<Foo, Foo> for Foo {
/// fn add(&self, _rhs: &Foo) -> Foo {
/// println!("Adding!");
/// *self
/// }
/// }
///
/// fn main() {
/// Foo + Foo;
/// }
/// ```
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
#[lang="add"]
pub trait Add<RHS, Result> {
/// The method for the `+` operator
fn add(self, rhs: RHS) -> Result;
}
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! add_impl(
($($t:ty)*) => ($(
impl Add<$t, $t> for $t {
#[inline]
fn add(self, other: $t) -> $t { self + other }
}
)*)
)
add_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
/// The `Sub` trait is used to specify the functionality of `-`.
@ -143,12 +185,16 @@ add_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
/// Foo - Foo;
/// }
/// ```
// NOTE(stage0): Remove trait after a snapshot
#[cfg(stage0)]
#[lang="sub"]
pub trait Sub<Sized? RHS, Result> for Sized? {
/// The method for the `-` operator
fn sub(&self, rhs: &RHS) -> Result;
}
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! sub_impl(
($($t:ty)*) => ($(
impl Sub<$t, $t> for $t {
@ -158,6 +204,44 @@ macro_rules! sub_impl(
)*)
)
/// The `Sub` trait is used to specify the functionality of `-`.
///
/// # Example
///
/// A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
/// calling `sub`, and therefore, `main` prints `Subtracting!`.
///
/// ```rust
/// struct Foo;
///
/// impl Sub<Foo, Foo> for Foo {
/// fn sub(&self, _rhs: &Foo) -> Foo {
/// println!("Subtracting!");
/// *self
/// }
/// }
///
/// fn main() {
/// Foo - Foo;
/// }
/// ```
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
#[lang="sub"]
pub trait Sub<RHS, Result> {
/// The method for the `-` operator
fn sub(self, rhs: RHS) -> Result;
}
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! sub_impl(
($($t:ty)*) => ($(
impl Sub<$t, $t> for $t {
#[inline]
fn sub(self, other: $t) -> $t { self - other }
}
)*)
)
sub_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
/// The `Mul` trait is used to specify the functionality of `*`.
@ -183,12 +267,16 @@ sub_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
/// Foo * Foo;
/// }
/// ```
// NOTE(stage0): Remove trait after a snapshot
#[cfg(stage0)]
#[lang="mul"]
pub trait Mul<Sized? RHS, Result> for Sized? {
/// The method for the `*` operator
fn mul(&self, rhs: &RHS) -> Result;
}
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! mul_impl(
($($t:ty)*) => ($(
impl Mul<$t, $t> for $t {
@ -198,6 +286,44 @@ macro_rules! mul_impl(
)*)
)
/// The `Mul` trait is used to specify the functionality of `*`.
///
/// # Example
///
/// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
/// calling `mul`, and therefore, `main` prints `Multiplying!`.
///
/// ```rust
/// struct Foo;
///
/// impl Mul<Foo, Foo> for Foo {
/// fn mul(&self, _rhs: &Foo) -> Foo {
/// println!("Multiplying!");
/// *self
/// }
/// }
///
/// fn main() {
/// Foo * Foo;
/// }
/// ```
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
#[lang="mul"]
pub trait Mul<RHS, Result> {
/// The method for the `*` operator
fn mul(self, rhs: RHS) -> Result;
}
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! mul_impl(
($($t:ty)*) => ($(
impl Mul<$t, $t> for $t {
#[inline]
fn mul(self, other: $t) -> $t { self * other }
}
)*)
)
mul_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
/// The `Div` trait is used to specify the functionality of `/`.
@ -223,12 +349,16 @@ mul_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
/// Foo / Foo;
/// }
/// ```
// NOTE(stage0): Remove trait after a snapshot
#[cfg(stage0)]
#[lang="div"]
pub trait Div<Sized? RHS, Result> for Sized? {
/// The method for the `/` operator
fn div(&self, rhs: &RHS) -> Result;
}
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! div_impl(
($($t:ty)*) => ($(
impl Div<$t, $t> for $t {
@ -238,6 +368,44 @@ macro_rules! div_impl(
)*)
)
/// The `Div` trait is used to specify the functionality of `/`.
///
/// # Example
///
/// A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
/// calling `div`, and therefore, `main` prints `Dividing!`.
///
/// ```
/// struct Foo;
///
/// impl Div<Foo, Foo> for Foo {
/// fn div(&self, _rhs: &Foo) -> Foo {
/// println!("Dividing!");
/// *self
/// }
/// }
///
/// fn main() {
/// Foo / Foo;
/// }
/// ```
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
#[lang="div"]
pub trait Div<RHS, Result> {
/// The method for the `/` operator
fn div(self, rhs: RHS) -> Result;
}
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! div_impl(
($($t:ty)*) => ($(
impl Div<$t, $t> for $t {
#[inline]
fn div(self, other: $t) -> $t { self / other }
}
)*)
)
div_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
/// The `Rem` trait is used to specify the functionality of `%`.
@ -263,12 +431,16 @@ div_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64)
/// Foo % Foo;
/// }
/// ```
// NOTE(stage0): Remove trait after a snapshot
#[cfg(stage0)]
#[lang="rem"]
pub trait Rem<Sized? RHS, Result> for Sized? {
/// The method for the `%` operator
fn rem(&self, rhs: &RHS) -> Result;
}
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! rem_impl(
($($t:ty)*) => ($(
impl Rem<$t, $t> for $t {
@ -278,6 +450,8 @@ macro_rules! rem_impl(
)*)
)
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! rem_float_impl(
($t:ty, $fmod:ident) => {
impl Rem<$t, $t> for $t {
@ -290,6 +464,57 @@ macro_rules! rem_float_impl(
}
)
/// The `Rem` trait is used to specify the functionality of `%`.
///
/// # Example
///
/// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
/// calling `rem`, and therefore, `main` prints `Remainder-ing!`.
///
/// ```
/// struct Foo;
///
/// impl Rem<Foo, Foo> for Foo {
/// fn rem(&self, _rhs: &Foo) -> Foo {
/// println!("Remainder-ing!");
/// *self
/// }
/// }
///
/// fn main() {
/// Foo % Foo;
/// }
/// ```
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
#[lang="rem"]
pub trait Rem<RHS, Result> {
/// The method for the `%` operator
fn rem(self, rhs: RHS) -> Result;
}
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! rem_impl(
($($t:ty)*) => ($(
impl Rem<$t, $t> for $t {
#[inline]
fn rem(self, other: $t) -> $t { self % other }
}
)*)
)
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! rem_float_impl(
($t:ty, $fmod:ident) => {
impl Rem<$t, $t> for $t {
#[inline]
fn rem(self, other: $t) -> $t {
extern { fn $fmod(a: $t, b: $t) -> $t; }
unsafe { $fmod(self, other) }
}
}
}
)
rem_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
rem_float_impl!(f32, fmodf)
rem_float_impl!(f64, fmod)
@ -414,12 +639,16 @@ not_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
/// Foo & Foo;
/// }
/// ```
// NOTE(stage0): Remove trait after a snapshot
#[cfg(stage0)]
#[lang="bitand"]
pub trait BitAnd<Sized? RHS, Result> for Sized? {
/// The method for the `&` operator
fn bitand(&self, rhs: &RHS) -> Result;
}
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! bitand_impl(
($($t:ty)*) => ($(
impl BitAnd<$t, $t> for $t {
@ -429,6 +658,44 @@ macro_rules! bitand_impl(
)*)
)
/// The `BitAnd` trait is used to specify the functionality of `&`.
///
/// # Example
///
/// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
/// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
///
/// ```
/// struct Foo;
///
/// impl BitAnd<Foo, Foo> for Foo {
/// fn bitand(&self, _rhs: &Foo) -> Foo {
/// println!("Bitwise And-ing!");
/// *self
/// }
/// }
///
/// fn main() {
/// Foo & Foo;
/// }
/// ```
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
#[lang="bitand"]
pub trait BitAnd<RHS, Result> {
/// The method for the `&` operator
fn bitand(self, rhs: RHS) -> Result;
}
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! bitand_impl(
($($t:ty)*) => ($(
impl BitAnd<$t, $t> for $t {
#[inline]
fn bitand(self, rhs: $t) -> $t { self & rhs }
}
)*)
)
bitand_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
/// The `BitOr` trait is used to specify the functionality of `|`.
@ -454,12 +721,16 @@ bitand_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
/// Foo | Foo;
/// }
/// ```
// NOTE(stage0): Remove trait after a snapshot
#[cfg(stage0)]
#[lang="bitor"]
pub trait BitOr<Sized? RHS, Result> for Sized? {
/// The method for the `|` operator
fn bitor(&self, rhs: &RHS) -> Result;
}
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! bitor_impl(
($($t:ty)*) => ($(
impl BitOr<$t,$t> for $t {
@ -469,6 +740,44 @@ macro_rules! bitor_impl(
)*)
)
/// The `BitOr` trait is used to specify the functionality of `|`.
///
/// # Example
///
/// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
/// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
///
/// ```
/// struct Foo;
///
/// impl BitOr<Foo, Foo> for Foo {
/// fn bitor(&self, _rhs: &Foo) -> Foo {
/// println!("Bitwise Or-ing!");
/// *self
/// }
/// }
///
/// fn main() {
/// Foo | Foo;
/// }
/// ```
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
#[lang="bitor"]
pub trait BitOr<RHS, Result> {
/// The method for the `|` operator
fn bitor(self, rhs: RHS) -> Result;
}
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! bitor_impl(
($($t:ty)*) => ($(
impl BitOr<$t,$t> for $t {
#[inline]
fn bitor(self, rhs: $t) -> $t { self | rhs }
}
)*)
)
bitor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
/// The `BitXor` trait is used to specify the functionality of `^`.
@ -494,12 +803,16 @@ bitor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
/// Foo ^ Foo;
/// }
/// ```
// NOTE(stage0): Remove trait after a snapshot
#[cfg(stage0)]
#[lang="bitxor"]
pub trait BitXor<Sized? RHS, Result> for Sized? {
/// The method for the `^` operator
fn bitxor(&self, rhs: &RHS) -> Result;
}
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! bitxor_impl(
($($t:ty)*) => ($(
impl BitXor<$t, $t> for $t {
@ -509,6 +822,44 @@ macro_rules! bitxor_impl(
)*)
)
/// The `BitXor` trait is used to specify the functionality of `^`.
///
/// # Example
///
/// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
/// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
///
/// ```
/// struct Foo;
///
/// impl BitXor<Foo, Foo> for Foo {
/// fn bitxor(&self, _rhs: &Foo) -> Foo {
/// println!("Bitwise Xor-ing!");
/// *self
/// }
/// }
///
/// fn main() {
/// Foo ^ Foo;
/// }
/// ```
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
#[lang="bitxor"]
pub trait BitXor<RHS, Result> {
/// The method for the `^` operator
fn bitxor(self, rhs: RHS) -> Result;
}
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! bitxor_impl(
($($t:ty)*) => ($(
impl BitXor<$t, $t> for $t {
#[inline]
fn bitxor(self, other: $t) -> $t { self ^ other }
}
)*)
)
bitxor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
/// The `Shl` trait is used to specify the functionality of `<<`.
@ -534,12 +885,16 @@ bitxor_impl!(bool uint u8 u16 u32 u64 int i8 i16 i32 i64)
/// Foo << Foo;
/// }
/// ```
// NOTE(stage0): Remove trait after a snapshot
#[cfg(stage0)]
#[lang="shl"]
pub trait Shl<Sized? RHS, Result> for Sized? {
/// The method for the `<<` operator
fn shl(&self, rhs: &RHS) -> Result;
}
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! shl_impl(
($($t:ty)*) => ($(
impl Shl<uint, $t> for $t {
@ -551,6 +906,46 @@ macro_rules! shl_impl(
)*)
)
/// The `Shl` trait is used to specify the functionality of `<<`.
///
/// # Example
///
/// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
/// calling `shl`, and therefore, `main` prints `Shifting left!`.
///
/// ```
/// struct Foo;
///
/// impl Shl<Foo, Foo> for Foo {
/// fn shl(&self, _rhs: &Foo) -> Foo {
/// println!("Shifting left!");
/// *self
/// }
/// }
///
/// fn main() {
/// Foo << Foo;
/// }
/// ```
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
#[lang="shl"]
pub trait Shl<RHS, Result> {
/// The method for the `<<` operator
fn shl(self, rhs: RHS) -> Result;
}
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! shl_impl(
($($t:ty)*) => ($(
impl Shl<uint, $t> for $t {
#[inline]
fn shl(self, other: uint) -> $t {
self << other
}
}
)*)
)
shl_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
/// The `Shr` trait is used to specify the functionality of `>>`.
@ -576,12 +971,16 @@ shl_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
/// Foo >> Foo;
/// }
/// ```
// NOTE(stage0): Remove trait after a snapshot
#[cfg(stage0)]
#[lang="shr"]
pub trait Shr<Sized? RHS, Result> for Sized? {
/// The method for the `>>` operator
fn shr(&self, rhs: &RHS) -> Result;
}
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! shr_impl(
($($t:ty)*) => ($(
impl Shr<uint, $t> for $t {
@ -591,6 +990,44 @@ macro_rules! shr_impl(
)*)
)
/// The `Shr` trait is used to specify the functionality of `>>`.
///
/// # Example
///
/// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
/// calling `shr`, and therefore, `main` prints `Shifting right!`.
///
/// ```
/// struct Foo;
///
/// impl Shr<Foo, Foo> for Foo {
/// fn shr(&self, _rhs: &Foo) -> Foo {
/// println!("Shifting right!");
/// *self
/// }
/// }
///
/// fn main() {
/// Foo >> Foo;
/// }
/// ```
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
#[lang="shr"]
pub trait Shr<RHS, Result> {
/// The method for the `>>` operator
fn shr(self, rhs: RHS) -> Result;
}
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! shr_impl(
($($t:ty)*) => ($(
impl Shr<uint, $t> for $t {
#[inline]
fn shr(self, other: uint) -> $t { self >> other }
}
)*)
)
shr_impl!(uint u8 u16 u32 u64 int i8 i16 i32 i64)
/// The `Index` trait is used to specify the functionality of indexing operations