]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/operator-multidispatch.rs
New upstream version 1.19.0+dfsg1
[rustc.git] / src / test / run-pass / operator-multidispatch.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Test that we can overload the `+` operator for points so that two
12 // points can be added, and a point can be added to an integer.
13
14 use std::ops;
15
16 #[derive(Debug,PartialEq,Eq)]
17 struct Point {
18 x: isize,
19 y: isize
20 }
21
22 impl ops::Add for Point {
23 type Output = Point;
24
25 fn add(self, other: Point) -> Point {
26 Point {x: self.x + other.x, y: self.y + other.y}
27 }
28 }
29
30 impl ops::Add<isize> for Point {
31 type Output = Point;
32
33 fn add(self, other: isize) -> Point {
34 Point {x: self.x + other,
35 y: self.y + other}
36 }
37 }
38
39 pub fn main() {
40 let mut p = Point {x: 10, y: 20};
41 p = p + Point {x: 101, y: 102};
42 assert_eq!(p, Point {x: 111, y: 122});
43 p = p + 1;
44 assert_eq!(p, Point {x: 112, y: 123});
45 }