diff --git a/src/map.rs b/src/map.rs index 5116815..4121a99 100644 --- a/src/map.rs +++ b/src/map.rs @@ -521,6 +521,18 @@ mod test { } } + #[test] + fn test_remove_mut_accessor() { + let map: ConcHashMap = 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(); diff --git a/src/table.rs b/src/table.rs index bf79392..b51fcf4 100644 --- a/src/table.rs +++ b/src/table.rs @@ -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>, idx: usize) -> MutAccessor<'a, K, V> { MutAccessor { table: table, @@ -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 { + self.table.remove_index(self.idx) + } } impl Table where K: Hash + Eq { @@ -171,6 +175,10 @@ impl Table where K: Hash + Eq { Some(i) => i, None => return None }; + self.remove_index(i) + } + + pub fn remove_index(&mut self, i: usize) -> Option { unsafe { drop_in_place::(self.keys.offset(i as isize)); *self.hashes.offset(i as isize) = TOMBSTONE;