]> git.lizzy.rs Git - rust.git/blob - src/test/ui/traits/generic.rs
Auto merge of #87284 - Aaron1011:remove-paren-special, r=petrochenkov
[rust.git] / src / test / ui / traits / generic.rs
1 // run-pass
2 #![allow(non_camel_case_types)]
3
4
5
6 trait to_str {
7     fn to_string_(&self) -> String;
8 }
9 impl to_str for isize {
10     fn to_string_(&self) -> String { self.to_string() }
11 }
12 impl to_str for String {
13     fn to_string_(&self) -> String { self.clone() }
14 }
15 impl to_str for () {
16     fn to_string_(&self) -> String { "()".to_string() }
17 }
18
19 trait map<T> {
20     fn map<U, F>(&self, f: F) -> Vec<U> where F: FnMut(&T) -> U;
21 }
22 impl<T> map<T> for Vec<T> {
23     fn map<U, F>(&self, mut f: F) -> Vec<U> where F: FnMut(&T) -> U {
24         let mut r = Vec::new();
25         for i in self {
26             r.push(f(i));
27         }
28         r
29     }
30 }
31
32 fn foo<U, T: map<U>>(x: T) -> Vec<String> {
33     x.map(|_e| "hi".to_string() )
34 }
35 fn bar<U:to_str,T:map<U>>(x: T) -> Vec<String> {
36     x.map(|_e| _e.to_string_() )
37 }
38
39 pub fn main() {
40     assert_eq!(foo(vec![1]), ["hi".to_string()]);
41     assert_eq!(bar::<isize, Vec<isize> >(vec![4, 5]), ["4".to_string(), "5".to_string()]);
42     assert_eq!(bar::<String, Vec<String> >(vec!["x".to_string(), "y".to_string()]),
43                ["x".to_string(), "y".to_string()]);
44     assert_eq!(bar::<(), Vec<()>>(vec![()]), ["()".to_string()]);
45 }