diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index bc29a2b4a58..0090da3cdad 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -542,12 +542,16 @@ rem_float_impl! { f64, fmod } /// -Foo; /// } /// ``` +// NOTE(stage0): Remove trait after a snapshot +#[cfg(stage0)] #[lang="neg"] pub trait Neg for Sized? { /// The method for the unary `-` operator fn neg(&self) -> Result; } +// NOTE(stage0): Remove macro after a snapshot +#[cfg(stage0)] macro_rules! neg_impl { ($($t:ty)*) => ($( 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 { ($t:ty, $t_signed:ty) => { 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 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 { + /// 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_uint_impl! { uint, int } @@ -598,6 +654,8 @@ neg_uint_impl! { u64, i64 } /// !Foo; /// } /// ``` +// NOTE(stage0): Remove macro after a snapshot +#[cfg(stage0)] #[lang="not"] pub trait Not for Sized? { /// The method for the unary `!` operator @@ -605,6 +663,8 @@ pub trait Not for Sized? { } +// NOTE(stage0): Remove macro after a snapshot +#[cfg(stage0)] macro_rules! not_impl { ($($t:ty)*) => ($( 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 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 { + /// 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 } /// The `BitAnd` trait is used to specify the functionality of `&`.