]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/trait-generic.rs
rustdoc: Replace no-pretty-expanded with pretty-expanded
[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 // pretty-expanded FIXME #23616
14
15 trait to_str {
16     fn to_string_(&self) -> String;
17 }
18 impl to_str for int {
19     fn to_string_(&self) -> String { self.to_string() }
20 }
21 impl to_str for String {
22     fn to_string_(&self) -> String { self.clone() }
23 }
24 impl to_str for () {
25     fn to_string_(&self) -> String { "()".to_string() }
26 }
27
28 trait map<T> {
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();
34         for i in self {
35             r.push(f(i));
36         }
37         r
38     }
39 }
40
41 fn foo<U, T: map<U>>(x: T) -> Vec<String> {
42     x.map(|_e| "hi".to_string() )
43 }
44 fn bar<U:to_str,T:map<U>>(x: T) -> Vec<String> {
45     x.map(|_e| _e.to_string_() )
46 }
47
48 pub fn main() {
49     assert_eq!(foo(vec!(1)), ["hi".to_string()]);
50     assert_eq!(bar::<int, Vec<int> >(vec!(4, 5)), ["4".to_string(), "5".to_string()]);
51     assert_eq!(bar::<String, Vec<String> >(vec!("x".to_string(), "y".to_string())),
52                ["x".to_string(), "y".to_string()]);
53     assert_eq!(bar::<(), Vec<()>>(vec!(())), ["()".to_string()]);
54 }