]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/operator-multidispatch.rs
New upstream version 1.33.0+dfsg1
[rustc.git] / src / test / run-pass / operator-multidispatch.rs
CommitLineData
1a4d82fc
JJ
1// Test that we can overload the `+` operator for points so that two
2// points can be added, and a point can be added to an integer.
3
4use std::ops;
5
85aaf69f 6#[derive(Debug,PartialEq,Eq)]
1a4d82fc 7struct Point {
c34b1796
AL
8 x: isize,
9 y: isize
1a4d82fc
JJ
10}
11
12impl ops::Add for Point {
13 type Output = Point;
14
15 fn add(self, other: Point) -> Point {
16 Point {x: self.x + other.x, y: self.y + other.y}
17 }
18}
19
c34b1796 20impl ops::Add<isize> for Point {
1a4d82fc
JJ
21 type Output = Point;
22
c34b1796 23 fn add(self, other: isize) -> Point {
1a4d82fc
JJ
24 Point {x: self.x + other,
25 y: self.y + other}
26 }
27}
28
29pub fn main() {
30 let mut p = Point {x: 10, y: 20};
31 p = p + Point {x: 101, y: 102};
32 assert_eq!(p, Point {x: 111, y: 122});
33 p = p + 1;
34 assert_eq!(p, Point {x: 112, y: 123});
35}