|
| 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 | +// ignore-pretty |
| 12 | + |
| 13 | +struct X { val: i32 } |
| 14 | +impl std::ops::Deref for X { |
| 15 | + type Target = i32; |
| 16 | + fn deref(&self) -> &i32 { &self.val } |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +trait M { fn m(self); } |
| 21 | +impl M for i32 { fn m(self) { println!("i32::m()"); } } |
| 22 | +impl M for X { fn m(self) { println!("X::m()"); } } |
| 23 | +impl<'a> M for &'a X { fn m(self) { println!("&X::m()"); } } |
| 24 | +impl<'a, 'b> M for &'a &'b X { fn m(self) { println!("&&X::m()"); } } |
| 25 | +impl<'a, 'b, 'c> M for &'a &'b &'c X { fn m(self) { println!("&&&X::m()"); } } |
| 26 | + |
| 27 | +trait RefM { fn refm(&self); } |
| 28 | +impl RefM for i32 { fn refm(&self) { println!("i32::refm()"); } } |
| 29 | +impl RefM for X { fn refm(&self) { println!("X::refm()"); } } |
| 30 | +impl<'a> RefM for &'a X { fn refm(&self) { println!("&X::refm()"); } } |
| 31 | +impl<'a, 'b> RefM for &'a &'b X { fn refm(&self) { println!("&&X::refm()"); } } |
| 32 | +impl<'a, 'b, 'c> RefM for &'a &'b &'c X { fn refm(&self) { println!("&&&X::refm()"); } } |
| 33 | + |
| 34 | +struct Y { val: i32 } |
| 35 | +impl std::ops::Deref for Y { |
| 36 | + type Target = i32; |
| 37 | + fn deref(&self) -> &i32 { &self.val } |
| 38 | +} |
| 39 | + |
| 40 | +struct Z { val: Y } |
| 41 | +impl std::ops::Deref for Z { |
| 42 | + type Target = Y; |
| 43 | + fn deref(&self) -> &Y { &self.val } |
| 44 | +} |
| 45 | + |
| 46 | +struct A; |
| 47 | +impl std::marker::Copy for A {} |
| 48 | +impl Clone for A { fn clone(&self) -> Self { *self } } |
| 49 | +impl M for A { fn m(self) { println!("A::m()"); } } |
| 50 | +impl<'a, 'b, 'c> M for &'a &'b &'c A { fn m(self) { println!("&&&A::m()"); } } |
| 51 | +impl RefM for A { fn refm(&self) { println!("A::refm()"); } } |
| 52 | +impl<'a, 'b, 'c> RefM for &'a &'b &'c A { fn refm(&self) { println!("&&&A::refm()"); } } |
| 53 | + |
| 54 | +fn main() { |
| 55 | + // I'll use @ to denote left side of the dot operator |
| 56 | + (*X{val:42}).m(); // i32::refm() , self == @ |
| 57 | + X{val:42}.m(); // X::m() , self == @ |
| 58 | + (&X{val:42}).m(); // &X::m() , self == @ |
| 59 | + (&&X{val:42}).m(); // &&X::m() , self == @ |
| 60 | + (&&&X{val:42}).m(); // &&&X:m() , self == @ |
| 61 | + (&&&&X{val:42}).m(); // &&&X::m() , self == *@ |
| 62 | + (&&&&&X{val:42}).m(); // &&&X::m() , self == **@ |
| 63 | + |
| 64 | + (*X{val:42}).refm(); // i32::refm() , self == @ |
| 65 | + X{val:42}.refm(); // X::refm() , self == @ |
| 66 | + (&X{val:42}).refm(); // X::refm() , self == *@ |
| 67 | + (&&X{val:42}).refm(); // &X::refm() , self == *@ |
| 68 | + (&&&X{val:42}).refm(); // &&X::refm() , self == *@ |
| 69 | + (&&&&X{val:42}).refm(); // &&&X::refm(), self == *@ |
| 70 | + (&&&&&X{val:42}).refm(); // &&&X::refm(), self == **@ |
| 71 | + |
| 72 | + Y{val:42}.refm(); // i32::refm() , self == *@ |
| 73 | + Z{val:Y{val:42}}.refm(); // i32::refm() , self == **@ |
| 74 | + |
| 75 | + A.m(); // A::m() , self == @ |
| 76 | + // without the Copy trait, (&A).m() would be a compilation error: |
| 77 | + // cannot move out of borrowed content |
| 78 | + (&A).m(); // A::m() , self == *@ |
| 79 | + (&&A).m(); // &&&A::m() , self == &@ |
| 80 | + (&&&A).m(); // &&&A::m() , self == @ |
| 81 | + A.refm(); // A::refm() , self == @ |
| 82 | + (&A).refm(); // A::refm() , self == *@ |
| 83 | + (&&A).refm(); // A::refm() , self == **@ |
| 84 | + (&&&A).refm(); // &&&A::refm(), self == @ |
| 85 | +} |
0 commit comments