]> git.proxmox.com Git - rustc.git/blame - src/test/ui/traits/trait-inheritance-overloading.rs
New upstream version 1.51.0+dfsg1
[rustc.git] / src / test / ui / traits / trait-inheritance-overloading.rs
CommitLineData
b7449926 1// run-pass
1a4d82fc
JJ
2use std::cmp::PartialEq;
3use std::ops::{Add, Sub, Mul};
223e47cc 4
1a4d82fc 5trait MyNum : Add<Output=Self> + Sub<Output=Self> + Mul<Output=Self> + PartialEq + Clone { }
223e47cc 6
85aaf69f 7#[derive(Clone, Debug)]
c34b1796 8struct MyInt { val: isize }
223e47cc 9
1a4d82fc
JJ
10impl 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
16impl 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
22impl 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 28impl 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 33impl MyNum for MyInt {}
223e47cc 34
1a4d82fc
JJ
35fn 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 39fn mi(v: isize) -> MyInt { MyInt { val: v } }
223e47cc
LB
40
41pub 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}