Skip to content

Commit

Permalink
Add method to get vacant key without mutable access (#114)
Browse files Browse the repository at this point in the history
Same as `slab.vacant_entry().key()` without requiring mutable access.

Co-authored-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
binier and hawkw authored Mar 23, 2022
1 parent c512385 commit 711e24a
Showing 1 changed file with 24 additions and 0 deletions.
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

0 comments on commit 711e24a

Please sign in to comment.