use core::cell::UnsafeCell;
use core::ptr;
use allocator_api2::alloc::Allocator;
use super::chunk_mutator::ChunkMutator;
#[repr(transparent)]
pub(crate) struct CurrentChunk<A: Allocator + Clone>(UnsafeCell<ChunkMutator<A>>);
impl<A: Allocator + Clone> CurrentChunk<A> {
#[inline]
pub(crate) const fn new(mutator: ChunkMutator<A>) -> Self {
Self(UnsafeCell::new(mutator))
}
#[expect(clippy::inline_always, reason = "hot-path entry; must inline fully for arena performance")]
#[inline(always)]
pub(crate) fn borrow(&self) -> &ChunkMutator<A> {
unsafe { &*self.0.get() }
}
#[inline]
pub(crate) fn replace(&self, new: ChunkMutator<A>) -> ChunkMutator<A> {
unsafe {
let slot = self.0.get();
let prev = ptr::read(slot);
ptr::write(slot, new);
prev
}
}
#[inline]
#[cfg_attr(test, mutants::skip)] pub(crate) fn drop_replace(&self, new: ChunkMutator<A>) {
let _old = self.replace(new);
}
#[inline]
pub(crate) fn get_mut(&mut self) -> &mut ChunkMutator<A> {
self.0.get_mut()
}
}