]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | use std::cmp::PartialEq; |
2 | use std::ops::{Add, Sub, Mul}; | |
223e47cc | 3 | |
1a4d82fc | 4 | pub trait MyNum : Add<Output=Self> + Sub<Output=Self> + Mul<Output=Self> + PartialEq + Clone { |
223e47cc LB |
5 | } |
6 | ||
85aaf69f | 7 | #[derive(Clone, Debug)] |
223e47cc | 8 | pub struct MyInt { |
c34b1796 | 9 | pub val: isize |
223e47cc LB |
10 | } |
11 | ||
1a4d82fc JJ |
12 | impl Add for MyInt { |
13 | type Output = MyInt; | |
14 | ||
15 | fn add(self, other: MyInt) -> MyInt { mi(self.val + other.val) } | |
223e47cc LB |
16 | } |
17 | ||
1a4d82fc JJ |
18 | impl Sub for MyInt { |
19 | type Output = MyInt; | |
20 | ||
21 | fn sub(self, other: MyInt) -> MyInt { mi(self.val - other.val) } | |
223e47cc LB |
22 | } |
23 | ||
1a4d82fc JJ |
24 | impl Mul for MyInt { |
25 | type Output = MyInt; | |
26 | ||
27 | fn mul(self, other: MyInt) -> MyInt { mi(self.val * other.val) } | |
223e47cc LB |
28 | } |
29 | ||
1a4d82fc | 30 | impl PartialEq for MyInt { |
223e47cc LB |
31 | fn eq(&self, other: &MyInt) -> bool { self.val == other.val } |
32 | ||
33 | fn ne(&self, other: &MyInt) -> bool { !self.eq(other) } | |
34 | } | |
35 | ||
1a4d82fc | 36 | impl MyNum for MyInt {} |
223e47cc | 37 | |
c34b1796 | 38 | fn mi(v: isize) -> MyInt { MyInt { val: v } } |