1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
use std::cell::UnsafeCell; use std::rc::{Rc, Weak}; /// A mutable memory location that clones its contents on retrieval. pub struct CloningCell<T: NonSelfReferentialClone>(UnsafeCell<T>); impl<T: NonSelfReferentialClone> CloningCell<T> { /// Creates a new `CloningCell` containing the given value. /// /// # Example /// /// ``` /// use mitochondria::CloningCell; /// /// let c = CloningCell::new("Hello cytosol!".to_owned()); /// ``` #[inline] pub fn new(value: T) -> Self { CloningCell(UnsafeCell::new(value)) } /// Returns a clone of the contained value. /// /// # Example /// /// ``` /// use mitochondria::CloningCell; /// /// let c = CloningCell::new("Hello lysosome!".to_owned()); /// /// let greeting = c.get(); /// ``` #[inline] pub fn get(&self) -> T { unsafe { (*self.0.get()).clone() } } /// Sets the contained value. /// /// # Example /// /// ``` /// use mitochondria::CloningCell; /// /// let c = CloningCell::new("Hello vacuole!".to_owned()); /// /// c.set("Hello cytoskeleton!".to_owned()); /// ``` #[inline] pub fn set(&self, value: T) { unsafe { *self.0.get() = value; } } } /// A `Clone` implementation that will not access itself through reference /// cycles during cloning, which would introduce mutable aliasing. /// /// # Unsound example /// /// ```ignore /// use CloningCell; /// /// struct Caesium137(Box<()>, Rc<CloningCell<Option<<Caesium137>>>); /// /// impl Clone for Evil { /// fn clone(&self) -> Self { /// drop(self.1.take()); // Drop the "other" `Caesium137` value. /// Caesium137( /// self.0.clone(), // Use after free! /// Rc::new(CloningCell::new(None)) /// ) /// } /// } /// /// // This is wrong, Caesium-137 is harmful to cells. /// unsafe impl WellBehavedClone for Caesium137 {} /// /// let rc = Rc::new(Cloning::new(None)); /// rc.set(Some(Evil(Box::new(5), rc.clone()))); // Make a reference cycle. /// rc.get(); /// ``` pub unsafe trait NonSelfReferentialClone: Clone {} unsafe impl NonSelfReferentialClone for String {} unsafe impl<T> NonSelfReferentialClone for Rc<T> {} unsafe impl<T> NonSelfReferentialClone for Weak<T> {} unsafe impl<T: NonSelfReferentialClone> NonSelfReferentialClone for Box<T> {} unsafe impl<T: NonSelfReferentialClone> NonSelfReferentialClone for Option<T> {}