From fe98eaf422b1de8345db32ee4029e74c074ec1a8 Mon Sep 17 00:00:00 2001 From: Sludge <96552222+SludgePhD@users.noreply.github.com> Date: Tue, 20 Sep 2022 01:39:20 +0200 Subject: [PATCH] Fix `ContiguousArena::remove` --- src/object/contiguous_arena.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/object/contiguous_arena.rs b/src/object/contiguous_arena.rs index 58cbdf1..e7f664f 100644 --- a/src/object/contiguous_arena.rs +++ b/src/object/contiguous_arena.rs @@ -125,7 +125,10 @@ impl ContiguousArena { let _ = self.rev_indices.swap_remove(i); if let Some(rev_id) = swapped_rev_id { - self.indices[rev_id] = i; + if i != self.objects.len() { + // fixup the swapped index + self.indices[rev_id] = i; + } } Some(deleted_object) @@ -161,3 +164,22 @@ impl AsMut<[T]> for ContiguousArena { &mut self.objects } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn smoke() { + let mut arena: ContiguousArena = ContiguousArena::new(); + let i0 = arena.insert(123); + let i1 = arena.insert(456); + let i2 = arena.insert(789); + assert_eq!(arena.remove(i0), Some(123)); + assert_eq!(arena.remove(i0), None); + assert_eq!(arena.remove(i1), Some(456)); + assert_eq!(arena.remove(i1), None); + assert_eq!(arena.remove(i2), Some(789)); + assert_eq!(arena.remove(i2), None); + } +}