Skip to content

Commit

Permalink
Return the freed element in arena
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Feb 12, 2024
1 parent 9e52fd0 commit d0f9f23
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions crates/h10/src/arena.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt;
use std::hash::Hash;
use std::marker::PhantomData;
use std::mem::replace;

#[derive(Debug)]
pub struct Arena<T> {
Expand Down Expand Up @@ -78,7 +79,7 @@ impl<T> Arena<T> {
match self.free.take() {
Some(idx) => {
let free_decl =
std::mem::replace(&mut self.allocs[idx.as_usize()], Allocation::Used { elem });
replace(&mut self.allocs[idx.as_usize()], Allocation::Used { elem });
match free_decl {
Allocation::Free { next_free_slot } => {
self.free = next_free_slot;
Expand All @@ -97,11 +98,18 @@ impl<T> Arena<T> {
}
}

pub fn free(&mut self, idx: Idx<T>) {
self.allocs[idx.as_usize()] = Allocation::Free {
next_free_slot: self.free,
};
pub fn free(&mut self, idx: Idx<T>) -> T {
let elem = replace(
&mut self.allocs[idx.as_usize()],
Allocation::Free {
next_free_slot: self.free,
},
);
self.free = Some(idx);
match elem {
Allocation::Free { .. } => panic!("Freed an already free slot"),
Allocation::Used { elem } => elem,
}
}

pub fn get(&self, idx: Idx<T>) -> &T {
Expand Down

0 comments on commit d0f9f23

Please sign in to comment.