]> git.proxmox.com Git - rustc.git/blame - src/test/ui/methods/method-projection.rs
New upstream version 1.39.0+dfsg1
[rustc.git] / src / test / ui / methods / method-projection.rs
CommitLineData
b7449926 1// run-pass
1a4d82fc
JJ
2// Test that we can use method notation to call methods based on a
3// projection bound from a trait. Issue #20469.
4
1a4d82fc
JJ
5trait MakeString {
6 fn make_string(&self) -> String;
7}
8
c34b1796 9impl MakeString for isize {
1a4d82fc
JJ
10 fn make_string(&self) -> String {
11 format!("{}", *self)
12 }
13}
14
c34b1796 15impl MakeString for usize {
1a4d82fc
JJ
16 fn make_string(&self) -> String {
17 format!("{}", *self)
18 }
19}
20
1a4d82fc
JJ
21trait Foo {
22 type F: MakeString;
23
24 fn get(&self) -> &Self::F;
25}
26
27fn foo<F:Foo>(f: &F) -> String {
28 f.get().make_string()
29}
30
1a4d82fc 31struct SomeStruct {
c34b1796 32 field: isize,
1a4d82fc
JJ
33}
34
35impl Foo for SomeStruct {
c34b1796 36 type F = isize;
1a4d82fc 37
c34b1796 38 fn get(&self) -> &isize {
1a4d82fc
JJ
39 &self.field
40 }
41}
42
1a4d82fc 43struct SomeOtherStruct {
c34b1796 44 field: usize,
1a4d82fc
JJ
45}
46
47impl Foo for SomeOtherStruct {
c34b1796 48 type F = usize;
1a4d82fc 49
c34b1796 50 fn get(&self) -> &usize {
1a4d82fc
JJ
51 &self.field
52 }
53}
54
55fn main() {
56 let x = SomeStruct { field: 22 };
57 assert_eq!(foo(&x), format!("22"));
58
59 let x = SomeOtherStruct { field: 44 };
60 assert_eq!(foo(&x), format!("44"));
61}