2014-02-07 19:08:32 +00:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-11 01:32:48 +00: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 22:52:21 +00:00
|
|
|
|
2014-03-05 23:28:08 +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
|
|
|
}
|
2013-02-14 19:47:00 +00:00
|
|
|
impl to_str for int {
|
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> {
|
2014-03-05 22:02:44 +00:00
|
|
|
fn map<U>(&self, f: |&T| -> U) -> Vec<U> ;
|
2012-01-04 16:28:16 +00:00
|
|
|
}
|
2014-03-05 22:02:44 +00:00
|
|
|
impl<T> map<T> for Vec<T> {
|
|
|
|
fn map<U>(&self, f: |&T| -> U) -> Vec<U> {
|
|
|
|
let mut r = Vec::new();
|
2014-06-23 17:01:14 +00:00
|
|
|
for i in self.iter() {
|
|
|
|
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() {
|
2014-04-21 21:58:52 +00:00
|
|
|
assert_eq!(foo(vec!(1i)), vec!("hi".to_string()));
|
2014-05-25 10:17:19 +00:00
|
|
|
assert_eq!(bar::<int, Vec<int> >(vec!(4, 5)), vec!("4".to_string(), "5".to_string()));
|
|
|
|
assert_eq!(bar::<String, Vec<String> >(vec!("x".to_string(), "y".to_string())),
|
|
|
|
vec!("x".to_string(), "y".to_string()));
|
|
|
|
assert_eq!(bar::<(), Vec<()>>(vec!(())), vec!("()".to_string()));
|
2012-01-04 16:28:16 +00:00
|
|
|
}
|