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