2012-08-14 14:11:15 -05:00
|
|
|
// NB: transitionary, de-mode-ing.
|
|
|
|
#[forbid(deprecated_mode)];
|
|
|
|
#[forbid(deprecated_pattern)];
|
|
|
|
|
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-09-12 19:59:29 -05:00
|
|
|
impl f32: ToStr {
|
|
|
|
fn to_str() -> ~str { float::to_str(self as float, 4u) }
|
|
|
|
}
|
|
|
|
impl f64: ToStr {
|
|
|
|
fn to_str() -> ~str { float::to_str(self as float, 4u) }
|
|
|
|
}
|
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-09-02 17:37:15 -05:00
|
|
|
fn to_str() -> ~str { copy self }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-08-25 20:42:07 -05:00
|
|
|
impl &str: ToStr {
|
|
|
|
fn to_str() -> ~str { str::from_slice(self) }
|
|
|
|
}
|
2012-09-12 14:15:27 -05:00
|
|
|
impl @str: ToStr {
|
|
|
|
fn to_str() -> ~str { str::from_slice(self) }
|
|
|
|
}
|
2012-02-22 06:06:38 -06:00
|
|
|
|
2012-09-07 16:52:28 -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-09-07 16:52:28 -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-09-10 14:14:14 -05:00
|
|
|
move acc
|
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
|
|
|
}
|
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)]
|
2012-09-02 18:34:20 -05:00
|
|
|
#[allow(non_implicitly_copyable_typarams)]
|
2012-02-22 06:06:38 -06:00
|
|
|
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-09-12 14:15:27 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|