]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/overloaded/overloaded-autoderef-order.rs
New upstream version 1.37.0+dfsg1
[rustc.git] / src / test / run-pass / overloaded / overloaded-autoderef-order.rs
CommitLineData
b7449926 1// run-pass
c34b1796 2
1a4d82fc
JJ
3use std::rc::Rc;
4use std::ops::Deref;
5
c34b1796 6#[derive(Copy, Clone)]
1a4d82fc
JJ
7struct DerefWrapper<X, Y> {
8 x: X,
9 y: Y
10}
11
1a4d82fc
JJ
12impl<X, Y> DerefWrapper<X, Y> {
13 fn get_x(self) -> X {
14 self.x
15 }
16}
17
18impl<X, Y> Deref for DerefWrapper<X, Y> {
19 type Target = Y;
20
21 fn deref(&self) -> &Y {
22 &self.y
23 }
24}
25
26mod priv_test {
27 use std::ops::Deref;
28
c34b1796 29 #[derive(Copy, Clone)]
1a4d82fc
JJ
30 pub struct DerefWrapperHideX<X, Y> {
31 x: X,
32 pub y: Y
33 }
34
1a4d82fc
JJ
35 impl<X, Y> DerefWrapperHideX<X, Y> {
36 pub fn new(x: X, y: Y) -> DerefWrapperHideX<X, Y> {
37 DerefWrapperHideX {
38 x: x,
39 y: y
40 }
41 }
42 }
43
44 impl<X, Y> Deref for DerefWrapperHideX<X, Y> {
45 type Target = Y;
46
47 fn deref(&self) -> &Y {
48 &self.y
49 }
50 }
51}
52
53pub fn main() {
85aaf69f 54 let nested = DerefWrapper {x: true, y: DerefWrapper {x: 0, y: 1}};
1a4d82fc
JJ
55
56 // Use the first field that you can find.
57 assert_eq!(nested.x, true);
58 assert_eq!((*nested).x, 0);
59
60 // Same for methods, even though there are multiple
61 // candidates (at different nesting levels).
62 assert_eq!(nested.get_x(), true);
63 assert_eq!((*nested).get_x(), 0);
64
65 // Also go through multiple levels of indirection.
66 assert_eq!(Rc::new(nested).x, true);
67
85aaf69f 68 let nested_priv = priv_test::DerefWrapperHideX::new(true, DerefWrapper {x: 0, y: 1});
ea8adc8c 69 assert_eq!(nested_priv.x, 0);
1a4d82fc
JJ
70 assert_eq!((*nested_priv).x, 0);
71}