blob: b6dab1c32e8b291e1212c9e322447d7afb926788 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
// check-pass
struct Ctxt {
v: usize,
}
trait GetCtxt {
// Here the `&` is bound in the method definition:
fn get_ctxt(&self) -> &Ctxt;
}
struct HasCtxt<'a> {
c: &'a Ctxt,
}
impl<'a> GetCtxt for HasCtxt<'a> {
// Ok: Have implied bound of WF(&'b HasCtxt<'a>)
// so know 'a: 'b
// so know &'a Ctxt <: &'b Ctxt
fn get_ctxt<'b>(&'b self) -> &'a Ctxt {
self.c
}
}
fn get_v(gc: Box<dyn GetCtxt + '_>) -> usize {
gc.get_ctxt().v
}
fn main() {
let ctxt = Ctxt { v: 22 };
let hc = HasCtxt { c: &ctxt };
assert_eq!(get_v(Box::new(hc) as Box<dyn GetCtxt>), 22);
}
|