2018-08-30 12:18:55 +00:00
|
|
|
// run-pass
|
2015-03-22 20:13:15 +00:00
|
|
|
|
2012-11-28 20:34:30 +00:00
|
|
|
pub trait Add<RHS,Result> {
|
2013-03-22 18:23:21 +00:00
|
|
|
fn add(&self, rhs: &RHS) -> Result;
|
2012-11-28 20:34:30 +00:00
|
|
|
}
|
|
|
|
|
2015-12-15 09:31:58 +00:00
|
|
|
trait MyNum : Sized + Add<Self,Self> { }
|
2012-11-28 20:34:30 +00:00
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
struct MyInt { val: isize }
|
2012-11-28 20:34:30 +00:00
|
|
|
|
2013-02-14 19:47:00 +00:00
|
|
|
impl Add<MyInt, MyInt> for MyInt {
|
2013-03-22 18:23:21 +00:00
|
|
|
fn add(&self, other: &MyInt) -> MyInt { mi(self.val + other.val) }
|
2012-11-28 20:34:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-19 19:09:52 +00:00
|
|
|
impl MyNum for MyInt {}
|
2012-11-28 20:34:30 +00:00
|
|
|
|
|
|
|
fn f<T:MyNum>(x: T, y: T) -> T {
|
|
|
|
return x.add(&y);
|
|
|
|
}
|
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
fn mi(v: isize) -> MyInt { MyInt { val: v } }
|
2012-11-28 20:34:30 +00:00
|
|
|
|
2013-02-02 03:43:17 +00:00
|
|
|
pub fn main() {
|
2012-11-28 20:34:30 +00:00
|
|
|
let (x, y) = (mi(3), mi(5));
|
|
|
|
let z = f(x, y);
|
2015-06-07 18:00:38 +00:00
|
|
|
assert_eq!(z.val, 8)
|
2012-11-28 20:34:30 +00:00
|
|
|
}
|