blob: f8e6d12088f9852af88ae127d1ccbb588bcc49e1 (
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
34
35
36
37
38
39
|
// run-pass
#![allow(dead_code)]
use std::ops::Deref;
struct DerefWithHelper<H, T> {
helper: H,
value: T
}
trait Helper<T> {
fn helper_borrow(&self) -> &T;
}
impl<T> Helper<T> for Option<T> {
fn helper_borrow(&self) -> &T {
self.as_ref().unwrap()
}
}
impl<T, H: Helper<T>> Deref for DerefWithHelper<H, T> {
type Target = T;
fn deref(&self) -> &T {
self.helper.helper_borrow()
}
}
struct Foo {x: isize}
impl Foo {
fn foo(&self) -> isize {self.x}
}
pub fn main() {
let x: DerefWithHelper<Option<Foo>, Foo> =
DerefWithHelper { helper: Some(Foo {x: 5}), value: Foo { x: 2 } };
assert_eq!(x.foo(), 5);
}
|