use alloc::alloc::Layout;
use core::ptr::{self, NonNull};
mod sealed {
pub trait Sealed {}
}
pub trait Allocator: sealed::Sealed {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, ()> {
assert!(layout.size() != 0);
NonNull::new(ptr::slice_from_raw_parts_mut(
unsafe { alloc::alloc::alloc(layout) },
layout.size(),
))
.ok_or(())
}
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
unsafe { alloc::alloc::dealloc(ptr.as_ptr(), layout) };
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Global;
impl sealed::Sealed for Global {}
impl Allocator for Global {}