CloneFromCell

Trait CloneFromCell 

Source
pub unsafe trait CloneFromCell: Clone { }
🔬This is a nightly-only experimental API. (cell_get_cloned #145329)
Expand description

Types for which cloning Cell<Self> is sound.

§Safety

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<'_> {}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<A: CloneFromCell, Z: CloneFromCell, Y: CloneFromCell, X: CloneFromCell, W: CloneFromCell, V: CloneFromCell, U: CloneFromCell, T: CloneFromCell> CloneFromCell for (A, Z, Y, X, W, V, U, T)

Source§

impl<B: CloneFromCell, A: CloneFromCell, Z: CloneFromCell, Y: CloneFromCell, X: CloneFromCell, W: CloneFromCell, V: CloneFromCell, U: CloneFromCell, T: CloneFromCell> CloneFromCell for (B, A, Z, Y, X, W, V, U, T)

Source§

impl<C: CloneFromCell, B: CloneFromCell, A: CloneFromCell, Z: CloneFromCell, Y: CloneFromCell, X: CloneFromCell, W: CloneFromCell, V: CloneFromCell, U: CloneFromCell, T: CloneFromCell> CloneFromCell for (C, B, A, Z, Y, X, W, V, U, T)

Source§

impl<D: CloneFromCell, C: CloneFromCell, B: CloneFromCell, A: CloneFromCell, Z: CloneFromCell, Y: CloneFromCell, X: CloneFromCell, W: CloneFromCell, V: CloneFromCell, U: CloneFromCell, T: CloneFromCell> CloneFromCell for (D, C, B, A, Z, Y, X, W, V, U, T)

Source§

impl<E: CloneFromCell, D: CloneFromCell, C: CloneFromCell, B: CloneFromCell, A: CloneFromCell, Z: CloneFromCell, Y: CloneFromCell, X: CloneFromCell, W: CloneFromCell, V: CloneFromCell, U: CloneFromCell, T: CloneFromCell> CloneFromCell for (E, D, C, B, A, Z, Y, X, W, V, U, T)

Source§

impl<T: CloneFromCell> CloneFromCell for Option<T>

Source§

impl<T: CloneFromCell> CloneFromCell for (T₁, T₂, …, Tₙ)

This trait is implemented for tuples up to twelve items long.

Source§

impl<T: CloneFromCell> CloneFromCell for ManuallyDrop<T>

Source§

impl<T: CloneFromCell> CloneFromCell for core::ops::range::Range<T>

Source§

impl<T: CloneFromCell> CloneFromCell for core::range::Range<T>

Source§

impl<T: CloneFromCell, E: CloneFromCell> CloneFromCell for Result<T, E>

Source§

impl<T: CloneFromCell, const N: usize> CloneFromCell for [T; N]

Source§

impl<T: ?Sized> CloneFromCell for PhantomData<T>

Source§

impl<U: CloneFromCell, T: CloneFromCell> CloneFromCell for (U, T)

Source§

impl<V: CloneFromCell, U: CloneFromCell, T: CloneFromCell> CloneFromCell for (V, U, T)

Source§

impl<W: CloneFromCell, V: CloneFromCell, U: CloneFromCell, T: CloneFromCell> CloneFromCell for (W, V, U, T)

Source§

impl<X: CloneFromCell, W: CloneFromCell, V: CloneFromCell, U: CloneFromCell, T: CloneFromCell> CloneFromCell for (X, W, V, U, T)

Source§

impl<Y: CloneFromCell, X: CloneFromCell, W: CloneFromCell, V: CloneFromCell, U: CloneFromCell, T: CloneFromCell> CloneFromCell for (Y, X, W, V, U, T)

Source§

impl<Z: CloneFromCell, Y: CloneFromCell, X: CloneFromCell, W: CloneFromCell, V: CloneFromCell, U: CloneFromCell, T: CloneFromCell> CloneFromCell for (Z, Y, X, W, V, U, T)