Skip to content

Commit

Permalink
#104 remove Clone requirement from Map::remove_entry, drop test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zefick committed Apr 30, 2023
1 parent 97b3033 commit 906a329
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

use crate::Map;
use std::borrow::Borrow;
use std::mem;
use std::mem::MaybeUninit;

impl<K: PartialEq, V, const N: usize> Map<K, V, N> {
/// Get its total capacity.
Expand Down Expand Up @@ -198,15 +200,15 @@ impl<K: PartialEq, V, const N: usize> Map<K, V, N> {
#[inline]
pub fn remove_entry<Q: PartialEq + ?Sized>(&mut self, k: &Q) -> Option<(K, V)>
where
K: Borrow<Q> + Clone,
V: Clone,
K: Borrow<Q>,
{
for i in 0..self.next {
if let Some(p) = self.item(i) {
if p.0.borrow() == k {
let ret = Some(p.clone());
self.pairs[i].write(None);
return ret;
let ret = mem::replace(&mut self.pairs[i], MaybeUninit::new(None));
unsafe {
return ret.assume_init();
}
}
}
}
Expand Down Expand Up @@ -399,3 +401,14 @@ fn remove_entry_absent() {
m.insert("one", 42);
assert_eq!(m.remove_entry("two"), None);
}

#[test]
fn drop_removed_entry() {
use std::rc::Rc;
let mut m: Map<(), Rc<()>, 8> = Map::new();
let v = Rc::new(());
m.insert((), Rc::clone(&v));
assert_eq!(Rc::strong_count(&v), 2);
m.remove_entry(&());
assert_eq!(Rc::strong_count(&v), 1);
}

0 comments on commit 906a329

Please sign in to comment.