]> git.lizzy.rs Git - rust.git/commitdiff
libcore: convert unop traits to by value
authorJorge Aparicio <japaricious@gmail.com>
Mon, 15 Dec 2014 21:24:24 +0000 (16:24 -0500)
committerJorge Aparicio <japaricious@gmail.com>
Thu, 18 Dec 2014 20:02:27 +0000 (15:02 -0500)
src/libcore/ops.rs

index bc29a2b4a58cdc7a201d09c9e7c7ae394857fa18..0090da3cdad6edfc9ebbf4764f566702c28b5998 100644 (file)
@@ -542,12 +542,16 @@ fn rem(self, other: $t) -> $t {
 ///     -Foo;
 /// }
 /// ```
+// NOTE(stage0): Remove trait after a snapshot
+#[cfg(stage0)]
 #[lang="neg"]
 pub trait Neg<Result> 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 @@ fn neg(&self) -> $t { -*self }
     )*)
 }
 
+// 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 @@ fn neg(&self) -> $t { -(*self as $t_signed) as $t }
     }
 }
 
+/// 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_uint_impl! { uint, int }
@@ -598,6 +654,8 @@ fn neg(&self) -> $t { -(*self as $t_signed) as $t }
 ///     !Foo;
 /// }
 /// ```
+// NOTE(stage0): Remove macro after a snapshot
+#[cfg(stage0)]
 #[lang="not"]
 pub trait Not<Result> for Sized? {
     /// 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 {
     ($($t:ty)*) => ($(
         impl Not<$t> for $t {
@@ -614,6 +674,46 @@ fn not(&self) -> $t { !*self }
     )*)
 }
 
+/// 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 }
 
 /// The `BitAnd` trait is used to specify the functionality of `&`.