#[cfg(not(feature = "std"))]
use core::fmt;
#[cfg(feature = "std")]
use std::sync::OnceLock;
#[cfg(not(feature = "std"))]
use once_cell::race::OnceBox;
#[cfg(not(feature = "std"))]
pub struct OnceLockCompat<T> {
inner: OnceBox<T>,
}
#[cfg(not(feature = "std"))]
impl<T> OnceLockCompat<T> {
pub const fn new() -> Self {
Self { inner: OnceBox::new() }
}
pub fn get_or_init<F>(&self, f: F) -> &T
where
F: FnOnce() -> T,
{
self.inner.get_or_init(|| alloc::boxed::Box::new(f()))
}
pub fn take(&mut self) -> Option<T> {
self.inner = OnceBox::new();
None
}
}
#[cfg(not(feature = "std"))]
impl<T> Default for OnceLockCompat<T> {
fn default() -> Self {
Self::new()
}
}
#[cfg(not(feature = "std"))]
impl<T> Clone for OnceLockCompat<T> {
fn clone(&self) -> Self {
Self::new()
}
}
#[cfg(not(feature = "std"))]
impl<T: fmt::Debug> fmt::Debug for OnceLockCompat<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
}
}
#[cfg(feature = "std")]
pub type OnceLockCompat<T> = OnceLock<T>;