]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
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 | ||
223e47cc | 11 | |
970d7e83 | 12 | |
c34b1796 AL |
13 | // pretty-expanded FIXME #23616 |
14 | ||
223e47cc | 15 | trait to_str { |
1a4d82fc | 16 | fn to_string_(&self) -> String; |
223e47cc | 17 | } |
c34b1796 | 18 | impl to_str for isize { |
1a4d82fc | 19 | fn to_string_(&self) -> String { self.to_string() } |
223e47cc | 20 | } |
1a4d82fc JJ |
21 | impl to_str for String { |
22 | fn to_string_(&self) -> String { self.clone() } | |
223e47cc LB |
23 | } |
24 | impl to_str for () { | |
1a4d82fc | 25 | fn to_string_(&self) -> String { "()".to_string() } |
223e47cc LB |
26 | } |
27 | ||
28 | trait map<T> { | |
1a4d82fc JJ |
29 | fn map<U, F>(&self, f: F) -> Vec<U> where F: FnMut(&T) -> U; |
30 | } | |
31 | impl<T> map<T> for Vec<T> { | |
32 | fn map<U, F>(&self, mut f: F) -> Vec<U> where F: FnMut(&T) -> U { | |
33 | let mut r = Vec::new(); | |
85aaf69f | 34 | for i in self { |
1a4d82fc | 35 | r.push(f(i)); |
970d7e83 | 36 | } |
223e47cc LB |
37 | r |
38 | } | |
39 | } | |
40 | ||
1a4d82fc JJ |
41 | fn foo<U, T: map<U>>(x: T) -> Vec<String> { |
42 | x.map(|_e| "hi".to_string() ) | |
223e47cc | 43 | } |
1a4d82fc JJ |
44 | fn bar<U:to_str,T:map<U>>(x: T) -> Vec<String> { |
45 | x.map(|_e| _e.to_string_() ) | |
223e47cc LB |
46 | } |
47 | ||
48 | pub fn main() { | |
c34b1796 AL |
49 | assert_eq!(foo(vec!(1)), ["hi".to_string()]); |
50 | assert_eq!(bar::<isize, Vec<isize> >(vec!(4, 5)), ["4".to_string(), "5".to_string()]); | |
1a4d82fc | 51 | assert_eq!(bar::<String, Vec<String> >(vec!("x".to_string(), "y".to_string())), |
c34b1796 AL |
52 | ["x".to_string(), "y".to_string()]); |
53 | assert_eq!(bar::<(), Vec<()>>(vec!(())), ["()".to_string()]); | |
223e47cc | 54 | } |