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.
|
|
|
|
|
2014-02-07 20:08:32 +01:00
|
|
|
// ignore-fast
|
2012-09-18 15:52:21 -07:00
|
|
|
|
2012-07-31 10:27:51 -07:00
|
|
|
trait to_str {
|
2013-08-17 22:47:54 -04:00
|
|
|
fn to_string(&self) -> ~str;
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2013-02-14 11:47:00 -08:00
|
|
|
impl to_str for int {
|
2013-08-17 22:47:54 -04:00
|
|
|
fn to_string(&self) -> ~str { self.to_str() }
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2013-02-14 11:47:00 -08:00
|
|
|
impl to_str for ~str {
|
2013-08-17 22:47:54 -04:00
|
|
|
fn to_string(&self) -> ~str { self.clone() }
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2013-02-14 11:47:00 -08:00
|
|
|
impl to_str for () {
|
2013-08-17 22:47:54 -04:00
|
|
|
fn to_string(&self) -> ~str { ~"()" }
|
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> {
|
2013-11-19 16:34:19 -08:00
|
|
|
fn map<U>(&self, f: |&T| -> U) -> ~[U];
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2013-02-14 11:47:00 -08:00
|
|
|
impl<T> map<T> for ~[T] {
|
2013-11-19 16:34:19 -08:00
|
|
|
fn map<U>(&self, f: |&T| -> U) -> ~[U] {
|
2012-06-29 16:26:56 -07:00
|
|
|
let mut r = ~[];
|
2013-08-01 18:35:46 -04:00
|
|
|
// FIXME: #7355 generates bad code with VecIterator
|
2013-08-03 12:45:23 -04:00
|
|
|
for i in range(0u, self.len()) {
|
2013-06-11 19:13:42 -07:00
|
|
|
r.push(f(&self[i]));
|
2013-06-24 18:34:20 -04:00
|
|
|
}
|
2012-01-04 17:28:16 +01:00
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-13 22:57:48 -07:00
|
|
|
fn foo<U, T: map<U>>(x: T) -> ~[~str] {
|
|
|
|
x.map(|_e| ~"hi" )
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2013-02-20 17:07:17 -08:00
|
|
|
fn bar<U:to_str,T:map<U>>(x: T) -> ~[~str] {
|
2013-08-17 22:47:54 -04: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() {
|
2013-05-18 22:02:45 -04:00
|
|
|
assert_eq!(foo(~[1]), ~[~"hi"]);
|
|
|
|
assert_eq!(bar::<int, ~[int]>(~[4, 5]), ~[~"4", ~"5"]);
|
|
|
|
assert_eq!(bar::<~str, ~[~str]>(~[~"x", ~"y"]), ~[~"x", ~"y"]);
|
|
|
|
assert_eq!(bar::<(), ~[()]>(~[()]), ~[~"()"]);
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|