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