]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/static-impl.rs
Imported Upstream version 1.0.0~beta
[rustc.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
12
13 // pretty-expanded FIXME #23616
14
15 pub trait plus {
16 fn plus(&self) -> isize;
17 }
18
19 mod a {
20 use plus;
21 impl plus for usize { fn plus(&self) -> isize { *self as isize + 20 } }
22 }
23
24 mod b {
25 use plus;
26 impl plus for String { fn plus(&self) -> isize { 200 } }
27 }
28
29 trait uint_utils {
30 fn str(&self) -> String;
31 fn multi<F>(&self, f: F) where F: FnMut(usize);
32 }
33
34 impl uint_utils for usize {
35 fn str(&self) -> String {
36 self.to_string()
37 }
38 fn multi<F>(&self, mut f: F) where F: FnMut(usize) {
39 let mut c = 0_usize;
40 while c < *self { f(c); c += 1_usize; }
41 }
42 }
43
44 trait vec_utils<T> {
45 fn length_(&self, ) -> usize;
46 fn iter_<F>(&self, f: F) where F: FnMut(&T);
47 fn map_<U, F>(&self, f: F) -> Vec<U> where F: FnMut(&T) -> U;
48 }
49
50 impl<T> vec_utils<T> for Vec<T> {
51 fn length_(&self) -> usize { self.len() }
52 fn iter_<F>(&self, mut f: F) where F: FnMut(&T) { for x in self { f(x); } }
53 fn map_<U, F>(&self, mut f: F) -> Vec<U> where F: FnMut(&T) -> U {
54 let mut r = Vec::new();
55 for elt in self {
56 r.push(f(elt));
57 }
58 r
59 }
60 }
61
62 pub fn main() {
63 assert_eq!(10_usize.plus(), 30);
64 assert_eq!(("hi".to_string()).plus(), 200);
65
66 assert_eq!((vec!(1)).length_().str(), "1".to_string());
67 let vect = vec!(3, 4).map_(|a| *a + 4);
68 assert_eq!(vect[0], 7);
69 let vect = (vec!(3, 4)).map_::<usize, _>(|a| *a as usize + 4_usize);
70 assert_eq!(vect[0], 7_usize);
71 let mut x = 0_usize;
72 10_usize.multi(|_n| x += 2_usize );
73 assert_eq!(x, 20_usize);
74 }