2012-08-13 18:20:27 -05:00
|
|
|
trait ToStr { fn to_str() -> ~str; }
|
2012-02-22 06:06:38 -06:00
|
|
|
|
2012-08-13 18:20:27 -05:00
|
|
|
impl int: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { int::str(self) }
|
2012-06-04 19:26:17 -05:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl i8: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { i8::str(self) }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl i16: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { i16::str(self) }
|
2012-05-26 01:47:02 -05:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl i32: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { i32::str(self) }
|
2012-05-26 01:47:02 -05:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl i64: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { i64::str(self) }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl uint: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { uint::str(self) }
|
2012-06-04 19:26:17 -05:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl u8: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { u8::str(self) }
|
2012-05-26 01:47:02 -05:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl u16: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { u16::str(self) }
|
2012-05-26 01:47:02 -05:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl u32: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { u32::str(self) }
|
2012-05-26 01:47:02 -05:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl u64: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { u64::str(self) }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl float: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { float::to_str(self, 4u) }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl bool: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { bool::to_str(self) }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl (): ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { ~"()" }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl ~str: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { self }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
|
2012-08-13 18:20:27 -05:00
|
|
|
impl<A: ToStr copy, B: ToStr copy> (A, B): ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str {
|
2012-02-22 06:06:38 -06:00
|
|
|
let (a, b) = self;
|
2012-07-14 00:57:48 -05:00
|
|
|
~"(" + a.to_str() + ~", " + b.to_str() + ~")"
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl<A: ToStr copy, B: ToStr copy, C: ToStr copy> (A, B, C): ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str {
|
2012-02-22 06:06:38 -06:00
|
|
|
let (a, b, c) = self;
|
2012-07-14 00:57:48 -05:00
|
|
|
~"(" + a.to_str() + ~", " + b.to_str() + ~", " + c.to_str() + ~")"
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-13 18:20:27 -05:00
|
|
|
impl<A: ToStr> ~[A]: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str {
|
|
|
|
let mut acc = ~"[", first = true;
|
2012-06-30 18:19:07 -05:00
|
|
|
for vec::each(self) |elt| {
|
2012-02-22 06:06:38 -06:00
|
|
|
if first { first = false; }
|
2012-07-14 00:57:48 -05:00
|
|
|
else { str::push_str(acc, ~", "); }
|
2012-07-06 13:35:46 -05:00
|
|
|
str::push_str(acc, elt.to_str());
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-07-06 13:35:46 -05:00
|
|
|
str::push_char(acc, ']');
|
2012-02-22 06:06:38 -06:00
|
|
|
acc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-13 18:20:27 -05:00
|
|
|
impl<A: ToStr> @A: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { ~"@" + (*self).to_str() }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl<A: ToStr> ~A: ToStr {
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str() -> ~str { ~"~" + (*self).to_str() }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn test_simple_types() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert 1.to_str() == ~"1";
|
|
|
|
assert (-1).to_str() == ~"-1";
|
|
|
|
assert 200u.to_str() == ~"200";
|
|
|
|
assert 2u8.to_str() == ~"2";
|
|
|
|
assert true.to_str() == ~"true";
|
|
|
|
assert false.to_str() == ~"false";
|
|
|
|
assert ().to_str() == ~"()";
|
|
|
|
assert (~"hi").to_str() == ~"hi";
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tuple_types() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (1, 2).to_str() == ~"(1, 2)";
|
|
|
|
assert (~"a", ~"b", false).to_str() == ~"(a, b, false)";
|
|
|
|
assert ((), ((), 100)).to_str() == ~"((), ((), 100))";
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
|
2012-07-08 02:39:20 -05:00
|
|
|
#[test]
|
2012-07-08 02:50:41 -05:00
|
|
|
#[ignore]
|
2012-02-22 06:06:38 -06:00
|
|
|
fn test_vectors() {
|
2012-06-29 18:26:56 -05:00
|
|
|
let x: ~[int] = ~[];
|
2012-07-14 00:57:48 -05:00
|
|
|
assert x.to_str() == ~"~[]";
|
|
|
|
assert (~[1]).to_str() == ~"~[1]";
|
|
|
|
assert (~[1, 2, 3]).to_str() == ~"~[1, 2, 3]";
|
2012-06-29 18:26:56 -05:00
|
|
|
assert (~[~[], ~[1], ~[1, 1]]).to_str() ==
|
2012-07-14 00:57:48 -05:00
|
|
|
~"~[~[], ~[1], ~[1, 1]]";
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
|
2012-07-08 02:39:20 -05:00
|
|
|
#[test]
|
2012-02-22 06:06:38 -06:00
|
|
|
fn test_pointer_types() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert (@1).to_str() == ~"@1";
|
|
|
|
assert (~(true, false)).to_str() == ~"~(true, false)";
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
}
|