From 2edda57c37f1ba2f89f9e3a98d01feea598085c8 Mon Sep 17 00:00:00 2001 From: Hendrik van Antwerpen Date: Tue, 14 Nov 2023 11:27:08 +0100 Subject: [PATCH 1/2] Fix arena clear methods --- stack-graphs/src/arena.rs | 8 ++++---- stack-graphs/tests/it/arena.rs | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/stack-graphs/src/arena.rs b/stack-graphs/src/arena.rs index 6e4747267..18009db02 100644 --- a/stack-graphs/src/arena.rs +++ b/stack-graphs/src/arena.rs @@ -183,10 +183,10 @@ impl Arena { /// Clear the arena, keeping underlying allocated capacity. After this, all previous handles into /// the arena are invalid. - #[cfg_attr(not(feature = "storage"), allow(dead_code))] #[inline(always)] - pub(crate) fn clear(&mut self) { + pub fn clear(&mut self) { self.items.clear(); + self.items.push(MaybeUninit::uninit()); } /// Adds a new instance to this arena, returning a stable handle to it. @@ -290,10 +290,10 @@ impl SupplementalArena { /// Clear the supplemantal arena, keeping underlying allocated capacity. After this, /// all previous handles into the arena are invalid. - #[cfg_attr(not(feature = "storage"), allow(dead_code))] #[inline(always)] - pub(crate) fn clear(&mut self) { + pub fn clear(&mut self) { self.items.clear(); + self.items.push(MaybeUninit::uninit()); } /// Creates a new, empty supplemental arena, preallocating enough space to store supplemental diff --git a/stack-graphs/tests/it/arena.rs b/stack-graphs/tests/it/arena.rs index 5af25d66e..6963dae29 100644 --- a/stack-graphs/tests/it/arena.rs +++ b/stack-graphs/tests/it/arena.rs @@ -229,3 +229,28 @@ fn can_compare_deques() { deque10.ensure_backwards(&mut arena); assert_eq!(deque1.cmp(&mut arena, deque10), Ordering::Less); } + +#[test] +fn can_use_arena_after_clear() { + let mut a = Arena::new(); + let h = a.add(12 as u8); + assert_eq!(12, *a.get(h)); + + a.clear(); + let h = a.add(7); + assert_eq!(7, *a.get(h)); +} + +#[test] +fn can_use_supplemental_arena_after_clear() { + let mut a = Arena::new(); + let h = a.add(()); + + let mut x = SupplementalArena::new(); + x[h] = 12; + assert_eq!(Some(12), x.get(h).cloned()); + + x.clear(); + x[h] = 7; + assert_eq!(Some(7), x.get(h).cloned()); +} From 3696992ed06c7a40ca4eff849eda7b290dbf8950 Mon Sep 17 00:00:00 2001 From: Hendrik van Antwerpen Date: Tue, 14 Nov 2023 17:29:36 +0100 Subject: [PATCH 2/2] Use truncate instead --- stack-graphs/src/arena.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/stack-graphs/src/arena.rs b/stack-graphs/src/arena.rs index 18009db02..134a90d14 100644 --- a/stack-graphs/src/arena.rs +++ b/stack-graphs/src/arena.rs @@ -185,8 +185,7 @@ impl Arena { /// the arena are invalid. #[inline(always)] pub fn clear(&mut self) { - self.items.clear(); - self.items.push(MaybeUninit::uninit()); + self.items.truncate(1); } /// Adds a new instance to this arena, returning a stable handle to it. @@ -292,8 +291,7 @@ impl SupplementalArena { /// all previous handles into the arena are invalid. #[inline(always)] pub fn clear(&mut self) { - self.items.clear(); - self.items.push(MaybeUninit::uninit()); + self.items.truncate(1); } /// Creates a new, empty supplemental arena, preallocating enough space to store supplemental