]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/static-impl.rs
auto merge of #10679 : alexcrichton/rust/no-routine, r=pcwalton
[rust.git] / src / test / run-pass / static-impl.rs
1 // Copyright 2012 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 // xfail-fast
12
13 pub trait plus {
14     fn plus(&self) -> int;
15 }
16
17 mod a {
18     use plus;
19     impl plus for uint { fn plus(&self) -> int { *self as int + 20 } }
20 }
21
22 mod b {
23     use plus;
24     impl plus for ~str { fn plus(&self) -> int { 200 } }
25 }
26
27 trait uint_utils {
28     fn str(&self) -> ~str;
29     fn multi(&self, f: |uint|);
30 }
31
32 impl uint_utils for uint {
33     fn str(&self) -> ~str { self.to_str() }
34     fn multi(&self, f: |uint|) {
35         let mut c = 0u;
36         while c < *self { f(c); c += 1u; }
37     }
38 }
39
40 trait vec_utils<T> {
41     fn length_(&self, ) -> uint;
42     fn iter_(&self, f: |&T|);
43     fn map_<U>(&self, f: |&T| -> U) -> ~[U];
44 }
45
46 impl<T> vec_utils<T> for ~[T] {
47     fn length_(&self) -> uint { self.len() }
48     fn iter_(&self, f: |&T|) { for x in self.iter() { f(x); } }
49     fn map_<U>(&self, f: |&T| -> U) -> ~[U] {
50         let mut r = ~[];
51         for elt in self.iter() {
52             r.push(f(elt));
53         }
54         r
55     }
56 }
57
58 pub fn main() {
59     assert_eq!(10u.plus(), 30);
60     assert_eq!((~"hi").plus(), 200);
61
62     assert_eq!((~[1]).length_().str(), ~"1");
63     assert_eq!((~[3, 4]).map_(|a| *a + 4 )[0], 7);
64     assert_eq!((~[3, 4]).map_::<uint>(|a| *a as uint + 4u )[0], 7u);
65     let mut x = 0u;
66     10u.multi(|_n| x += 2u );
67     assert_eq!(x, 20u);
68 }