2012-12-03 18:48:01 -06: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 18:52:32 -05:00
|
|
|
/*!
|
|
|
|
|
|
|
|
The `ToStr` trait for converting to strings
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2012-08-14 14:11:15 -05:00
|
|
|
// NB: transitionary, de-mode-ing.
|
|
|
|
#[forbid(deprecated_mode)];
|
|
|
|
#[forbid(deprecated_pattern)];
|
|
|
|
|
2012-10-11 16:12:50 -05:00
|
|
|
pub trait ToStr { pure fn to_str() -> ~str; }
|
2012-02-22 06:06:38 -06:00
|
|
|
|
2012-08-13 18:20:27 -05:00
|
|
|
impl int: ToStr {
|
2012-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure 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 {
|
2012-10-11 16:12:50 -05:00
|
|
|
pure fn to_str() -> ~str { float::to_str(self as float, 4u) }
|
2012-09-12 19:59:29 -05:00
|
|
|
}
|
|
|
|
impl f64: ToStr {
|
2012-10-11 16:12:50 -05:00
|
|
|
pure fn to_str() -> ~str { float::to_str(self as float, 4u) }
|
2012-09-12 19:59:29 -05:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl bool: ToStr {
|
2012-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure fn to_str() -> ~str { ~"()" }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl ~str: ToStr {
|
2012-10-11 16:12:50 -05:00
|
|
|
pure 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 {
|
2012-10-11 16:12:50 -05:00
|
|
|
pure fn to_str() -> ~str { str::from_slice(self) }
|
2012-08-25 20:42:07 -05:00
|
|
|
}
|
2012-09-12 14:15:27 -05:00
|
|
|
impl @str: ToStr {
|
2012-10-11 16:12:50 -05:00
|
|
|
pure fn to_str() -> ~str { str::from_slice(self) }
|
2012-09-12 14:15:27 -05:00
|
|
|
}
|
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-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure fn to_str() -> ~str unsafe {
|
|
|
|
// Bleh -- not really unsafe
|
|
|
|
// push_str and push_char
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut acc = ~"[", first = true;
|
2012-10-11 16:12:50 -05:00
|
|
|
for vec::each(self) |elt| unsafe {
|
2012-02-22 06:06:38 -06:00
|
|
|
if first { first = false; }
|
2012-09-21 20:36:32 -05:00
|
|
|
else { str::push_str(&mut acc, ~", "); }
|
|
|
|
str::push_str(&mut acc, elt.to_str());
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-09-21 20:36:32 -05:00
|
|
|
str::push_char(&mut 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-10-11 16:12:50 -05:00
|
|
|
pure 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-10-11 16:12:50 -05:00
|
|
|
pure 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
|
|
|
}
|
|
|
|
}
|