2012-12-10 17:32:48 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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-19 13:59:44 -07:00
|
|
|
// xfail-fast
|
2012-09-18 15:52:21 -07:00
|
|
|
#[legacy_modes];
|
|
|
|
|
2012-07-31 10:27:51 -07:00
|
|
|
trait to_str {
|
2012-07-13 22:57:48 -07:00
|
|
|
fn to_str() -> ~str;
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2012-08-07 18:10:06 -07:00
|
|
|
impl int: to_str {
|
2012-07-13 22:57:48 -07:00
|
|
|
fn to_str() -> ~str { int::str(self) }
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2012-08-07 18:10:06 -07:00
|
|
|
impl ~str: to_str {
|
2012-09-06 15:42:59 -07:00
|
|
|
fn to_str() -> ~str { copy self }
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2012-08-07 18:10:06 -07:00
|
|
|
impl (): to_str {
|
2012-07-13 22:57:48 -07:00
|
|
|
fn to_str() -> ~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> {
|
2012-09-07 14:52:28 -07:00
|
|
|
fn map<U: Copy>(f: fn(T) -> U) -> ~[U];
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
2012-08-07 18:10:06 -07:00
|
|
|
impl<T> ~[T]: map<T> {
|
2012-09-07 14:52:28 -07:00
|
|
|
fn map<U: Copy>(f: fn(T) -> U) -> ~[U] {
|
2012-06-29 16:26:56 -07:00
|
|
|
let mut r = ~[];
|
2012-09-19 16:55:01 -07:00
|
|
|
for self.each |x| { r += ~[f(*x)]; }
|
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
|
|
|
}
|
2012-07-13 22:57:48 -07:00
|
|
|
fn bar<U: to_str, T: map<U>>(x: T) -> ~[~str] {
|
2012-06-30 16:19:07 -07:00
|
|
|
x.map(|_e| _e.to_str() )
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|
|
|
|
|
2013-02-01 19:43:17 -08:00
|
|
|
pub fn main() {
|
2012-07-13 22:57:48 -07:00
|
|
|
assert foo(~[1]) == ~[~"hi"];
|
|
|
|
assert bar::<int, ~[int]>(~[4, 5]) == ~[~"4", ~"5"];
|
|
|
|
assert bar::<~str, ~[~str]>(~[~"x", ~"y"]) == ~[~"x", ~"y"];
|
|
|
|
assert bar::<(), ~[()]>(~[()]) == ~[~"()"];
|
2012-01-04 17:28:16 +01:00
|
|
|
}
|