Skip to content

Commit d01c7d0

Browse files
committed
Fix a bug with HashMap::consume
1 parent aed53f9 commit d01c7d0

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/libstd/hashmap.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,12 @@ pub fn linear_map_with_capacity<K:Eq + Hash,V>(
7272
fn linear_map_with_capacity_and_keys<K:Eq + Hash,V>(
7373
k0: u64, k1: u64,
7474
initial_capacity: uint) -> HashMap<K, V> {
75+
let cap = uint::max(INITIAL_CAPACITY, initial_capacity);
7576
HashMap {
7677
k0: k0, k1: k1,
77-
resize_at: resize_at(initial_capacity),
78+
resize_at: resize_at(cap),
7879
size: 0,
79-
buckets: vec::from_fn(initial_capacity, |_| None)
80+
buckets: vec::from_fn(cap, |_| None)
8081
}
8182
}
8283

@@ -480,7 +481,8 @@ pub impl<K: Hash + Eq, V> HashMap<K, V> {
480481
}
481482

482483
fn consume(&mut self, f: &fn(K, V)) {
483-
let buckets = replace(&mut self.buckets, ~[]);
484+
let buckets = replace(&mut self.buckets,
485+
vec::from_fn(INITIAL_CAPACITY, |_| None));
484486
self.size = 0;
485487

486488
do vec::consume(buckets) |_, bucket| {
@@ -664,6 +666,12 @@ mod test_map {
664666
use super::*;
665667
use uint;
666668

669+
#[test]
670+
fn test_create_capacity_zero() {
671+
let mut m = HashMap::with_capacity(0);
672+
assert!(m.insert(1, 1));
673+
}
674+
667675
#[test]
668676
fn test_insert() {
669677
let mut m = HashMap::new();
@@ -771,6 +779,14 @@ mod test_map {
771779
assert_eq!(m2.get(&2), &3);
772780
}
773781

782+
#[test]
783+
fn test_consume_still_usable() {
784+
let mut m = HashMap::new();
785+
assert!(m.insert(1, 2));
786+
do m.consume |_, _| {}
787+
assert!(m.insert(1, 2));
788+
}
789+
774790
#[test]
775791
fn test_iterate() {
776792
let mut m = linear_map_with_capacity(4);

0 commit comments

Comments
 (0)