use crate::allocator::{AllocError, Allocator};
use alloc::alloc::Layout;
use core::mem::ManuallyDrop;
use core::ops::Deref;
use core::ptr::NonNull;
pub struct PersistentAlloc<A>(ManuallyDrop<A>);
impl<A: Allocator> PersistentAlloc<A> {
pub fn new(alloc: A) -> Self
where
A: 'static,
{
Self(ManuallyDrop::new(alloc))
}
pub unsafe fn drop(&mut self) {
unsafe {
ManuallyDrop::drop(&mut self.0);
}
}
}
impl<A> Deref for PersistentAlloc<A> {
type Target = A;
fn deref(&self) -> &Self::Target {
&self.0
}
}
unsafe impl<A: Allocator> Allocator for PersistentAlloc<A> {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
self.0.allocate(layout)
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe {
self.0.deallocate(ptr, layout);
}
}
}