]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/method-projection.rs
Imported Upstream version 1.0.0~beta.3
[rustc.git] / src / test / run-pass / method-projection.rs
1 // Copyright 2014 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 can use method notation to call methods based on a
12 // projection bound from a trait. Issue #20469.
13
14 ///////////////////////////////////////////////////////////////////////////
15
16
17 trait MakeString {
18 fn make_string(&self) -> String;
19 }
20
21 impl MakeString for isize {
22 fn make_string(&self) -> String {
23 format!("{}", *self)
24 }
25 }
26
27 impl MakeString for usize {
28 fn make_string(&self) -> String {
29 format!("{}", *self)
30 }
31 }
32
33 ///////////////////////////////////////////////////////////////////////////
34
35 trait Foo {
36 type F: MakeString;
37
38 fn get(&self) -> &Self::F;
39 }
40
41 fn foo<F:Foo>(f: &F) -> String {
42 f.get().make_string()
43 }
44
45 ///////////////////////////////////////////////////////////////////////////
46
47 struct SomeStruct {
48 field: isize,
49 }
50
51 impl Foo for SomeStruct {
52 type F = isize;
53
54 fn get(&self) -> &isize {
55 &self.field
56 }
57 }
58
59 ///////////////////////////////////////////////////////////////////////////
60
61 struct SomeOtherStruct {
62 field: usize,
63 }
64
65 impl Foo for SomeOtherStruct {
66 type F = usize;
67
68 fn get(&self) -> &usize {
69 &self.field
70 }
71 }
72
73 fn main() {
74 let x = SomeStruct { field: 22 };
75 assert_eq!(foo(&x), format!("22"));
76
77 let x = SomeOtherStruct { field: 44 };
78 assert_eq!(foo(&x), format!("44"));
79 }