Como o compilador Rust sabe que `Cell 'tem mutabilidade interna?
Considere o seguinte código (Versão para parque infantil):
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
}
Estou curioso comorustc
Sabe dissoCell
tem mutabilidade interior e pode se apegar a uma referência ao outro argumento.
Se eu criar outra estrutura de dados do zero, semelhante aCell
que também tem mutabilidade interior, como posso dizerrustc
este?