1
Fork 0

libcore: convert unop traits to by value

This commit is contained in:
Jorge Aparicio 2014-12-15 16:24:24 -05:00
parent 5caebb23cd
commit 5359879fb6

View file

@ -542,12 +542,16 @@ rem_float_impl! { f64, fmod }
/// -Foo; /// -Foo;
/// } /// }
/// ``` /// ```
// NOTE(stage0): Remove trait after a snapshot
#[cfg(stage0)]
#[lang="neg"] #[lang="neg"]
pub trait Neg<Result> for Sized? { pub trait Neg<Result> for Sized? {
/// The method for the unary `-` operator /// The method for the unary `-` operator
fn neg(&self) -> Result; fn neg(&self) -> Result;
} }
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! neg_impl { macro_rules! neg_impl {
($($t:ty)*) => ($( ($($t:ty)*) => ($(
impl Neg<$t> for $t { impl Neg<$t> for $t {
@ -557,6 +561,8 @@ macro_rules! neg_impl {
)*) )*)
} }
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! neg_uint_impl { macro_rules! neg_uint_impl {
($t:ty, $t_signed:ty) => { ($t:ty, $t_signed:ty) => {
impl Neg<$t> for $t { impl Neg<$t> for $t {
@ -566,6 +572,56 @@ macro_rules! neg_uint_impl {
} }
} }
/// The `Neg` trait is used to specify the functionality of unary `-`.
///
/// # Example
///
/// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
/// `neg`, and therefore, `main` prints `Negating!`.
///
/// ```
/// struct Foo;
///
/// impl Copy for Foo {}
///
/// impl Neg<Foo> for Foo {
/// fn neg(self) -> Foo {
/// println!("Negating!");
/// self
/// }
/// }
///
/// fn main() {
/// -Foo;
/// }
/// ```
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
#[lang="neg"]
pub trait Neg<Result> {
/// The method for the unary `-` operator
fn neg(self) -> Result;
}
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! neg_impl {
($($t:ty)*) => ($(
impl Neg<$t> for $t {
#[inline]
fn neg(self) -> $t { -self }
}
)*)
}
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! neg_uint_impl {
($t:ty, $t_signed:ty) => {
impl Neg<$t> for $t {
#[inline]
fn neg(self) -> $t { -(self as $t_signed) as $t }
}
}
}
neg_impl! { int i8 i16 i32 i64 f32 f64 } neg_impl! { int i8 i16 i32 i64 f32 f64 }
neg_uint_impl! { uint, int } neg_uint_impl! { uint, int }
@ -598,6 +654,8 @@ neg_uint_impl! { u64, i64 }
/// !Foo; /// !Foo;
/// } /// }
/// ``` /// ```
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
#[lang="not"] #[lang="not"]
pub trait Not<Result> for Sized? { pub trait Not<Result> for Sized? {
/// The method for the unary `!` operator /// The method for the unary `!` operator
@ -605,6 +663,8 @@ pub trait Not<Result> for Sized? {
} }
// NOTE(stage0): Remove macro after a snapshot
#[cfg(stage0)]
macro_rules! not_impl { macro_rules! not_impl {
($($t:ty)*) => ($( ($($t:ty)*) => ($(
impl Not<$t> for $t { impl Not<$t> for $t {
@ -614,6 +674,46 @@ macro_rules! not_impl {
)*) )*)
} }
/// The `Not` trait is used to specify the functionality of unary `!`.
///
/// # Example
///
/// A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
/// `not`, and therefore, `main` prints `Not-ing!`.
///
/// ```
/// struct Foo;
///
/// impl Copy for Foo {}
///
/// impl Not<Foo> for Foo {
/// fn not(self) -> Foo {
/// println!("Not-ing!");
/// self
/// }
/// }
///
/// fn main() {
/// !Foo;
/// }
/// ```
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
#[lang="not"]
pub trait Not<Result> {
/// The method for the unary `!` operator
fn not(self) -> Result;
}
#[cfg(not(stage0))] // NOTE(stage0): Remove cfg after a snapshot
macro_rules! not_impl {
($($t:ty)*) => ($(
impl Not<$t> for $t {
#[inline]
fn not(self) -> $t { !self }
}
)*)
}
not_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 } not_impl! { bool uint u8 u16 u32 u64 int i8 i16 i32 i64 }
/// The `BitAnd` trait is used to specify the functionality of `&`. /// The `BitAnd` trait is used to specify the functionality of `&`.