Woher weiß der Rust-Compiler, dass `Cell` eine interne Wandlungsfähigkeit hat?

Betrachten Sie den folgenden Code Playground version):

use std::cell::Cell;

struct Foo(u32);

#[derive(Clone, Copy)]
struct FooRef<'a>(&'a Foo);

// the body of these functions don't matter
fn testa<'a>(x: &FooRef<'a>, y: &'a Foo) { x; }
fn testa_mut<'a>(x: &mut FooRef<'a>, y: &'a Foo) { *x = FooRef(y); }
fn testb<'a>(x: &Cell<FooRef<'a>>, y: &'a Foo) { x.set(FooRef(y)); }

fn main() {
    let u1 = Foo(3);
    let u2 = Foo(5);
    let mut a = FooRef(&u1);
    let b = Cell::new(FooRef(&u1));

    // try one of the following 3 statements
    testa(&a, &u2);         // allow move at (1)
    testa_mut(&mut a, &u2); // deny move -- fine!
    testb(&b, &u2);         // deny move -- but how does rustc know?

    u2;                     // (1) move out
    // ... do something with a or b
}

Ich bin gespannt wierustc weiß, dassCell hat innere Wandlungsfähigkeit und kann sich an einer Referenz des anderen Arguments festhalten.

Wenn ich eine andere Datenstruktur von Grund auf neu erstelle, ähnlich wie beiCell das hat auch innere Wandlungsfähigkeit, wie erzähle ichrustc Das

Antworten auf die Frage(6)

Ihre Antwort auf die Frage