]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/overloaded_deref_with_ref_pattern.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / overloaded_deref_with_ref_pattern.rs
1 // Copyright 2015 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
11 // Test that we choose Deref or DerefMut appropriately based on mutability of ref bindings (#15609).
12
13 use std::ops::{Deref, DerefMut};
14
15 struct DerefOk<T>(T);
16 struct DerefMutOk<T>(T);
17
18 impl<T> Deref for DerefOk<T> {
19     type Target = T;
20     fn deref(&self) -> &Self::Target {
21         &self.0
22     }
23 }
24
25 impl<T> DerefMut for DerefOk<T> {
26     fn deref_mut(&mut self) -> &mut Self::Target {
27         panic!()
28     }
29 }
30
31 impl<T> Deref for DerefMutOk<T> {
32     type Target = T;
33     fn deref(&self) -> &Self::Target {
34         panic!()
35     }
36 }
37
38 impl<T> DerefMut for DerefMutOk<T> {
39     fn deref_mut(&mut self) -> &mut Self::Target {
40         &mut self.0
41     }
42 }
43
44 fn main() {
45     // Check that mutable ref binding in match picks DerefMut
46     let mut b = DerefMutOk(0);
47     match *b {
48         ref mut n => n,
49     };
50
51     // Check that mutable ref binding in let picks DerefMut
52     let mut y = DerefMutOk(1);
53     let ref mut z = *y;
54
55     // Check that immutable ref binding in match picks Deref
56     let mut b = DerefOk(2);
57     match *b {
58         ref n => n,
59     };
60
61     // Check that immutable ref binding in let picks Deref
62     let mut y = DerefOk(3);
63     let ref z = *y;
64
65     // Check that mixed mutable/immutable ref binding in match picks DerefMut
66     let mut b = DerefMutOk((0, 9));
67     match *b {
68         (ref mut n, ref m) => (n, m),
69     };
70
71     let mut b = DerefMutOk((0, 9));
72     match *b {
73         (ref n, ref mut m) => (n, m),
74     };
75
76     // Check that mixed mutable/immutable ref binding in let picks DerefMut
77     let mut y = DerefMutOk((1, 8));
78     let (ref mut z, ref a) = *y;
79
80     let mut y = DerefMutOk((1, 8));
81     let (ref z, ref mut a) = *y;
82
83     // Check that multiple immutable ref bindings in match picks Deref
84     let mut b = DerefOk((2, 7));
85     match *b {
86         (ref n, ref m) => (n, m),
87     };
88
89     // Check that multiple immutable ref bindings in let picks Deref
90     let mut y = DerefOk((3, 6));
91     let (ref z, ref a) = *y;
92
93     // Check that multiple mutable ref bindings in match picks DerefMut
94     let mut b = DerefMutOk((4, 5));
95     match *b {
96         (ref mut n, ref mut m) => (n, m),
97     };
98
99     // Check that multiple mutable ref bindings in let picks DerefMut
100     let mut y = DerefMutOk((5, 4));
101     let (ref mut z, ref mut a) = *y;
102 }