2013-02-23 17:41:44 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08: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 16:52:32 -07:00
|
|
|
/*!
|
|
|
|
|
|
|
|
The `ToStr` trait for converting to strings
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-08-01 03:16:42 -04:00
|
|
|
use option::{Some, None};
|
2013-05-11 19:09:11 +05:30
|
|
|
use str::OwnedStr;
|
2013-05-11 02:14:36 +05:30
|
|
|
use hashmap::HashMap;
|
2013-05-11 19:09:11 +05:30
|
|
|
use hashmap::HashSet;
|
2013-05-11 02:14:36 +05:30
|
|
|
use hash::Hash;
|
2013-09-08 11:01:16 -04:00
|
|
|
use iter::Iterator;
|
2013-05-11 02:14:36 +05:30
|
|
|
use cmp::Eq;
|
2013-06-21 08:29:53 -04:00
|
|
|
use vec::ImmutableVector;
|
2012-12-23 17:41:37 -05:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// A generic trait for converting a value to a string
|
2013-02-03 20:47:26 -08:00
|
|
|
pub trait ToStr {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Converts the value of `self` to an owned string
|
2013-03-21 21:20:48 -07:00
|
|
|
fn to_str(&self) -> ~str;
|
2013-02-03 20:47:26 -08:00
|
|
|
}
|
2012-02-22 13:06:38 +01:00
|
|
|
|
2013-04-20 19:39:15 +02:00
|
|
|
/// Trait for converting a type to a string, consuming it in the process.
|
2013-12-15 01:04:22 +11:00
|
|
|
pub trait IntoStr {
|
2013-08-18 08:28:04 +10:00
|
|
|
/// Consume and convert to a string.
|
2013-06-16 11:04:53 +02:00
|
|
|
fn into_str(self) -> ~str;
|
2013-04-20 19:39:15 +02:00
|
|
|
}
|
|
|
|
|
2013-02-14 11:47:00 -08:00
|
|
|
impl ToStr for () {
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-03-21 21:20:48 -07:00
|
|
|
fn to_str(&self) -> ~str { ~"()" }
|
2012-02-22 13:06:38 +01:00
|
|
|
}
|
|
|
|
|
2013-03-26 16:08:05 -07:00
|
|
|
impl<A:ToStr> ToStr for (A,) {
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-03-26 16:08:05 -07:00
|
|
|
fn to_str(&self) -> ~str {
|
|
|
|
match *self {
|
|
|
|
(ref a,) => {
|
2013-09-27 17:02:31 -07:00
|
|
|
format!("({},)", (*a).to_str())
|
2013-03-26 16:08:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-18 17:45:56 -08:00
|
|
|
|
2013-07-23 16:40:25 +04:00
|
|
|
impl<A:ToStr+Hash+Eq, B:ToStr> ToStr for HashMap<A, B> {
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-05-11 02:14:36 +05:30
|
|
|
fn to_str(&self) -> ~str {
|
2013-06-06 18:54:14 -07:00
|
|
|
let mut acc = ~"{";
|
|
|
|
let mut first = true;
|
2013-08-03 12:45:23 -04:00
|
|
|
for (key, value) in self.iter() {
|
2013-05-11 02:14:36 +05:30
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else {
|
2013-05-11 19:09:11 +05:30
|
|
|
acc.push_str(", ");
|
2013-05-11 02:14:36 +05:30
|
|
|
}
|
2013-05-11 19:09:11 +05:30
|
|
|
acc.push_str(key.to_str());
|
|
|
|
acc.push_str(": ");
|
|
|
|
acc.push_str(value.to_str());
|
2013-05-11 02:14:36 +05:30
|
|
|
}
|
2013-05-11 19:09:11 +05:30
|
|
|
acc.push_char('}');
|
2013-05-11 02:14:36 +05:30
|
|
|
acc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-11 19:18:14 +05:30
|
|
|
impl<A:ToStr+Hash+Eq> ToStr for HashSet<A> {
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-05-11 19:18:14 +05:30
|
|
|
fn to_str(&self) -> ~str {
|
2013-06-06 18:54:14 -07:00
|
|
|
let mut acc = ~"{";
|
|
|
|
let mut first = true;
|
2013-08-03 12:45:23 -04:00
|
|
|
for element in self.iter() {
|
2013-06-04 21:43:41 -07:00
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(element.to_str());
|
2013-05-11 19:18:14 +05:30
|
|
|
}
|
2013-06-04 21:43:41 -07:00
|
|
|
acc.push_char('}');
|
|
|
|
acc
|
2013-05-11 19:18:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 17:07:17 -08:00
|
|
|
impl<A:ToStr,B:ToStr> ToStr for (A, B) {
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-03-21 21:20:48 -07:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-06-06 20:44:30 +05:30
|
|
|
// FIXME(#4653): this causes an llvm assertion
|
2013-02-03 20:47:26 -08:00
|
|
|
//let &(ref a, ref b) = self;
|
|
|
|
match *self {
|
|
|
|
(ref a, ref b) => {
|
2013-09-27 17:02:31 -07:00
|
|
|
format!("({}, {})", (*a).to_str(), (*b).to_str())
|
2013-02-03 20:47:26 -08:00
|
|
|
}
|
|
|
|
}
|
2012-02-22 13:06:38 +01:00
|
|
|
}
|
|
|
|
}
|
2013-05-11 19:09:11 +05:30
|
|
|
|
2013-02-20 17:07:17 -08:00
|
|
|
impl<A:ToStr,B:ToStr,C:ToStr> ToStr for (A, B, C) {
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-03-21 21:20:48 -07:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-06-06 20:44:30 +05:30
|
|
|
// FIXME(#4653): this causes an llvm assertion
|
2013-02-03 20:47:26 -08:00
|
|
|
//let &(ref a, ref b, ref c) = self;
|
|
|
|
match *self {
|
|
|
|
(ref a, ref b, ref c) => {
|
2013-09-27 17:02:31 -07:00
|
|
|
format!("({}, {}, {})",
|
2013-02-03 20:47:26 -08:00
|
|
|
(*a).to_str(),
|
|
|
|
(*b).to_str(),
|
|
|
|
(*c).to_str()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2012-02-22 13:06:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 23:16:18 -08:00
|
|
|
impl<'a,A:ToStr> ToStr for &'a [A] {
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-03-21 21:20:48 -07:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-06-06 18:54:14 -07:00
|
|
|
let mut acc = ~"[";
|
|
|
|
let mut first = true;
|
2013-08-03 12:45:23 -04:00
|
|
|
for elt in self.iter() {
|
2013-05-11 19:31:53 +05:30
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(elt.to_str());
|
2013-02-27 16:01:53 +01:00
|
|
|
}
|
2013-05-11 19:31:53 +05:30
|
|
|
acc.push_char(']');
|
2013-04-08 16:50:34 -04:00
|
|
|
acc
|
2013-02-27 16:01:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 17:07:17 -08:00
|
|
|
impl<A:ToStr> ToStr for ~[A] {
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-03-21 21:20:48 -07:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-06-06 18:54:14 -07:00
|
|
|
let mut acc = ~"[";
|
|
|
|
let mut first = true;
|
2013-08-03 12:45:23 -04:00
|
|
|
for elt in self.iter() {
|
2013-05-11 19:31:53 +05:30
|
|
|
if first {
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
acc.push_str(", ");
|
|
|
|
}
|
|
|
|
acc.push_str(elt.to_str());
|
2013-02-27 16:01:53 +01:00
|
|
|
}
|
2013-05-11 19:31:53 +05:30
|
|
|
acc.push_char(']');
|
2013-04-08 16:50:34 -04:00
|
|
|
acc
|
2013-02-27 16:01:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 13:06:38 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-05-11 04:00:00 +05:30
|
|
|
use hashmap::HashMap;
|
2013-05-11 20:35:44 +05:30
|
|
|
use hashmap::HashSet;
|
2013-07-17 21:32:49 +02:00
|
|
|
use container::{MutableSet, MutableMap};
|
2013-07-23 16:40:25 +04:00
|
|
|
use super::*;
|
|
|
|
|
2012-02-22 13:06:38 +01:00
|
|
|
#[test]
|
|
|
|
fn test_simple_types() {
|
2013-05-18 22:02:45 -04: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");
|
2012-02-22 13:06:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tuple_types() {
|
2013-05-18 22:02:45 -04: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 13:06:38 +01:00
|
|
|
}
|
|
|
|
|
2012-07-08 00:39:20 -07:00
|
|
|
#[test]
|
2012-02-22 13:06:38 +01:00
|
|
|
fn test_vectors() {
|
2012-06-29 16:26:56 -07:00
|
|
|
let x: ~[int] = ~[];
|
2013-05-18 22:02:45 -04: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 18:39:09 -07:00
|
|
|
assert!((~[~[], ~[1], ~[1, 1]]).to_str() ==
|
2013-03-06 13:58:02 -08:00
|
|
|
~"[[], [1], [1, 1]]");
|
2012-02-22 13:06:38 +01:00
|
|
|
}
|
2013-05-11 02:14:36 +05:30
|
|
|
|
2013-07-23 16:40:25 +04:00
|
|
|
struct StructWithToStrWithoutEqOrHash {
|
|
|
|
value: int
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToStr for StructWithToStrWithoutEqOrHash {
|
|
|
|
fn to_str(&self) -> ~str {
|
2013-09-27 17:02:31 -07:00
|
|
|
format!("s{}", self.value)
|
2013-07-23 16:40:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-11 17:40:52 +05:30
|
|
|
#[test]
|
|
|
|
fn test_hashmap() {
|
2013-07-23 16:40:25 +04:00
|
|
|
let mut table: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new();
|
|
|
|
let empty: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new();
|
2013-05-11 17:40:52 +05:30
|
|
|
|
2013-07-23 16:40:25 +04:00
|
|
|
table.insert(3, StructWithToStrWithoutEqOrHash { value: 4 });
|
|
|
|
table.insert(1, StructWithToStrWithoutEqOrHash { value: 2 });
|
2013-05-11 02:14:36 +05:30
|
|
|
|
2013-05-11 17:40:52 +05:30
|
|
|
let table_str = table.to_str();
|
2013-05-11 02:14:36 +05:30
|
|
|
|
2013-07-23 16:40:25 +04:00
|
|
|
assert!(table_str == ~"{1: s2, 3: s4}" || table_str == ~"{3: s4, 1: s2}");
|
2013-05-18 22:02:45 -04:00
|
|
|
assert_eq!(empty.to_str(), ~"{}");
|
2013-05-11 17:40:52 +05:30
|
|
|
}
|
2013-05-11 19:25:14 +05:30
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hashset() {
|
2013-05-11 20:35:44 +05:30
|
|
|
let mut set: HashSet<int> = HashSet::new();
|
|
|
|
let empty_set: HashSet<int> = HashSet::new();
|
2013-05-11 19:25:14 +05:30
|
|
|
|
|
|
|
set.insert(1);
|
|
|
|
set.insert(2);
|
|
|
|
|
|
|
|
let set_str = set.to_str();
|
|
|
|
|
|
|
|
assert!(set_str == ~"{1, 2}" || set_str == ~"{2, 1}");
|
2013-05-18 22:02:45 -04:00
|
|
|
assert_eq!(empty_set.to_str(), ~"{}");
|
2013-05-11 19:25:14 +05:30
|
|
|
}
|
2013-05-12 20:34:15 -04:00
|
|
|
}
|