2018-08-30 12:18:55 +00:00
|
|
|
// run-pass
|
2018-08-31 13:02:01 +00:00
|
|
|
#![allow(non_camel_case_types)]
|
2012-09-18 22:52:21 +00:00
|
|
|
|
2014-03-05 23:28:08 +00:00
|
|
|
|
2015-03-22 20:13:15 +00:00
|
|
|
|
2012-07-31 17:27:51 +00:00
|
|
|
trait to_str {
|
2014-06-21 10:39:03 +00:00
|
|
|
fn to_string_(&self) -> String;
|
2012-01-04 16:28:16 +00:00
|
|
|
}
|
2015-03-26 00:06:52 +00:00
|
|
|
impl to_str for isize {
|
2014-06-21 10:39:03 +00:00
|
|
|
fn to_string_(&self) -> String { self.to_string() }
|
2012-01-04 16:28:16 +00:00
|
|
|
}
|
2014-05-22 23:57:53 +00:00
|
|
|
impl to_str for String {
|
2014-06-21 10:39:03 +00:00
|
|
|
fn to_string_(&self) -> String { self.clone() }
|
2012-01-04 16:28:16 +00:00
|
|
|
}
|
2013-02-14 19:47:00 +00:00
|
|
|
impl to_str for () {
|
2014-06-21 10:39:03 +00:00
|
|
|
fn to_string_(&self) -> String { "()".to_string() }
|
2012-01-06 09:23:55 +00:00
|
|
|
}
|
2012-01-04 16:28:16 +00:00
|
|
|
|
2012-07-31 17:27:51 +00:00
|
|
|
trait map<T> {
|
2015-01-02 22:32:54 +00:00
|
|
|
fn map<U, F>(&self, f: F) -> Vec<U> where F: FnMut(&T) -> U;
|
2012-01-04 16:28:16 +00:00
|
|
|
}
|
2014-03-05 22:02:44 +00:00
|
|
|
impl<T> map<T> for Vec<T> {
|
2015-01-02 22:32:54 +00:00
|
|
|
fn map<U, F>(&self, mut f: F) -> Vec<U> where F: FnMut(&T) -> U {
|
2014-03-05 22:02:44 +00:00
|
|
|
let mut r = Vec::new();
|
2015-01-31 17:20:46 +00:00
|
|
|
for i in self {
|
2014-06-23 17:01:14 +00:00
|
|
|
r.push(f(i));
|
2013-06-24 22:34:20 +00:00
|
|
|
}
|
2012-01-04 16:28:16 +00:00
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 23:57:53 +00:00
|
|
|
fn foo<U, T: map<U>>(x: T) -> Vec<String> {
|
2014-05-25 10:17:19 +00:00
|
|
|
x.map(|_e| "hi".to_string() )
|
2012-01-04 16:28:16 +00:00
|
|
|
}
|
2014-05-22 23:57:53 +00:00
|
|
|
fn bar<U:to_str,T:map<U>>(x: T) -> Vec<String> {
|
2014-06-21 10:39:03 +00:00
|
|
|
x.map(|_e| _e.to_string_() )
|
2012-01-04 16:28:16 +00:00
|
|
|
}
|
|
|
|
|
2013-02-02 03:43:17 +00:00
|
|
|
pub fn main() {
|
2016-10-29 21:54:04 +00:00
|
|
|
assert_eq!(foo(vec![1]), ["hi".to_string()]);
|
|
|
|
assert_eq!(bar::<isize, Vec<isize> >(vec![4, 5]), ["4".to_string(), "5".to_string()]);
|
|
|
|
assert_eq!(bar::<String, Vec<String> >(vec!["x".to_string(), "y".to_string()]),
|
2015-02-24 18:15:45 +00:00
|
|
|
["x".to_string(), "y".to_string()]);
|
2016-10-29 21:54:04 +00:00
|
|
|
assert_eq!(bar::<(), Vec<()>>(vec![()]), ["()".to_string()]);
|
2012-01-04 16:28:16 +00:00
|
|
|
}
|