From c043b35b73fef1fa22a07cc44251afe731b775b3 Mon Sep 17 00:00:00 2001 From: Phil Ruffwind Date: Sun, 14 May 2017 07:14:45 -0400 Subject: [PATCH] Add next_vacant next_vacant returns the index of the next available slot. This can be useful for inserting self-referential data. --- src/lib.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 9f701a0..df2a665 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -210,6 +210,32 @@ impl Arena { self.len == 0 } + /// Returns the index of the slot that next `insert` will use if no other + /// mutating calls take place in between. + /// + /// # Examples + /// + /// ``` + /// use vec_arena::Arena; + /// + /// let mut arena = Arena::new(); + /// + /// let a = arena.next_vacant(); + /// let b = arena.insert(1); + /// assert_eq!(a, b); + /// let c = arena.next_vacant(); + /// let d = arena.insert(2); + /// assert_eq!(c, d); + /// ``` + #[inline] + pub fn next_vacant(&mut self) -> usize { + if self.head == !0 { + self.len + } else { + self.head + } + } + /// Inserts an object into the arena and returns the slot index it was stored in. /// The arena will reallocate if it's full. ///