use crate::utils::linkedlist::NodeRef; use crate::utils::ptr_ext::{MutPtrExt, PtrExt}; use std::cell::UnsafeCell; use std::mem; use std::rc::Rc; pub struct CloneCell { data: UnsafeCell, } impl CloneCell { pub fn new(t: T) -> Self { Self { data: UnsafeCell::new(t), } } #[inline(always)] pub fn get(&self) -> T { 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, { unsafe { mem::take(self.data.get().deref_mut()) } } } 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 NodeRef {}