@@ -14,7 +14,12 @@ The `ToStr` trait for converting to strings
14
14
15
15
*/
16
16
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 ;
18
23
19
24
pub trait ToStr {
20
25
fn to_str ( & self ) -> ~str ;
@@ -46,6 +51,44 @@ impl<A:ToStr> ToStr for (A,) {
46
51
}
47
52
}
48
53
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
+
49
92
impl<A:ToStr,B:ToStr> ToStr for (A, B) {
50
93
#[inline(always)]
51
94
fn to_str(&self) -> ~str {
@@ -58,6 +101,7 @@ impl<A:ToStr,B:ToStr> ToStr for (A, B) {
58
101
}
59
102
}
60
103
}
104
+
61
105
impl < A : ToStr , B : ToStr , C : ToStr > ToStr for ( A , B , C ) {
62
106
#[ inline( always) ]
63
107
fn to_str ( & self ) -> ~str {
@@ -80,11 +124,15 @@ impl<'self,A:ToStr> ToStr for &'self [A] {
80
124
fn to_str ( & self ) -> ~str {
81
125
let mut acc = ~"[ ", first = true;
82
126
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());
86
134
}
87
- str:: push_char(&mut acc, ']');
135
+ acc. push_char(']');
88
136
acc
89
137
}
90
138
}
@@ -94,11 +142,15 @@ impl<A:ToStr> ToStr for ~[A] {
94
142
fn to_str(&self) -> ~str {
95
143
let mut acc = ~" [ ", first = true;
96
144
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());
100
152
}
101
- str:: push_char(&mut acc, ']');
153
+ acc. push_char(']');
102
154
acc
103
155
}
104
156
}
@@ -108,18 +160,25 @@ impl<A:ToStr> ToStr for @[A] {
108
160
fn to_str(&self) -> ~str {
109
161
let mut acc = ~" [ ", first = true;
110
162
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());
114
170
}
115
- str:: push_char(&mut acc, ']');
171
+ acc. push_char(']');
116
172
acc
117
173
}
118
174
}
119
175
120
176
#[cfg(test)]
121
177
#[allow(non_implicitly_copyable_typarams)]
122
178
mod tests {
179
+ use hashmap::HashMap;
180
+ use hashmap::HashSet;
181
+ use container::Set;
123
182
#[test]
124
183
fn test_simple_types() {
125
184
assert!(1i.to_str() == ~" 1 ") ;
@@ -149,4 +208,32 @@ mod tests {
149
208
assert!((~[~[], ~[1], ~[1, 1]]).to_str() ==
150
209
~" [ [ ] , [ 1 ] , [ 1 , 1 ] ] ");
151
210
}
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