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
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use kinds::Copy;
|
2012-12-23 16:41:37 -06:00
|
|
|
use str;
|
|
|
|
use vec;
|
|
|
|
|
2013-02-03 22:47:26 -06:00
|
|
|
pub trait ToStr {
|
|
|
|
pure fn to_str(&self) -> ~str;
|
|
|
|
}
|
2012-02-22 06:06:38 -06:00
|
|
|
|
2012-08-13 18:20:27 -05:00
|
|
|
impl bool: ToStr {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-02-03 22:47:26 -06:00
|
|
|
pure fn to_str(&self) -> ~str { ::bool::to_str(*self) }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl (): ToStr {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-02-03 22:47:26 -06:00
|
|
|
pure fn to_str(&self) -> ~str { ~"()" }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-08-13 18:20:27 -05:00
|
|
|
impl ~str: ToStr {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-02-03 22:47:26 -06:00
|
|
|
pure fn to_str(&self) -> ~str { copy *self }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2012-08-25 20:42:07 -05:00
|
|
|
impl &str: ToStr {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-02-03 22:47:26 -06:00
|
|
|
pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
|
2012-08-25 20:42:07 -05:00
|
|
|
}
|
2012-09-12 14:15:27 -05:00
|
|
|
impl @str: ToStr {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-02-03 22:47:26 -06:00
|
|
|
pure fn to_str(&self) -> ~str { ::str::from_slice(*self) }
|
2012-09-12 14:15:27 -05:00
|
|
|
}
|
2012-02-22 06:06:38 -06:00
|
|
|
|
2013-02-03 22:47:26 -06:00
|
|
|
impl<A: ToStr, B: ToStr> (A, B): ToStr {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-02-03 22:47:26 -06:00
|
|
|
pure fn to_str(&self) -> ~str {
|
|
|
|
// FIXME(#4760): this causes an llvm assertion
|
|
|
|
//let &(ref a, ref b) = self;
|
|
|
|
match *self {
|
|
|
|
(ref a, ref b) => {
|
|
|
|
~"(" + a.to_str() + ~", " + b.to_str() + ~")"
|
|
|
|
}
|
|
|
|
}
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
}
|
2013-02-03 22:47:26 -06:00
|
|
|
impl<A: ToStr, B: ToStr, C: ToStr> (A, B, C): ToStr {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-02-03 22:47:26 -06:00
|
|
|
pure fn to_str(&self) -> ~str {
|
|
|
|
// FIXME(#4760): this causes an llvm assertion
|
|
|
|
//let &(ref a, ref b, ref c) = self;
|
|
|
|
match *self {
|
|
|
|
(ref a, ref b, ref c) => {
|
|
|
|
fmt!("(%s, %s, %s)",
|
|
|
|
(*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 {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-02-03 22:47:26 -06:00
|
|
|
pure fn to_str(&self) -> ~str {
|
2013-01-23 13:43:58 -06:00
|
|
|
unsafe {
|
|
|
|
// Bleh -- not really unsafe
|
|
|
|
// push_str and push_char
|
|
|
|
let mut acc = ~"[", first = true;
|
2013-02-03 22:47:26 -06:00
|
|
|
for self.each |elt| {
|
2013-01-23 13:43:58 -06:00
|
|
|
unsafe {
|
|
|
|
if first { first = false; }
|
|
|
|
else { str::push_str(&mut acc, ~", "); }
|
|
|
|
str::push_str(&mut acc, elt.to_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str::push_char(&mut acc, ']');
|
|
|
|
move acc
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-13 18:20:27 -05:00
|
|
|
impl<A: ToStr> @A: ToStr {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-02-03 22:47:26 -06:00
|
|
|
pure fn to_str(&self) -> ~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 {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-02-03 22:47:26 -06:00
|
|
|
pure fn to_str(&self) -> ~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() {
|
2013-01-30 18:21:10 -06:00
|
|
|
assert 1i.to_str() == ~"1";
|
|
|
|
assert (-1i).to_str() == ~"-1";
|
2012-07-14 00:57:48 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|