]>
Commit | Line | Data |
---|---|---|
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 |
5 | trait MakeString { |
6 | fn make_string(&self) -> String; | |
7 | } | |
8 | ||
c34b1796 | 9 | impl MakeString for isize { |
1a4d82fc JJ |
10 | fn make_string(&self) -> String { |
11 | format!("{}", *self) | |
12 | } | |
13 | } | |
14 | ||
c34b1796 | 15 | impl MakeString for usize { |
1a4d82fc JJ |
16 | fn make_string(&self) -> String { |
17 | format!("{}", *self) | |
18 | } | |
19 | } | |
20 | ||
1a4d82fc JJ |
21 | trait Foo { |
22 | type F: MakeString; | |
23 | ||
24 | fn get(&self) -> &Self::F; | |
25 | } | |
26 | ||
27 | fn foo<F:Foo>(f: &F) -> String { | |
28 | f.get().make_string() | |
29 | } | |
30 | ||
1a4d82fc | 31 | struct SomeStruct { |
c34b1796 | 32 | field: isize, |
1a4d82fc JJ |
33 | } |
34 | ||
35 | impl 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 | 43 | struct SomeOtherStruct { |
c34b1796 | 44 | field: usize, |
1a4d82fc JJ |
45 | } |
46 | ||
47 | impl Foo for SomeOtherStruct { | |
c34b1796 | 48 | type F = usize; |
1a4d82fc | 49 | |
c34b1796 | 50 | fn get(&self) -> &usize { |
1a4d82fc JJ |
51 | &self.field |
52 | } | |
53 | } | |
54 | ||
55 | fn 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 | } |