Skip to content

Add a try_remove method #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 7, 2021
Merged
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
39 changes: 30 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,15 +945,12 @@ impl<T> Slab<T> {
}
}

/// Remove and return the value associated with the given key.
/// Tries to remove the value associated with the given key,
/// returning the value if the key existed.
///
/// The key is then released and may be associated with future stored
/// values.
///
/// # Panics
///
/// Panics if `key` is not associated with a value.
///
/// # Examples
///
/// ```
Expand All @@ -962,10 +959,10 @@ impl<T> Slab<T> {
///
/// let hello = slab.insert("hello");
///
/// assert_eq!(slab.remove(hello), "hello");
/// assert_eq!(slab.try_remove(hello), Some("hello"));
/// assert!(!slab.contains(hello));
/// ```
pub fn remove(&mut self, key: usize) -> T {
pub fn try_remove(&mut self, key: usize) -> Option<T> {
if let Some(entry) = self.entries.get_mut(key) {
// Swap the entry at the provided value
let prev = mem::replace(entry, Entry::Vacant(self.next));
Expand All @@ -974,15 +971,39 @@ impl<T> Slab<T> {
Entry::Occupied(val) => {
self.len -= 1;
self.next = key;
return val;
return val.into();
}
_ => {
// Woops, the entry is actually vacant, restore the state
*entry = prev;
}
}
}
panic!("invalid key");
None
}

/// Remove and return the value associated with the given key.
///
/// The key is then released and may be associated with future stored
/// values.
///
/// # Panics
///
/// Panics if `key` is not associated with a value.
///
/// # Examples
///
/// ```
/// # use slab::*;
/// let mut slab = Slab::new();
///
/// let hello = slab.insert("hello");
///
/// assert_eq!(slab.remove(hello), "hello");
/// assert!(!slab.contains(hello));
/// ```
pub fn remove(&mut self, key: usize) -> T {
self.try_remove(key).expect("invalid key")
}

/// Return `true` if a value is associated with the given key.
Expand Down
11 changes: 11 additions & 0 deletions tests/slab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,14 @@ fn drain_rev() {
let vals: Vec<u64> = slab.drain().rev().collect();
assert_eq!(vals, (0..9).rev().collect::<Vec<u64>>());
}

#[test]
fn try_remove() {
let mut slab = Slab::new();

let key = slab.insert(1);

assert_eq!(slab.try_remove(key), Some(1));
assert_eq!(slab.try_remove(key), None);
assert_eq!(slab.get(key), None);
}