]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
c34b1796 AL |
11 | // pretty-expanded FIXME #23616 |
12 | ||
1a4d82fc JJ |
13 | use std::rc::Rc; |
14 | use std::ops::Deref; | |
15 | ||
c34b1796 | 16 | #[derive(Copy, Clone)] |
1a4d82fc JJ |
17 | struct DerefWrapper<X, Y> { |
18 | x: X, | |
19 | y: Y | |
20 | } | |
21 | ||
1a4d82fc JJ |
22 | impl<X, Y> DerefWrapper<X, Y> { |
23 | fn get_x(self) -> X { | |
24 | self.x | |
25 | } | |
26 | } | |
27 | ||
28 | impl<X, Y> Deref for DerefWrapper<X, Y> { | |
29 | type Target = Y; | |
30 | ||
31 | fn deref(&self) -> &Y { | |
32 | &self.y | |
33 | } | |
34 | } | |
35 | ||
36 | mod priv_test { | |
37 | use std::ops::Deref; | |
38 | ||
c34b1796 | 39 | #[derive(Copy, Clone)] |
1a4d82fc JJ |
40 | pub struct DerefWrapperHideX<X, Y> { |
41 | x: X, | |
42 | pub y: Y | |
43 | } | |
44 | ||
1a4d82fc JJ |
45 | impl<X, Y> DerefWrapperHideX<X, Y> { |
46 | pub fn new(x: X, y: Y) -> DerefWrapperHideX<X, Y> { | |
47 | DerefWrapperHideX { | |
48 | x: x, | |
49 | y: y | |
50 | } | |
51 | } | |
52 | } | |
53 | ||
54 | impl<X, Y> Deref for DerefWrapperHideX<X, Y> { | |
55 | type Target = Y; | |
56 | ||
57 | fn deref(&self) -> &Y { | |
58 | &self.y | |
59 | } | |
60 | } | |
61 | } | |
62 | ||
63 | pub fn main() { | |
85aaf69f | 64 | let nested = DerefWrapper {x: true, y: DerefWrapper {x: 0, y: 1}}; |
1a4d82fc JJ |
65 | |
66 | // Use the first field that you can find. | |
67 | assert_eq!(nested.x, true); | |
68 | assert_eq!((*nested).x, 0); | |
69 | ||
70 | // Same for methods, even though there are multiple | |
71 | // candidates (at different nesting levels). | |
72 | assert_eq!(nested.get_x(), true); | |
73 | assert_eq!((*nested).get_x(), 0); | |
74 | ||
75 | // Also go through multiple levels of indirection. | |
76 | assert_eq!(Rc::new(nested).x, true); | |
77 | ||
85aaf69f | 78 | let nested_priv = priv_test::DerefWrapperHideX::new(true, DerefWrapper {x: 0, y: 1}); |
1a4d82fc JJ |
79 | // FIXME(eddyb) #12808 should skip private fields. |
80 | // assert_eq!(nested_priv.x, 0); | |
81 | assert_eq!((*nested_priv).x, 0); | |
82 | } |