2013-02-23 16:41:44 -06:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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-12-23 16:41:37 -06:00
|
|
|
use str;
|
|
|
|
|
2013-02-03 22:47:26 -06:00
|
|
|
pub trait ToStr {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str(&self) -> ~str;
|
2013-02-03 22:47:26 -06:00
|
|
|
}
|
2012-02-22 06:06:38 -06:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToStr for bool {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str(&self) -> ~str { ::bool::to_str(*self) }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2013-02-14 13:47:00 -06:00
|
|
|
impl ToStr for () {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str(&self) -> ~str { ~"()" }
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
|
2013-03-26 18:08:05 -05:00
|
|
|
impl<A:ToStr> ToStr for (A,) {
|
|
|
|
#[inline(always)]
|
|
|
|
fn to_str(&self) -> ~str {
|
|
|
|
match *self {
|
|
|
|
(ref a,) => {
|
|
|
|
~"(" + a.to_str() + ~", " + ~")"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 19:45:56 -06:00
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:ToStr,B:ToStr> ToStr for (A, B) {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-02-03 22:47:26 -06:00
|
|
|
// 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-20 19:07:17 -06:00
|
|
|
impl<A:ToStr,B:ToStr,C:ToStr> ToStr for (A, B, C) {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-02-03 22:47:26 -06:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-25 15:21:04 -05:00
|
|
|
impl<'self,A:ToStr> ToStr for &'self [A] {
|
2013-02-27 09:01:53 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-02-27 09:01:53 -06:00
|
|
|
unsafe {
|
|
|
|
// FIXME #4568
|
|
|
|
// Bleh -- not really unsafe
|
|
|
|
// push_str and push_char
|
|
|
|
let mut acc = ~"[", first = true;
|
|
|
|
for self.each |elt| {
|
|
|
|
unsafe {
|
|
|
|
if first { first = false; }
|
|
|
|
else { str::push_str(&mut acc, ~", "); }
|
|
|
|
str::push_str(&mut acc, elt.to_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str::push_char(&mut acc, ']');
|
|
|
|
acc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
impl<A:ToStr> ToStr for ~[A] {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-01-23 13:43:58 -06:00
|
|
|
unsafe {
|
2013-02-27 09:01:53 -06:00
|
|
|
// FIXME #4568
|
|
|
|
// Bleh -- not really unsafe
|
|
|
|
// push_str and push_char
|
|
|
|
let mut acc = ~"[", first = true;
|
|
|
|
for self.each |elt| {
|
|
|
|
unsafe {
|
|
|
|
if first { first = false; }
|
|
|
|
else { str::push_str(&mut acc, ~", "); }
|
|
|
|
str::push_str(&mut acc, elt.to_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
str::push_char(&mut acc, ']');
|
|
|
|
acc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A:ToStr> ToStr for @[A] {
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-02-27 09:01:53 -06:00
|
|
|
unsafe {
|
|
|
|
// FIXME #4568
|
2013-01-23 13:43:58 -06:00
|
|
|
// 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, ']');
|
2013-02-15 02:51:28 -06:00
|
|
|
acc
|
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-03-06 15:58:02 -06:00
|
|
|
fail_unless!(1i.to_str() == ~"1");
|
|
|
|
fail_unless!((-1i).to_str() == ~"-1");
|
|
|
|
fail_unless!(200u.to_str() == ~"200");
|
|
|
|
fail_unless!(2u8.to_str() == ~"2");
|
|
|
|
fail_unless!(true.to_str() == ~"true");
|
|
|
|
fail_unless!(false.to_str() == ~"false");
|
|
|
|
fail_unless!(().to_str() == ~"()");
|
|
|
|
fail_unless!((~"hi").to_str() == ~"hi");
|
|
|
|
fail_unless!((@"hi").to_str() == ~"hi");
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tuple_types() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!((1, 2).to_str() == ~"(1, 2)");
|
|
|
|
fail_unless!((~"a", ~"b", false).to_str() == ~"(a, b, false)");
|
|
|
|
fail_unless!(((), ((), 100)).to_str() == ~"((), ((), 100))");
|
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_vectors() {
|
2012-06-29 18:26:56 -05:00
|
|
|
let x: ~[int] = ~[];
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(x.to_str() == ~"[]");
|
|
|
|
fail_unless!((~[1]).to_str() == ~"[1]");
|
|
|
|
fail_unless!((~[1, 2, 3]).to_str() == ~"[1, 2, 3]");
|
|
|
|
fail_unless!((~[~[], ~[1], ~[1, 1]]).to_str() ==
|
|
|
|
~"[[], [1], [1, 1]]");
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
}
|