struct MyType {
f1: i32,
f2: ^i32 // A weak reference
}
let mut x = MyType { f1: 42, f2: ^f1 };
let f = x.f2;
let y = ^f.clone(); //^f promotes f to either &i32 or &mut i32
//Explicitly promote f to &mut i32, only type check when x is mut
let mut v:&mut i32 = ^f;
//f is still valid as it is still "week", and this is a "copy", not a "move"
let g = f;
//error: immutable borrow whilst mutable borrow in scope
let u: &i32 = ^f;
//error: send a weak reference to a function alone, without the full object
std::mem::drop(f);
//OK, we send both the object and the weak reference
(|v,_| std::mem::drop(v))(x,f);
Reasons behind:
- When initialize self-referential structs, we don’t want the usual borrow rules apply - this gives us maximal flexibility to create the struct.
- When using those self references however, we need to apply the borrow rules to ensure safety.
- So this design is to separate these requirements - by introducing a new reference type - the “weak” reference
- A weak reference can be promoted to usual references and enforce borrow rules at the promotion site
- A week reference can only be passed to another function as arguments with its owner.