2014-03-22 14:44:16 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-03-22 15:13:15 -05:00
|
|
|
// pretty-expanded FIXME #23616
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Foo {x: int}
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self) -> int {self.x}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 } };
|
2014-03-22 14:44:16 -05:00
|
|
|
assert!(x.foo() == 5);
|
|
|
|
}
|