You don’t currently need TypedArena
, you can just have variables with the same lifetime by declaring them in the same expression:
use std::cell::Cell;
struct Foo<'a> {
r: Cell<Option<&'a Foo<'a>>>
}
fn main() {
let (x, y);
x = Foo { r: Cell::new(None) };
y = Foo { r: Cell::new(None) };
x.r.set(Some(&y));
y.r.set(Some(&x));
}
My question is, is there any reason to limit this to static, rather than just equal lifetimes?
In my intuition, if you can do this with 'static
, you should be able to do it with any variables with the same lifetime; the lifetimes are the same, as seen by the fact that the above example with Cell
and Option
works properly, and there is no observable point in which any one of them is uninitialized. But my intuition could be wrong.
struct Foo<'a> {
r: &'a Foo<'a>
}
fn main() {
let (x, y);
(x, y) = (Foo { r: &y }, Foo { r: &x })
}