2014-02-07 20:08:32 +01:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-10 17:32:48 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-09-18 15:52:21 -07:00
|
|
|
|
2014-03-05 15:28:08 -08:00
|
|
|
|
2015-03-22 13:13:15 -07:00
|
|
|
|
2012-07-31 10:27:51 -07:00
|
|
|
trait to_str {
|
2014-06-21 03:39:03 -07:00
|
|
|
fn to_string_(&self) -> String;
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2015-03-25 17:06:52 -07:00
|
|
|
impl to_str for isize {
|
2014-06-21 03:39:03 -07:00
|
|
|
fn to_string_(&self) -> String { self.to_string() }
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2014-05-22 16:57:53 -07:00
|
|
|
impl to_str for String {
|
2014-06-21 03:39:03 -07:00
|
|
|
fn to_string_(&self) -> String { self.clone() }
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2013-02-14 11:47:00 -08:00
|
|
|
impl to_str for () {
|
2014-06-21 03:39:03 -07:00
|
|
|
fn to_string_(&self) -> String { "()".to_string() }
|
2012-01-06 10:23:55 +01:00
|
|
|
}
|
2012-01-04 17:28:16 +01:00
|
|
|
|
2012-07-31 10:27:51 -07:00
|
|
|
trait map<T> {
|
2015-01-02 17:32:54 -05:00
|
|
|
fn map<U, F>(&self, f: F) -> Vec<U> where F: FnMut(&T) -> U;
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2014-03-05 14:02:44 -08:00
|
|
|
impl<T> map<T> for Vec<T> {
|
2015-01-02 17:32:54 -05:00
|
|
|
fn map<U, F>(&self, mut f: F) -> Vec<U> where F: FnMut(&T) -> U {
|
2014-03-05 14:02:44 -08:00
|
|
|
let mut r = Vec::new();
|
2015-01-31 12:20:46 -05:00
|
|
|
for i in self {
|
2014-06-23 19:01:14 +02:00
|
|
|
r.push(f(i));
|
2013-06-24 18:34:20 -04:00
|
|
|
}
|
2012-01-04 17:28:16 +01:00
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
fn foo<U, T: map<U>>(x: T) -> Vec<String> {
|
2014-05-25 03:17:19 -07:00
|
|
|
x.map(|_e| "hi".to_string() )
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2014-05-22 16:57:53 -07:00
|
|
|
fn bar<U:to_str,T:map<U>>(x: T) -> Vec<String> {
|
2014-06-21 03:39:03 -07:00
|
|
|
x.map(|_e| _e.to_string_() )
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
|
|
|
|
2013-02-01 19:43:17 -08:00
|
|
|
pub fn main() {
|
2016-10-29 22:54:04 +01: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 21:15:45 +03:00
|
|
|
["x".to_string(), "y".to_string()]);
|
2016-10-29 22:54:04 +01:00
|
|
|
assert_eq!(bar::<(), Vec<()>>(vec![()]), ["()".to_string()]);
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|