Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make HashMap::take not corrupt the map. Fixes #19292 #19301

Merged
merged 1 commit into from
Nov 27, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/libstd/collections/hash/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {

/// Takes the value out of the entry, and returns it
pub fn take(self) -> V {
let (_, _, v) = self.elem.take();
let (_, v) = pop_internal(self.elem);
v
}
}
Expand Down Expand Up @@ -1433,6 +1433,7 @@ mod test_map {
use hash;
use iter::{Iterator,range_inclusive,range_step_inclusive};
use cell::RefCell;
use rand::{weak_rng, Rng};

struct KindaIntLike(int);

Expand Down Expand Up @@ -2062,4 +2063,37 @@ mod test_map {
assert_eq!(map.get(&10).unwrap(), &1000);
assert_eq!(map.len(), 6);
}

#[test]
fn test_entry_take_doesnt_corrupt() {
// Test for #19292
fn check(m: &HashMap<int, ()>) {
for k in m.keys() {
assert!(m.contains_key(k),
"{} is in keys() but not in the map?", k);
}
}

let mut m = HashMap::new();
let mut rng = weak_rng();

// Populate the map with some items.
for _ in range(0u, 50) {
let x = rng.gen_range(-10, 10);
m.insert(x, ());
}

for i in range(0u, 1000) {
let x = rng.gen_range(-10, 10);
match m.entry(x) {
Vacant(_) => {},
Occupied(e) => {
println!("{}: remove {}", i, x);
e.take();
},
}

check(&m);
}
}
}