From 02c5d60f825cb36b4154baa7ee4f854e8016dbc1 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Sun, 22 Oct 2023 00:20:48 +0800 Subject: [PATCH] fix: take currently free slots into account --- src/arena.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/arena.rs b/src/arena.rs index 92e7fea..24991c1 100644 --- a/src/arena.rs +++ b/src/arena.rs @@ -167,9 +167,10 @@ impl Arena { } /// Reserve capacity for at least `additional` more elements to be inserted - #[inline(always)] pub fn reserve(&mut self, additional: usize) { - self.storage.reserve(additional) + let currently_free = self.storage.len().saturating_sub(self.len as usize); + let to_reserve = additional.saturating_sub(currently_free); + self.storage.reserve(to_reserve); } /// Returns whether the arena is empty.