Skip to content
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,18 @@ mod test {
}
}

#[test]
fn test_remove_mut_accessor() {
let map: ConcHashMap<i32, String> = Default::default();
map.insert(1, "one".to_string());
map.insert(2, "two".to_string());
map.insert(3, "three".to_string());
assert_eq!(Some("two".to_string()), map.find_mut(&2).unwrap().remove());
assert_eq!("one", map.find(&1).unwrap().get());
assert!(map.find(&2).is_none());
assert_eq!("three", map.find(&3).unwrap().get());
}

#[test]
fn test_from_iterator() {
let vec: Vec<(u32, u32)> = (0..100).map(|i| (i, i * i)).collect();
Expand Down
10 changes: 9 additions & 1 deletion src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl <'a, K, V> Accessor<'a, K, V> {
}
}

impl <'a, K, V> MutAccessor<'a, K, V> {
impl <'a, K: Hash + Eq, V> MutAccessor<'a, K, V> {
pub fn new(table: MutexGuard<'a, Table<K, V>>, idx: usize) -> MutAccessor<'a, K, V> {
MutAccessor {
table: table,
Expand All @@ -101,6 +101,10 @@ impl <'a, K, V> MutAccessor<'a, K, V> {
&mut *self.table.values.offset(self.idx as isize)
}
}

pub fn remove(&mut self) -> Option<V> {
self.table.remove_index(self.idx)
}
}

impl <K, V> Table<K, V> where K: Hash + Eq {
Expand Down Expand Up @@ -171,6 +175,10 @@ impl <K, V> Table<K, V> where K: Hash + Eq {
Some(i) => i,
None => return None
};
self.remove_index(i)
}

pub fn remove_index(&mut self, i: usize) -> Option<V> {
unsafe {
drop_in_place::<K>(self.keys.offset(i as isize));
*self.hashes.offset(i as isize) = TOMBSTONE;
Expand Down