Skip to content

Commit c50bbd8

Browse files
Merge pull request #349 from github/fix-arena-panic
Fix arena clear methods
2 parents fa506ac + 3696992 commit c50bbd8

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

stack-graphs/src/arena.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,9 @@ impl<T> Arena<T> {
183183

184184
/// Clear the arena, keeping underlying allocated capacity. After this, all previous handles into
185185
/// the arena are invalid.
186-
#[cfg_attr(not(feature = "storage"), allow(dead_code))]
187186
#[inline(always)]
188-
pub(crate) fn clear(&mut self) {
189-
self.items.clear();
187+
pub fn clear(&mut self) {
188+
self.items.truncate(1);
190189
}
191190

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

291290
/// Clear the supplemantal arena, keeping underlying allocated capacity. After this,
292291
/// all previous handles into the arena are invalid.
293-
#[cfg_attr(not(feature = "storage"), allow(dead_code))]
294292
#[inline(always)]
295-
pub(crate) fn clear(&mut self) {
296-
self.items.clear();
293+
pub fn clear(&mut self) {
294+
self.items.truncate(1);
297295
}
298296

299297
/// Creates a new, empty supplemental arena, preallocating enough space to store supplemental

stack-graphs/tests/it/arena.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,28 @@ fn can_compare_deques() {
229229
deque10.ensure_backwards(&mut arena);
230230
assert_eq!(deque1.cmp(&mut arena, deque10), Ordering::Less);
231231
}
232+
233+
#[test]
234+
fn can_use_arena_after_clear() {
235+
let mut a = Arena::new();
236+
let h = a.add(12 as u8);
237+
assert_eq!(12, *a.get(h));
238+
239+
a.clear();
240+
let h = a.add(7);
241+
assert_eq!(7, *a.get(h));
242+
}
243+
244+
#[test]
245+
fn can_use_supplemental_arena_after_clear() {
246+
let mut a = Arena::new();
247+
let h = a.add(());
248+
249+
let mut x = SupplementalArena::new();
250+
x[h] = 12;
251+
assert_eq!(Some(12), x.get(h).cloned());
252+
253+
x.clear();
254+
x[h] = 7;
255+
assert_eq!(Some(7), x.get(h).cloned());
256+
}

0 commit comments

Comments
 (0)