]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/trait-generic.rs
auto merge of #15421 : catharsis/rust/doc-ffi-minor-fixes, r=alexcrichton
[rust.git] / src / test / run-pass / trait-generic.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 trait to_str {
14     fn to_string_(&self) -> String;
15 }
16 impl to_str for int {
17     fn to_string_(&self) -> String { self.to_string() }
18 }
19 impl to_str for String {
20     fn to_string_(&self) -> String { self.clone() }
21 }
22 impl to_str for () {
23     fn to_string_(&self) -> String { "()".to_string() }
24 }
25
26 trait map<T> {
27     fn map<U>(&self, f: |&T| -> U) -> Vec<U> ;
28 }
29 impl<T> map<T> for Vec<T> {
30     fn map<U>(&self, f: |&T| -> U) -> Vec<U> {
31         let mut r = Vec::new();
32         for i in self.iter() {
33             r.push(f(i));
34         }
35         r
36     }
37 }
38
39 fn foo<U, T: map<U>>(x: T) -> Vec<String> {
40     x.map(|_e| "hi".to_string() )
41 }
42 fn bar<U:to_str,T:map<U>>(x: T) -> Vec<String> {
43     x.map(|_e| _e.to_string_() )
44 }
45
46 pub fn main() {
47     assert_eq!(foo(vec!(1i)), vec!("hi".to_string()));
48     assert_eq!(bar::<int, Vec<int> >(vec!(4, 5)), vec!("4".to_string(), "5".to_string()));
49     assert_eq!(bar::<String, Vec<String> >(vec!("x".to_string(), "y".to_string())),
50                vec!("x".to_string(), "y".to_string()));
51     assert_eq!(bar::<(), Vec<()>>(vec!(())), vec!("()".to_string()));
52 }