use { crate::ptr_ext::{MutPtrExt, PtrExt}, jay_config::{keyboard::mods::Modifiers, window::Window}, std::{ cell::UnsafeCell, fmt::{Debug, Formatter}, mem, rc::{Rc, Weak}, sync::Arc, }, }; pub struct CloneCell { data: UnsafeCell, } impl Clone for CloneCell { fn clone(&self) -> Self { Self { data: UnsafeCell::new(self.get()), } } } impl Debug for CloneCell { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { self.get().fmt(f) } } impl CloneCell { pub const fn new(t: T) -> Self { Self { data: UnsafeCell::new(t), } } #[inline(always)] pub fn get(&self) -> T where T: UnsafeCellCloneSafe, { unsafe { self.data.get().deref().clone() } } #[inline(always)] pub fn set(&self, t: T) -> T { unsafe { mem::replace(self.data.get().deref_mut(), t) } } #[inline(always)] pub fn take(&self) -> T where T: Default, { self.set(T::default()) } } impl CloneCell> { #[inline(always)] pub fn is_some(&self) -> bool { unsafe { self.data.get().deref().is_some() } } #[inline(always)] pub fn is_none(&self) -> bool { unsafe { self.data.get().deref().is_none() } } } impl Default for CloneCell { fn default() -> Self { Self::new(Default::default()) } } pub unsafe trait UnsafeCellCloneSafe: Clone {} unsafe impl UnsafeCellCloneSafe for Option {} unsafe impl UnsafeCellCloneSafe for Rc {} unsafe impl UnsafeCellCloneSafe for Weak {} unsafe impl UnsafeCellCloneSafe for Arc {} unsafe impl UnsafeCellCloneSafe for () {} unsafe impl UnsafeCellCloneSafe for u64 {} unsafe impl UnsafeCellCloneSafe for i32 {} unsafe impl UnsafeCellCloneSafe for u32 {} unsafe impl UnsafeCellCloneSafe for usize {} unsafe impl UnsafeCellCloneSafe for (A, B) {} unsafe impl UnsafeCellCloneSafe for Modifiers {} unsafe impl UnsafeCellCloneSafe for Window {} unsafe impl UnsafeCellCloneSafe for crate::linkedlist::NodeRef {}