Skip to content

Commit

Permalink
Fix ContiguousArena::remove
Browse files Browse the repository at this point in the history
  • Loading branch information
SludgePhD authored and sebcrozet committed Jun 24, 2024
1 parent 2de9ad0 commit fe98eaf
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/object/contiguous_arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ impl<Idx, T> ContiguousArena<Idx, T> {
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)
Expand Down Expand Up @@ -161,3 +164,22 @@ impl<Idx, T> AsMut<[T]> for ContiguousArena<Idx, T> {
&mut self.objects
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn smoke() {
let mut arena: ContiguousArena<Index, _> = 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);
}
}

0 comments on commit fe98eaf

Please sign in to comment.