about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-12-19 06:12:01 +0000
committerbors <bors@rust-lang.org>2014-12-19 06:12:01 +0000
commit6bdce25e155d846bb9252fa4a18baef7e74cf8bf (patch)
tree80099a51ee183950cfa5661299e8dfcdeafd20c0 /src/libcore
parent840de072085df360733c48396224e9966e2dc72c (diff)
parent9b5de39c25b9b19ffcff3d519821b72a31d39d6c (diff)
downloadrust-6bdce25e155d846bb9252fa4a18baef7e74cf8bf.tar.gz
rust-6bdce25e155d846bb9252fa4a18baef7e74cf8bf.zip
auto merge of #19899 : japaric/rust/unops-by-value, r=nikomatsakis
- The following operator traits now take their argument by value: `Neg`, `Not`. This breaks all existing implementations of these traits.

- The unary operation `OP a` now "desugars" to `OpTrait::op_method(a)` and consumes its argument.

[breaking-change]

---

r? @nikomatsakis This PR is very similar to the binops-by-value PR
cc @aturon 
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/ops.rs100
1 files changed, 100 insertions, 0 deletions
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<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 @@ 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<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 @@ neg_uint_impl! { u64, i64 }
 ///     !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 @@ 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 }
 
 /// The `BitAnd` trait is used to specify the functionality of `&`.