2018-08-30 12:18:55 +00:00
|
|
|
// run-pass
|
2014-03-07 07:43:39 +00:00
|
|
|
// Tests that you can use a fn lifetime parameter as part of
|
|
|
|
// the value for a type parameter in a bound.
|
|
|
|
|
2015-03-22 20:13:15 +00:00
|
|
|
|
2014-03-07 07:43:39 +00:00
|
|
|
trait GetRef<'a, T> {
|
|
|
|
fn get(&self) -> &'a T;
|
|
|
|
}
|
|
|
|
|
2015-03-30 13:38:27 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2014-08-28 01:46:52 +00:00
|
|
|
struct Box<'a, T:'a> {
|
2014-03-07 07:43:39 +00:00
|
|
|
t: &'a T
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,T:Clone> GetRef<'a,T> for Box<'a,T> {
|
|
|
|
fn get(&self) -> &'a T {
|
|
|
|
self.t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 00:06:52 +00:00
|
|
|
fn add<'a,G:GetRef<'a, isize>>(g1: G, g2: G) -> isize {
|
2014-03-07 07:43:39 +00:00
|
|
|
*g1.get() + *g2.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2015-01-25 21:05:03 +00:00
|
|
|
let b1 = Box { t: &3 };
|
|
|
|
assert_eq!(add(b1, b1), 6);
|
2014-03-07 07:43:39 +00:00
|
|
|
}
|