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