2018-08-30 07:18:55 -05:00
|
|
|
// run-pass
|
2014-03-07 01:43:39 -06: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 15:13:15 -05:00
|
|
|
|
2014-03-07 01:43:39 -06:00
|
|
|
trait GetRef<'a, T> {
|
|
|
|
fn get(&self) -> &'a T;
|
|
|
|
}
|
|
|
|
|
2015-03-30 08:38:27 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2014-08-27 20:46:52 -05:00
|
|
|
struct Box<'a, T:'a> {
|
2014-03-07 01:43:39 -06:00
|
|
|
t: &'a T
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,T:Clone> GetRef<'a,T> for Box<'a,T> {
|
|
|
|
fn get(&self) -> &'a T {
|
|
|
|
self.t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn add<'a,G:GetRef<'a, isize>>(g1: G, g2: G) -> isize {
|
2014-03-07 01:43:39 -06:00
|
|
|
*g1.get() + *g2.get()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let b1 = Box { t: &3 };
|
|
|
|
assert_eq!(add(b1, b1), 6);
|
2014-03-07 01:43:39 -06:00
|
|
|
}
|