Skip to content

Commit 28519c8

Browse files
gwillengraydon
authored andcommitted
Add to_str for hashmap.
1 parent 06ac0c2 commit 28519c8

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/libstd/map.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! A map type
22
33
import chained::hashmap;
4+
import io::writer_util;
5+
import to_str::to_str;
46
export hashmap, hashfn, eqfn, set, map, chained, hashmap, str_hash;
57
export box_str_hash;
68
export bytes_hash, int_hash, uint_hash, set_add;
@@ -98,6 +100,7 @@ mod chained {
98100
hasher: hashfn<K>,
99101
eqer: eqfn<K>
100102
};
103+
type t<K, V> = @hashmap_<K, V>;
101104

102105
enum hashmap_<K, V> {
103106
hashmap_(@hashmap__<K, V>)
@@ -111,7 +114,7 @@ mod chained {
111114
found_after(@entry<K,V>, @entry<K,V>)
112115
}
113116

114-
impl private_methods<K, V: copy> for t<K, V> {
117+
impl private_methods<K, V: copy> for hashmap_<K, V> {
115118
fn search_rem(k: K, h: uint, idx: uint,
116119
e_root: @entry<K,V>) -> search_result<K,V> {
117120
let mut e0 = e_root;
@@ -285,6 +288,33 @@ mod chained {
285288
fn each_value(blk: fn(V) -> bool) { self.each(|_k, v| blk(v)) }
286289
}
287290

291+
impl hashmap<K: to_str, V: to_str copy> of to_str for hashmap_<K, V> {
292+
fn to_writer(wr: io::writer) {
293+
if self.count == 0u {
294+
wr.write_str("{}");
295+
ret;
296+
}
297+
298+
wr.write_str("{ ");
299+
let mut first = true;
300+
for self.each_entry |entry| {
301+
if !first {
302+
wr.write_str(", ");
303+
}
304+
first = false;
305+
wr.write_str(entry.key.to_str());
306+
wr.write_str(": ");
307+
wr.write_str((copy entry.value).to_str());
308+
};
309+
wr.write_str(" }");
310+
}
311+
312+
fn to_str() -> ~str {
313+
do io::with_str_writer |wr| { self.to_writer(wr) }
314+
}
315+
}
316+
317+
288318
fn chains<K,V>(nchains: uint) -> ~[mut chain<K,V>] {
289319
ret vec::to_mut(vec::from_elem(nchains, absent));
290320
}

0 commit comments

Comments
 (0)