]>
Commit | Line | Data |
---|---|---|
b7449926 | 1 | // run-pass |
1a4d82fc JJ |
2 | use std::cmp::PartialEq; |
3 | use std::ops::{Add, Sub, Mul}; | |
223e47cc | 4 | |
1a4d82fc | 5 | trait MyNum : Add<Output=Self> + Sub<Output=Self> + Mul<Output=Self> + PartialEq + Clone { } |
223e47cc | 6 | |
85aaf69f | 7 | #[derive(Clone, Debug)] |
c34b1796 | 8 | struct MyInt { val: isize } |
223e47cc | 9 | |
1a4d82fc JJ |
10 | impl Add for MyInt { |
11 | type Output = MyInt; | |
12 | ||
13 | fn add(self, other: MyInt) -> MyInt { mi(self.val + other.val) } | |
223e47cc LB |
14 | } |
15 | ||
1a4d82fc JJ |
16 | impl Sub for MyInt { |
17 | type Output = MyInt; | |
18 | ||
19 | fn sub(self, other: MyInt) -> MyInt { mi(self.val - other.val) } | |
223e47cc LB |
20 | } |
21 | ||
1a4d82fc JJ |
22 | impl Mul for MyInt { |
23 | type Output = MyInt; | |
24 | ||
25 | fn mul(self, other: MyInt) -> MyInt { mi(self.val * other.val) } | |
223e47cc LB |
26 | } |
27 | ||
1a4d82fc | 28 | impl PartialEq for MyInt { |
223e47cc LB |
29 | fn eq(&self, other: &MyInt) -> bool { self.val == other.val } |
30 | fn ne(&self, other: &MyInt) -> bool { !self.eq(other) } | |
31 | } | |
32 | ||
1a4d82fc | 33 | impl MyNum for MyInt {} |
223e47cc | 34 | |
1a4d82fc JJ |
35 | fn f<T:MyNum>(x: T, y: T) -> (T, T, T) { |
36 | return (x.clone() + y.clone(), x.clone() - y.clone(), x * y); | |
223e47cc LB |
37 | } |
38 | ||
c34b1796 | 39 | fn mi(v: isize) -> MyInt { MyInt { val: v } } |
223e47cc LB |
40 | |
41 | pub fn main() { | |
42 | let (x, y) = (mi(3), mi(5)); | |
43 | let (a, b, c) = f(x, y); | |
970d7e83 LB |
44 | assert_eq!(a, mi(8)); |
45 | assert_eq!(b, mi(-2)); | |
46 | assert_eq!(c, mi(15)); | |
223e47cc | 47 | } |