Skip to content

Commit

Permalink
std::trie: Fix find_mut for non-present keys
Browse files Browse the repository at this point in the history
Make TrieMap/TrieSet's find_mut check the key for external nodes.
Without this find_mut sometimes returns a reference to another key when
querying for a non-present key.
  • Loading branch information
jix committed Nov 24, 2013
1 parent b3ff24a commit 525878f
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/libstd/trie.rs
Expand Up @@ -373,7 +373,8 @@ fn chunk(n: uint, idx: uint) -> uint {

fn find_mut<'r, T>(child: &'r mut Child<T>, key: uint, idx: uint) -> Option<&'r mut T> {
match *child {
External(_, ref mut value) => Some(value),
External(stored, ref mut value) if stored == key => Some(value),
External(*) => None,
Internal(ref mut x) => find_mut(&mut x.children[chunk(key, idx)], key, idx + 1),
Nothing => None
}
Expand Down Expand Up @@ -536,6 +537,16 @@ mod test_map {
assert_eq!(m.find(&5), Some(&new));
}

#[test]
fn test_find_mut_missing() {
let mut m = TrieMap::new();
assert!(m.find_mut(&0).is_none());
assert!(m.insert(1, 12));
assert!(m.find_mut(&0).is_none());
assert!(m.insert(2, 8));
assert!(m.find_mut(&0).is_none());
}

#[test]
fn test_step() {
let mut trie = TrieMap::new();
Expand Down

9 comments on commit 525878f

@bors
Copy link
Contributor

@bors bors commented on 525878f Nov 24, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from thestinger
at jix@525878f

@bors
Copy link
Contributor

@bors bors commented on 525878f Nov 24, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging jix/rust/fix_find_mut_in_trie = 525878f into auto

@bors
Copy link
Contributor

@bors bors commented on 525878f Nov 24, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jix/rust/fix_find_mut_in_trie = 525878f merged ok, testing candidate = 305bd243

@bors
Copy link
Contributor

@bors bors commented on 525878f Nov 24, 2013

@bors
Copy link
Contributor

@bors bors commented on 525878f Nov 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from thestinger
at jix@525878f

@bors
Copy link
Contributor

@bors bors commented on 525878f Nov 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging jix/rust/fix_find_mut_in_trie = 525878f into auto

@bors
Copy link
Contributor

@bors bors commented on 525878f Nov 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jix/rust/fix_find_mut_in_trie = 525878f merged ok, testing candidate = ca32743

@bors
Copy link
Contributor

@bors bors commented on 525878f Nov 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 525878f Nov 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = ca32743

Please sign in to comment.