]> git.proxmox.com Git - rustc.git/blob - src/test/ui/operator-overloading.rs
New upstream version 1.49.0+dfsg1
[rustc.git] / src / test / ui / operator-overloading.rs
1 // run-pass
2
3 #![allow(unused_variables)]
4 use std::cmp;
5 use std::ops;
6
7 #[derive(Copy, Clone, Debug)]
8 struct Point {
9 x: isize,
10 y: isize
11 }
12
13 impl ops::Add for Point {
14 type Output = Point;
15
16 fn add(self, other: Point) -> Point {
17 Point {x: self.x + other.x, y: self.y + other.y}
18 }
19 }
20
21 impl ops::Sub for Point {
22 type Output = Point;
23
24 fn sub(self, other: Point) -> Point {
25 Point {x: self.x - other.x, y: self.y - other.y}
26 }
27 }
28
29 impl ops::Neg for Point {
30 type Output = Point;
31
32 fn neg(self) -> Point {
33 Point {x: -self.x, y: -self.y}
34 }
35 }
36
37 impl ops::Not for Point {
38 type Output = Point;
39
40 fn not(self) -> Point {
41 Point {x: !self.x, y: !self.y }
42 }
43 }
44
45 impl ops::Index<bool> for Point {
46 type Output = isize;
47
48 fn index(&self, x: bool) -> &isize {
49 if x {
50 &self.x
51 } else {
52 &self.y
53 }
54 }
55 }
56
57 impl cmp::PartialEq for Point {
58 fn eq(&self, other: &Point) -> bool {
59 (*self).x == (*other).x && (*self).y == (*other).y
60 }
61 fn ne(&self, other: &Point) -> bool { !(*self).eq(other) }
62 }
63
64 pub fn main() {
65 let mut p = Point {x: 10, y: 20};
66 p = p + Point {x: 101, y: 102};
67 p = p - Point {x: 100, y: 100};
68 assert_eq!(p + Point {x: 5, y: 5}, Point {x: 16, y: 27});
69 assert_eq!(-p, Point {x: -11, y: -22});
70 assert_eq!(p[true], 11);
71 assert_eq!(p[false], 22);
72
73 let q = !p;
74 assert_eq!(q.x, !(p.x));
75 assert_eq!(q.y, !(p.y));
76
77 // Issue #1733
78 result(p[true]);
79 }
80
81 fn result(i: isize) { }