Skip to content

Commit

Permalink
Merge pull request #349 from github/fix-arena-panic
Browse files Browse the repository at this point in the history
Fix arena clear methods
  • Loading branch information
hendrikvanantwerpen authored Nov 14, 2023
2 parents fa506ac + 3696992 commit c50bbd8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
10 changes: 4 additions & 6 deletions stack-graphs/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,9 @@ impl<T> Arena<T> {

/// 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) {
self.items.clear();
pub fn clear(&mut self) {
self.items.truncate(1);
}

/// Adds a new instance to this arena, returning a stable handle to it.
Expand Down Expand Up @@ -290,10 +289,9 @@ impl<H, T> SupplementalArena<H, T> {

/// 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) {
self.items.clear();
pub fn clear(&mut self) {
self.items.truncate(1);
}

/// Creates a new, empty supplemental arena, preallocating enough space to store supplemental
Expand Down
25 changes: 25 additions & 0 deletions stack-graphs/tests/it/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

0 comments on commit c50bbd8

Please sign in to comment.