2018-08-30 14:18:55 +02:00
|
|
|
// run-pass
|
2018-09-25 23:51:35 +02:00
|
|
|
#![allow(dead_code)]
|
2015-03-22 13:13:15 -07:00
|
|
|
|
2014-03-22 21:44:16 +02:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
|
|
|
struct DerefWithHelper<H, T> {
|
2015-02-12 10:29:52 -05:00
|
|
|
helper: H,
|
|
|
|
value: T
|
2014-03-22 21:44:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
trait Helper<T> {
|
2014-07-17 21:44:59 -07:00
|
|
|
fn helper_borrow(&self) -> &T;
|
2014-03-22 21:44:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Helper<T> for Option<T> {
|
2014-07-17 21:44:59 -07:00
|
|
|
fn helper_borrow(&self) -> &T {
|
2014-03-22 21:44:16 +02:00
|
|
|
self.as_ref().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 14:53:20 -05:00
|
|
|
impl<T, H: Helper<T>> Deref for DerefWithHelper<H, T> {
|
|
|
|
type Target = T;
|
|
|
|
|
2014-07-17 21:44:59 -07:00
|
|
|
fn deref(&self) -> &T {
|
2014-03-22 21:44:16 +02:00
|
|
|
self.helper.helper_borrow()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
struct Foo {x: isize}
|
2014-03-22 21:44:16 +02:00
|
|
|
|
|
|
|
impl Foo {
|
2015-03-25 17:06:52 -07:00
|
|
|
fn foo(&self) -> isize {self.x}
|
2014-03-22 21:44:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2015-02-12 10:29:52 -05:00
|
|
|
let x: DerefWithHelper<Option<Foo>, Foo> =
|
|
|
|
DerefWithHelper { helper: Some(Foo {x: 5}), value: Foo { x: 2 } };
|
2015-06-07 21:00:38 +03:00
|
|
|
assert_eq!(x.foo(), 5);
|
2014-03-22 21:44:16 +02:00
|
|
|
}
|