pub unsafe trait CloneFromCell: Clone { }
cell_get_cloned #145329)
Types for which cloning Cell<Self> is sound.
Implementing this trait for a type is sound if and only if the following code is sound for T = that type.
#![feature(cell_get_cloned)]
fn clone_from_cell<T: CloneFromCell>(cell: &Cell<T>) -> T {
unsafe { T::clone(&*cell.as_ptr()) }
}Importantly, you can’t just implement CloneFromCell for any arbitrary Copy type, e.g. the following is unsound:
#![feature(cell_get_cloned)]
#[derive(Copy, Debug)]
pub struct Bad<'a>(Option<&'a Cell<Bad<'a>>>, u8);
impl Clone for Bad<'_> {
fn clone(&self) -> Self {
let a: &u8 = &self.1;
// when self.0 points to self, we write to self.1 while we have a live `&u8` pointing to
// it -- this is UB
self.0.unwrap().set(Self(None, 1));
dbg!((a, self));
Self(None, 0)
}
}
// this is not sound
// unsafe impl CloneFromCell for Bad<'_> {}This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
impl<T> CloneFromCell for Option<T>where
T: CloneFromCell,impl<T> CloneFromCell for (T₁, T₂, …, Tₙ)where
T: CloneFromCell,This trait is implemented for tuples up to twelve items long.
impl<T> CloneFromCell for PhantomData<T>where
T: ?Sized,impl<T> CloneFromCell for ManuallyDrop<T>where
T: CloneFromCell,impl<T> CloneFromCell for std::ops::Range<T>where
T: CloneFromCell,impl<T> CloneFromCell for std::range::Range<T>where
T: CloneFromCell,impl<T> CloneFromCell for Rc<T>where
T: ?Sized,impl<T> CloneFromCell for std::rc::Weak<T>where
T: ?Sized,impl<T> CloneFromCell for Arc<T>where
T: ?Sized,impl<T> CloneFromCell for std::sync::Weak<T>where
T: ?Sized,impl<T, E> CloneFromCell for Result<T, E>where
T: CloneFromCell,
E: CloneFromCell,impl<T, const N: usize> CloneFromCell for [T; N]where
T: CloneFromCell,
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/cell/trait.CloneFromCell.html