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