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
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-05-11 08:39:11 -05:00
|
|
|
use str::OwnedStr;
|
2013-05-10 15:44:36 -05:00
|
|
|
use hashmap::HashMap;
|
2013-05-11 08:39:11 -05:00
|
|
|
use hashmap::HashSet;
|
2013-05-10 15:44:36 -05:00
|
|
|
use container::Map;
|
|
|
|
use hash::Hash;
|
|
|
|
use cmp::Eq;
|
2013-05-12 19:34:15 -05:00
|
|
|
use old_iter::BaseIter;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// A generic trait for converting a value to a string
|
2013-02-03 22:47:26 -06:00
|
|
|
pub trait ToStr {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Converts the value of `self` to an owned string
|
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-04-20 12:39:15 -05:00
|
|
|
/// Trait for converting a type to a string, consuming it in the process.
|
|
|
|
pub trait ToStrConsume {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Cosume and convert to a string.
|
2013-04-20 12:39:15 -05:00
|
|
|
fn to_str_consume(self) -> ~str;
|
|
|
|
}
|
|
|
|
|
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,) => {
|
2013-05-23 11:09:11 -05:00
|
|
|
~"(" + a.to_str() + ",)"
|
2013-03-26 18:08:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 19:45:56 -06:00
|
|
|
|
2013-05-10 15:44:36 -05:00
|
|
|
impl<A:ToStr+Hash+Eq, B:ToStr+Hash+Eq> ToStr for HashMap<A, B> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn to_str(&self) -> ~str {
|
2013-06-04 23:43:41 -05:00
|
|
|
let mut (acc, first) = (~"{", true);
|
2013-05-10 15:44:36 -05:00
|
|
|
for self.each |key, value| {
|
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else {
|
2013-05-11 08:39:11 -05:00
|
|
|
acc.push_str(", ");
|
2013-05-10 15:44:36 -05:00
|
|
|
}
|
2013-05-11 08:39:11 -05:00
|
|
|
acc.push_str(key.to_str());
|
|
|
|
acc.push_str(": ");
|
|
|
|
acc.push_str(value.to_str());
|
2013-05-10 15:44:36 -05:00
|
|
|
}
|
2013-05-11 08:39:11 -05:00
|
|
|
acc.push_char('}');
|
2013-05-10 15:44:36 -05:00
|
|
|
acc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-11 08:48:14 -05:00
|
|
|
impl<A:ToStr+Hash+Eq> ToStr for HashSet<A> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn to_str(&self) -> ~str {
|
2013-06-04 23:43:41 -05:00
|
|
|
let mut (acc, first) = (~"{", true);
|
|
|
|
for self.each |element| {
|
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(element.to_str());
|
2013-05-11 08:48:14 -05:00
|
|
|
}
|
2013-06-04 23:43:41 -05:00
|
|
|
acc.push_char('}');
|
|
|
|
acc
|
2013-05-11 08:48:14 -05: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) => {
|
2013-05-23 11:09:11 -05:00
|
|
|
~"(" + a.to_str() + ", " + b.to_str() + ")"
|
2013-02-03 22:47:26 -06:00
|
|
|
}
|
|
|
|
}
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
}
|
2013-05-11 08:39:11 -05: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-06-04 23:43:41 -05:00
|
|
|
let mut (acc, first) = (~"[", true);
|
2013-04-08 15:50:34 -05:00
|
|
|
for self.each |elt| {
|
2013-05-11 09:01:53 -05:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(elt.to_str());
|
2013-02-27 09:01:53 -06:00
|
|
|
}
|
2013-05-11 09:01:53 -05:00
|
|
|
acc.push_char(']');
|
2013-04-08 15:50:34 -05:00
|
|
|
acc
|
2013-02-27 09:01:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06-04 23:43:41 -05:00
|
|
|
let mut (acc, first) = (~"[", true);
|
2013-04-08 15:50:34 -05:00
|
|
|
for self.each |elt| {
|
2013-05-11 09:01:53 -05:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(elt.to_str());
|
2013-02-27 09:01:53 -06:00
|
|
|
}
|
2013-05-11 09:01:53 -05:00
|
|
|
acc.push_char(']');
|
2013-04-08 15:50:34 -05:00
|
|
|
acc
|
2013-02-27 09:01:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A:ToStr> ToStr for @[A] {
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-06-04 23:43:41 -05:00
|
|
|
let mut (acc, first) = (~"[", true);
|
2013-04-08 15:50:34 -05:00
|
|
|
for self.each |elt| {
|
2013-05-11 09:01:53 -05:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(elt.to_str());
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2013-05-11 09:01:53 -05:00
|
|
|
acc.push_char(']');
|
2013-04-08 15:50:34 -05: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 {
|
2013-05-10 17:30:00 -05:00
|
|
|
use hashmap::HashMap;
|
2013-05-11 10:05:44 -05:00
|
|
|
use hashmap::HashSet;
|
2013-05-12 10:32:12 -05:00
|
|
|
use container::Set;
|
2012-02-22 06:06:38 -06:00
|
|
|
#[test]
|
|
|
|
fn test_simple_types() {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(1i.to_str(), ~"1");
|
|
|
|
assert_eq!((-1i).to_str(), ~"-1");
|
|
|
|
assert_eq!(200u.to_str(), ~"200");
|
|
|
|
assert_eq!(2u8.to_str(), ~"2");
|
|
|
|
assert_eq!(true.to_str(), ~"true");
|
|
|
|
assert_eq!(false.to_str(), ~"false");
|
|
|
|
assert_eq!(().to_str(), ~"()");
|
|
|
|
assert_eq!((~"hi").to_str(), ~"hi");
|
|
|
|
assert_eq!((@"hi").to_str(), ~"hi");
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tuple_types() {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!((1, 2).to_str(), ~"(1, 2)");
|
|
|
|
assert_eq!((~"a", ~"b", false).to_str(), ~"(a, b, false)");
|
|
|
|
assert_eq!(((), ((), 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-05-18 21:02:45 -05:00
|
|
|
assert_eq!(x.to_str(), ~"[]");
|
|
|
|
assert_eq!((~[1]).to_str(), ~"[1]");
|
|
|
|
assert_eq!((~[1, 2, 3]).to_str(), ~"[1, 2, 3]");
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!((~[~[], ~[1], ~[1, 1]]).to_str() ==
|
2013-03-06 15:58:02 -06:00
|
|
|
~"[[], [1], [1, 1]]");
|
2012-02-22 06:06:38 -06:00
|
|
|
}
|
2013-05-10 15:44:36 -05:00
|
|
|
|
2013-05-11 07:10:52 -05:00
|
|
|
#[test]
|
|
|
|
fn test_hashmap() {
|
|
|
|
let mut table: HashMap<int, int> = HashMap::new();
|
|
|
|
let empty: HashMap<int, int> = HashMap::new();
|
|
|
|
|
|
|
|
table.insert(3, 4);
|
|
|
|
table.insert(1, 2);
|
2013-05-10 15:44:36 -05:00
|
|
|
|
2013-05-11 07:10:52 -05:00
|
|
|
let table_str = table.to_str();
|
2013-05-10 15:44:36 -05:00
|
|
|
|
2013-05-11 08:39:11 -05:00
|
|
|
assert!(table_str == ~"{1: 2, 3: 4}" || table_str == ~"{3: 4, 1: 2}");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(empty.to_str(), ~"{}");
|
2013-05-11 07:10:52 -05:00
|
|
|
}
|
2013-05-11 08:55:14 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hashset() {
|
2013-05-11 10:05:44 -05:00
|
|
|
let mut set: HashSet<int> = HashSet::new();
|
|
|
|
let empty_set: HashSet<int> = HashSet::new();
|
2013-05-11 08:55:14 -05:00
|
|
|
|
|
|
|
set.insert(1);
|
|
|
|
set.insert(2);
|
|
|
|
|
|
|
|
let set_str = set.to_str();
|
|
|
|
|
|
|
|
assert!(set_str == ~"{1, 2}" || set_str == ~"{2, 1}");
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(empty_set.to_str(), ~"{}");
|
2013-05-11 08:55:14 -05:00
|
|
|
}
|
2013-05-12 19:34:15 -05:00
|
|
|
}
|