Skip to content
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

Add method to get vacant key without mutable access #114

Merged
merged 2 commits into from
Mar 23, 2022
Merged
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
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,30 @@ impl<T> Slab<T> {
key
}

/// Returns the key of the next vacant entry.
///
/// This function returns the key of the vacant entry which will be used
/// for the next insertion. This is equivalent to
/// `slab.vacant_entry().key()`, but it doesn't require mutable access.
///
/// # Examples
///
/// ```
/// # use slab::*;
/// let mut slab = Slab::new();
/// assert_eq!(slab.vacant_key(), 0);
///
/// slab.insert(0);
/// assert_eq!(slab.vacant_key(), 1);
///
/// slab.insert(1);
/// slab.remove(0);
/// assert_eq!(slab.vacant_key(), 0);
/// ```
pub fn vacant_key(&self) -> usize {
self.next
}

/// Return a handle to a vacant entry allowing for further manipulation.
///
/// This function is useful when creating values that must contain their
Expand Down