Link
Link, an experimental safe anti-ownership relation between Rust objects.
The Idea
Normally, rust advocates a ownership model, in which a strict high-level to low-level relationship between objects is decided at design time.
However, sometimes you may want to do it another way, in which you can defer the ownership relation decision to a later stage.
Using Link, you are able to do so directly.
However, there are three points to remember before you start to use Link:
-
You'll need to always refer those objects as Rc<RefCell<T>>, in this way, safety is guaranteed.
-
You HAVE TO disconnect all
Links before those objects went out of scope. You can, however, disconnect from either side. -
In the mutable borrow case, you can't trip around a
Link. That will fail because in Rust you can't borrow a thing mutable twice.
How to use
Add this to your Cargo.toml:
[]
= "0.1"
= "0.1"
To use it:
Declare the structs like:
import the offset_of! macro from the "field-offset" crate:
extern crate field_offset as offset;
let mut a = new;
let mut b = new;
connect;
println! // hello
println! // 42
You can also use remote_owner_mut() to borrow the objects on the other side
of the link mutably.
References
- The
field-offsetcrate is used along with Link. - The idea is also inspired by the
lialanguage.