Skip to content

Commit 0423878

Browse files
committed
Added remove api
1 parent 618cd43 commit 0423878

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/lib.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,15 +398,14 @@ impl<'a, K, V> CowHashMap<'a, K, V>
398398

399399
/// An iterator visiting all key-value pairs in arbitrary order.
400400
///
401+
/// ## Example
401402
/// ```rust
402403
/// # #[macro_use] extern crate hashcow; fn main() {
403404
/// # use std::collections::HashSet;
404405
/// use hashcow::CowHashMap;
405406
///
406407
/// let mut hm: CowHashMap<str, [u8]> = CowHashMap::new();
407-
/// let v1 = &[1, 2, 3];
408-
/// let v2 = &[4, 5, 6];
409-
/// hm.insert_borrowed("key1", v1);
408+
/// hm.insert_borrowed("key1", &[1, 2, 3]);
410409
/// hm.insert_owned("key2".to_owned(), vec![4, 5, 6]);
411410
///
412411
/// for (key, val) in hm.iter() {
@@ -418,6 +417,27 @@ impl<'a, K, V> CowHashMap<'a, K, V>
418417
pub fn iter(&self) -> impl Iterator<Item = (&K, &V)> {
419418
self.inner.iter().map(|(k, v)| (k.borrow(), v.borrow()))
420419
}
420+
421+
/// Removes a key from the map, returning the value at the key if the key was previously in the map.
422+
///
423+
/// ## Example
424+
/// ```rust
425+
/// use hashcow::CowHashMap;
426+
///
427+
/// let mut hm: CowHashMap<str, [u8]> = CowHashMap::new();
428+
/// assert!(hm.remove(&"key1").is_none());
429+
///
430+
/// hm.insert_borrowed("key1", &[1, 2, 3]);
431+
/// assert_eq!(hm.remove(&"key1").unwrap(), vec![1, 2, 3]);
432+
/// ```
433+
pub fn remove(&mut self, key: &K) -> Option<<V as ToOwned>::Owned> {
434+
let val = self.inner.remove(key)?;
435+
436+
match val {
437+
Cow::Borrowed(val) => Some(val.to_owned()),
438+
Cow::Owned(val) => Some(val),
439+
}
440+
}
421441
}
422442

423443
#[macro_use]

0 commit comments

Comments
 (0)