use alloc::boxed::Box;
use core::alloc::Allocator;
use core::marker::PhantomData;
use core::ops::{Deref, DerefMut};
use core::ptr::null_mut;
use core::sync::atomic::AtomicPtr;
use core::sync::atomic::Ordering::{AcqRel, Acquire};
pub struct LazyBox<T: LazyInit> {
ptr: AtomicPtr<T>,
allocator: T::Allocator,
_phantom: PhantomData<T>,
}
pub trait LazyInit {
type Allocator: Allocator + Clone;
fn init(allocator: Self::Allocator) -> Box<Self, Self::Allocator>;
fn cancel_init(x: Box<Self, Self::Allocator>) {
Self::destroy(x);
}
fn destroy(_: Box<Self, Self::Allocator>) { }
}
#[allow(dead_code)]
impl<T: LazyInit> LazyBox<T> {
pub const fn new_in(allocator: T::Allocator) -> Self {
Self { ptr: AtomicPtr::new(null_mut()), allocator, _phantom: PhantomData }
}
pub const fn allocator(lb: &LazyBox<T>) -> &T::Allocator { &lb.allocator }
fn get_pointer(&self) -> *mut T {
let ptr = self.ptr.load(Acquire);
if ptr.is_null() { self.initialize() } else { ptr }
}
fn initialize(&self) -> *mut T {
let new_ptr = Box::into_raw_with_allocator(T::init(self.allocator.clone())).0;
match self.ptr.compare_exchange(null_mut(), new_ptr, AcqRel, Acquire) {
Ok(_) => new_ptr,
Err(ptr) => {
T::cancel_init(unsafe { Box::from_raw_in(new_ptr, self.allocator.clone()) });
ptr
}
}
}
}
impl<T: LazyInit> Deref for LazyBox<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.get_pointer() }
}
}
impl<T: LazyInit> DerefMut for LazyBox<T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.get_pointer() }
}
}
impl<T: LazyInit> Drop for LazyBox<T> {
fn drop(&mut self) {
let ptr = *self.ptr.get_mut();
if !ptr.is_null() {
T::destroy(unsafe { Box::from_raw_in(ptr, self.allocator.clone()) });
}
}
}