1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 The `ToStr` trait for converting to strings
17 use option::{Some, None};
24 use vec::ImmutableVector;
26 /// A generic trait for converting a value to a string
28 /// Converts the value of `self` to an owned string
29 fn to_str(&self) -> ~str;
32 /// Trait for converting a type to a string, consuming it in the process.
33 pub trait ToStrConsume {
34 /// Consume and convert to a string.
35 fn into_str(self) -> ~str;
40 fn to_str(&self) -> ~str { ~"()" }
43 impl<A:ToStr> ToStr for (A,) {
45 fn to_str(&self) -> ~str {
48 fmt!("(%s,)", (*a).to_str())
54 impl<A:ToStr+Hash+Eq, B:ToStr> ToStr for HashMap<A, B> {
56 fn to_str(&self) -> ~str {
59 for (key, value) in self.iter() {
66 acc.push_str(key.to_str());
68 acc.push_str(value.to_str());
75 impl<A:ToStr+Hash+Eq> ToStr for HashSet<A> {
77 fn to_str(&self) -> ~str {
80 for element in self.iter() {
87 acc.push_str(element.to_str());
94 impl<A:ToStr,B:ToStr> ToStr for (A, B) {
96 fn to_str(&self) -> ~str {
97 // FIXME(#4653): this causes an llvm assertion
98 //let &(ref a, ref b) = self;
101 fmt!("(%s, %s)", (*a).to_str(), (*b).to_str())
107 impl<A:ToStr,B:ToStr,C:ToStr> ToStr for (A, B, C) {
109 fn to_str(&self) -> ~str {
110 // FIXME(#4653): this causes an llvm assertion
111 //let &(ref a, ref b, ref c) = self;
113 (ref a, ref b, ref c) => {
124 impl<'self,A:ToStr> ToStr for &'self [A] {
126 fn to_str(&self) -> ~str {
128 let mut first = true;
129 for elt in self.iter() {
136 acc.push_str(elt.to_str());
143 impl<A:ToStr> ToStr for ~[A] {
145 fn to_str(&self) -> ~str {
147 let mut first = true;
148 for elt in self.iter() {
155 acc.push_str(elt.to_str());
162 impl<A:ToStr> ToStr for @[A] {
164 fn to_str(&self) -> ~str {
166 let mut first = true;
167 for elt in self.iter() {
174 acc.push_str(elt.to_str());
183 use hashmap::HashMap;
184 use hashmap::HashSet;
185 use container::{MutableSet, MutableMap};
189 fn test_simple_types() {
190 assert_eq!(1i.to_str(), ~"1");
191 assert_eq!((-1i).to_str(), ~"-1");
192 assert_eq!(200u.to_str(), ~"200");
193 assert_eq!(2u8.to_str(), ~"2");
194 assert_eq!(true.to_str(), ~"true");
195 assert_eq!(false.to_str(), ~"false");
196 assert_eq!(().to_str(), ~"()");
197 assert_eq!((~"hi").to_str(), ~"hi");
198 assert_eq!((@"hi").to_str(), ~"hi");
202 fn test_tuple_types() {
203 assert_eq!((1, 2).to_str(), ~"(1, 2)");
204 assert_eq!((~"a", ~"b", false).to_str(), ~"(a, b, false)");
205 assert_eq!(((), ((), 100)).to_str(), ~"((), ((), 100))");
211 assert_eq!(x.to_str(), ~"[]");
212 assert_eq!((~[1]).to_str(), ~"[1]");
213 assert_eq!((~[1, 2, 3]).to_str(), ~"[1, 2, 3]");
214 assert!((~[~[], ~[1], ~[1, 1]]).to_str() ==
215 ~"[[], [1], [1, 1]]");
218 struct StructWithToStrWithoutEqOrHash {
222 impl ToStr for StructWithToStrWithoutEqOrHash {
223 fn to_str(&self) -> ~str {
224 fmt!("s%d", self.value)
230 let mut table: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new();
231 let empty: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new();
233 table.insert(3, StructWithToStrWithoutEqOrHash { value: 4 });
234 table.insert(1, StructWithToStrWithoutEqOrHash { value: 2 });
236 let table_str = table.to_str();
238 assert!(table_str == ~"{1: s2, 3: s4}" || table_str == ~"{3: s4, 1: s2}");
239 assert_eq!(empty.to_str(), ~"{}");
244 let mut set: HashSet<int> = HashSet::new();
245 let empty_set: HashSet<int> = HashSet::new();
250 let set_str = set.to_str();
252 assert!(set_str == ~"{1, 2}" || set_str == ~"{2, 1}");
253 assert_eq!(empty_set.to_str(), ~"{}");