use core::{mem::MaybeUninit, ops::Deref}; pub struct LazyConst { value: T, #[cfg(debug_assertions)] state: u8, } impl Deref for LazyConst { type Target = T; fn deref(&self) -> &Self::Target { #[cfg(debug_assertions)] if self.state == 0 { panic!("Lazy const for {} not assigned", core::any::type_name::()); } &self.value } } impl LazyConst { pub const fn new() -> Self { unsafe { Self { value: MaybeUninit::zeroed().assume_init(), #[cfg(debug_assertions)] state: 0, } } } pub unsafe fn init(&self, value: T) { as_mut(self).value = value; #[cfg(debug_assertions)] { as_mut(self).state = 1; } } } pub unsafe fn as_mut(x: &T) -> &mut T { #[allow(invalid_reference_casting)] &mut *((x as *const T) as *mut T) }