Skip to content

Commit 7dc94b8

Browse files
committed
auto merge of #6414 : samebchase/rust/experimental, r=graydon
Implemented to_str() for HashMap and HashSet Added tests. Minor formatting and stylistic cleanups.
2 parents 638b394 + 0acb6ab commit 7dc94b8

File tree

1 file changed

+101
-14
lines changed

1 file changed

+101
-14
lines changed

src/libcore/to_str.rs

Lines changed: 101 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ The `ToStr` trait for converting to strings
1414
1515
*/
1616

17-
use str;
17+
use str::OwnedStr;
18+
use hashmap::HashMap;
19+
use hashmap::HashSet;
20+
use container::Map;
21+
use hash::Hash;
22+
use cmp::Eq;
1823

1924
pub trait ToStr {
2025
fn to_str(&self) -> ~str;
@@ -46,6 +51,44 @@ impl<A:ToStr> ToStr for (A,) {
4651
}
4752
}
4853

54+
impl<A:ToStr+Hash+Eq, B:ToStr+Hash+Eq> ToStr for HashMap<A, B> {
55+
#[inline(always)]
56+
fn to_str(&self) -> ~str {
57+
let mut acc = ~"{", first = true;
58+
for self.each |key, value| {
59+
if first {
60+
first = false;
61+
}
62+
else {
63+
acc.push_str(", ");
64+
}
65+
acc.push_str(key.to_str());
66+
acc.push_str(": ");
67+
acc.push_str(value.to_str());
68+
}
69+
acc.push_char('}');
70+
acc
71+
}
72+
}
73+
74+
impl<A:ToStr+Hash+Eq> ToStr for HashSet<A> {
75+
#[inline(always)]
76+
fn to_str(&self) -> ~str {
77+
let mut acc = ~"{", first = true;
78+
for self.each |element| {
79+
if first {
80+
first = false;
81+
}
82+
else {
83+
acc.push_str(", ");
84+
}
85+
acc.push_str(element.to_str());
86+
}
87+
acc.push_char('}');
88+
acc
89+
}
90+
}
91+
4992
impl<A:ToStr,B:ToStr> ToStr for (A, B) {
5093
#[inline(always)]
5194
fn to_str(&self) -> ~str {
@@ -58,6 +101,7 @@ impl<A:ToStr,B:ToStr> ToStr for (A, B) {
58101
}
59102
}
60103
}
104+
61105
impl<A:ToStr,B:ToStr,C:ToStr> ToStr for (A, B, C) {
62106
#[inline(always)]
63107
fn to_str(&self) -> ~str {
@@ -80,11 +124,15 @@ impl<'self,A:ToStr> ToStr for &'self [A] {
80124
fn to_str(&self) -> ~str {
81125
let mut acc = ~"[", first = true;
82126
for self.each |elt| {
83-
if first { first = false; }
84-
else { str::push_str(&mut acc, ~", "); }
85-
str::push_str(&mut acc, elt.to_str());
127+
if first {
128+
first = false;
129+
}
130+
else {
131+
acc.push_str(", ");
132+
}
133+
acc.push_str(elt.to_str());
86134
}
87-
str::push_char(&mut acc, ']');
135+
acc.push_char(']');
88136
acc
89137
}
90138
}
@@ -94,11 +142,15 @@ impl<A:ToStr> ToStr for ~[A] {
94142
fn to_str(&self) -> ~str {
95143
let mut acc = ~"[", first = true;
96144
for self.each |elt| {
97-
if first { first = false; }
98-
else { str::push_str(&mut acc, ~", "); }
99-
str::push_str(&mut acc, elt.to_str());
145+
if first {
146+
first = false;
147+
}
148+
else {
149+
acc.push_str(", ");
150+
}
151+
acc.push_str(elt.to_str());
100152
}
101-
str::push_char(&mut acc, ']');
153+
acc.push_char(']');
102154
acc
103155
}
104156
}
@@ -108,18 +160,25 @@ impl<A:ToStr> ToStr for @[A] {
108160
fn to_str(&self) -> ~str {
109161
let mut acc = ~"[", first = true;
110162
for self.each |elt| {
111-
if first { first = false; }
112-
else { str::push_str(&mut acc, ~", "); }
113-
str::push_str(&mut acc, elt.to_str());
163+
if first {
164+
first = false;
165+
}
166+
else {
167+
acc.push_str(", ");
168+
}
169+
acc.push_str(elt.to_str());
114170
}
115-
str::push_char(&mut acc, ']');
171+
acc.push_char(']');
116172
acc
117173
}
118174
}
119175
120176
#[cfg(test)]
121177
#[allow(non_implicitly_copyable_typarams)]
122178
mod tests {
179+
use hashmap::HashMap;
180+
use hashmap::HashSet;
181+
use container::Set;
123182
#[test]
124183
fn test_simple_types() {
125184
assert!(1i.to_str() == ~"1");
@@ -149,4 +208,32 @@ mod tests {
149208
assert!((~[~[], ~[1], ~[1, 1]]).to_str() ==
150209
~"[[], [1], [1, 1]]");
151210
}
152-
}
211+
212+
#[test]
213+
fn test_hashmap() {
214+
let mut table: HashMap<int, int> = HashMap::new();
215+
let empty: HashMap<int, int> = HashMap::new();
216+
217+
table.insert(3, 4);
218+
table.insert(1, 2);
219+
220+
let table_str = table.to_str();
221+
222+
assert!(table_str == ~"{1: 2, 3: 4}" || table_str == ~"{3: 4, 1: 2}");
223+
assert!(empty.to_str() == ~"{}");
224+
}
225+
226+
#[test]
227+
fn test_hashset() {
228+
let mut set: HashSet<int> = HashSet::new();
229+
let empty_set: HashSet<int> = HashSet::new();
230+
231+
set.insert(1);
232+
set.insert(2);
233+
234+
let set_str = set.to_str();
235+
236+
assert!(set_str == ~"{1, 2}" || set_str == ~"{2, 1}");
237+
assert!(empty_set.to_str() == ~"{}");
238+
}
239+
}

0 commit comments

Comments
 (0)