Skip to content

Commit 9fde578

Browse files
author
Jorge Aparicio
committed
use assoc types in unop traits
1 parent 9e8a187 commit 9fde578

File tree

6 files changed

+46
-20
lines changed

6 files changed

+46
-20
lines changed

src/libcore/num/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub trait Int
5858
+ Mul<Output=Self>
5959
+ Div<Output=Self>
6060
+ Rem<Output=Self>
61-
+ Not<Self>
61+
+ Not<Output=Self>
6262
+ BitAnd<Output=Self>
6363
+ BitOr<Output=Self>
6464
+ BitXor<Output=Self>
@@ -613,7 +613,7 @@ int_impl! { int = i64, u64, 64,
613613
#[unstable = "recently settled as part of numerics reform"]
614614
pub trait SignedInt
615615
: Int
616-
+ Neg<Self>
616+
+ Neg<Output=Self>
617617
{
618618
/// Computes the absolute value of `self`. `Int::min_value()` will be
619619
/// returned if the number is `Int::min_value()`.
@@ -1245,7 +1245,7 @@ pub trait Float
12451245
+ NumCast
12461246
+ PartialOrd
12471247
+ PartialEq
1248-
+ Neg<Self>
1248+
+ Neg<Output=Self>
12491249
+ Add<Output=Self>
12501250
+ Sub<Output=Self>
12511251
+ Mul<Output=Self>
@@ -1718,7 +1718,7 @@ macro_rules! trait_impl {
17181718
#[deprecated = "Generalised numbers are no longer supported"]
17191719
#[allow(deprecated)]
17201720
pub trait Num: PartialEq + Zero + One
1721-
+ Neg<Self>
1721+
+ Neg<Output=Self>
17221722
+ Add<Output=Self>
17231723
+ Sub<Output=Self>
17241724
+ Mul<Output=Self>

src/libcore/ops.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,17 @@ rem_float_impl! { f64, fmod }
360360
/// `neg`, and therefore, `main` prints `Negating!`.
361361
///
362362
/// ```
363+
/// #![feature(associated_types)]
364+
///
363365
/// use std::ops::Neg;
364366
///
365367
/// struct Foo;
366368
///
367369
/// impl Copy for Foo {}
368370
///
369-
/// impl Neg<Foo> for Foo {
371+
/// impl Neg for Foo {
372+
/// type Output = Foo;
373+
///
370374
/// fn neg(self) -> Foo {
371375
/// println!("Negating!");
372376
/// self
@@ -378,14 +382,18 @@ rem_float_impl! { f64, fmod }
378382
/// }
379383
/// ```
380384
#[lang="neg"]
381-
pub trait Neg<Result> {
385+
pub trait Neg {
386+
type Output;
387+
382388
/// The method for the unary `-` operator
383-
fn neg(self) -> Result;
389+
fn neg(self) -> Self::Output;
384390
}
385391

386392
macro_rules! neg_impl {
387393
($($t:ty)*) => ($(
388-
impl Neg<$t> for $t {
394+
impl Neg for $t {
395+
type Output = $t;
396+
389397
#[inline]
390398
fn neg(self) -> $t { -self }
391399
}
@@ -394,7 +402,9 @@ macro_rules! neg_impl {
394402

395403
macro_rules! neg_uint_impl {
396404
($t:ty, $t_signed:ty) => {
397-
impl Neg<$t> for $t {
405+
impl Neg for $t {
406+
type Output = $t;
407+
398408
#[inline]
399409
fn neg(self) -> $t { -(self as $t_signed) as $t }
400410
}
@@ -418,13 +428,17 @@ neg_uint_impl! { u64, i64 }
418428
/// `not`, and therefore, `main` prints `Not-ing!`.
419429
///
420430
/// ```
431+
/// #![feature(associated_types)]
432+
///
421433
/// use std::ops::Not;
422434
///
423435
/// struct Foo;
424436
///
425437
/// impl Copy for Foo {}
426438
///
427-
/// impl Not<Foo> for Foo {
439+
/// impl Not for Foo {
440+
/// type Output = Foo;
441+
///
428442
/// fn not(self) -> Foo {
429443
/// println!("Not-ing!");
430444
/// self
@@ -436,14 +450,18 @@ neg_uint_impl! { u64, i64 }
436450
/// }
437451
/// ```
438452
#[lang="not"]
439-
pub trait Not<Result> {
453+
pub trait Not {
454+
type Output;
455+
440456
/// The method for the unary `!` operator
441-
fn not(self) -> Result;
457+
fn not(self) -> Self::Output;
442458
}
443459

444460
macro_rules! not_impl {
445461
($($t:ty)*) => ($(
446-
impl Not<$t> for $t {
462+
impl Not for $t {
463+
type Output = $t;
464+
447465
#[inline]
448466
fn not(self) -> $t { !self }
449467
}

src/libstd/bitflags.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ macro_rules! bitflags {
249249
}
250250
}
251251

252-
impl ::std::ops::Not<$BitFlags> for $BitFlags {
252+
impl ::std::ops::Not for $BitFlags {
253+
type Output = $BitFlags;
254+
253255
/// Returns the complement of this set of flags.
254256
#[inline]
255257
fn not(self) -> $BitFlags {

src/libstd/time/duration.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ impl Duration {
262262
}
263263
}
264264

265-
impl Neg<Duration> for Duration {
265+
impl Neg for Duration {
266+
type Output = Duration;
267+
266268
#[inline]
267269
fn neg(self) -> Duration {
268270
if self.nanos == 0 {

src/test/compile-fail/unop-move-semantics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
use std::ops::Not;
1414

15-
fn move_then_borrow<T: Not<T> + Clone>(x: T) {
15+
fn move_then_borrow<T: Not<Output=T> + Clone>(x: T) {
1616
!x;
1717

1818
x.clone(); //~ ERROR: use of moved value
1919
}
2020

21-
fn move_borrowed<T: Not<T>>(x: T, mut y: T) {
21+
fn move_borrowed<T: Not<Output=T>>(x: T, mut y: T) {
2222
let m = &x;
2323
let n = &mut y;
2424

@@ -27,7 +27,7 @@ fn move_borrowed<T: Not<T>>(x: T, mut y: T) {
2727
!y; //~ ERROR: cannot move out of `y` because it is borrowed
2828
}
2929

30-
fn illegal_dereference<T: Not<T>>(mut x: T, y: T) {
30+
fn illegal_dereference<T: Not<Output=T>>(mut x: T, y: T) {
3131
let m = &mut x;
3232
let n = &y;
3333

src/test/run-pass/operator-overloading.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ impl ops::Sub for Point {
3535
}
3636
}
3737

38-
impl ops::Neg<Point> for Point {
38+
impl ops::Neg for Point {
39+
type Output = Point;
40+
3941
fn neg(self) -> Point {
4042
Point {x: -self.x, y: -self.y}
4143
}
4244
}
4345

44-
impl ops::Not<Point> for Point {
46+
impl ops::Not for Point {
47+
type Output = Point;
48+
4549
fn not(self) -> Point {
4650
Point {x: !self.x, y: !self.y }
4751
}

0 commit comments

Comments
 (0)