Given the following example:
struct A;
struct B<'a> {
ptr: &'a A
}
struct C<'a> {
ptr: &'a A
}
struct Context<'a> {
a: A,
b: B<'a>,
c: C<'a>
}
fn setup<'a>() -> Context<'a> {
let a = A;
let b = B {ptr: &a};
let c = C {ptr: &a};
Context {a: a, b: b, c: c}
}
#[test]
fn it_works() {
let context = setup();
}
the compiler basically complains about references to an object that is later moved:
error: `a` does not live long enough
let b = B {ptr: &a};
^
note: reference must be valid for the lifetime 'a as defined on the block at 18:30...
fn setup<'a>() -> Context<'a> {
^
note: ...but borrowed value is only valid for the block suffix following statement 0 at 19:14
let a = A;
^
I understand that moving out of a value is impossible as long as it's borrowed (and that is basically what happens).
However, there should be a way around this as the data itself indeed lives long enough: Context
has the same lifetime as B
and C
, so both .ptr
s are valid at any time.
Does anyone know a solution for this? Background for this architecture: In a larger program I want to have a single context object that can be borrowed or moved around as needed. In order to keep dependencies at a minimum and make tests as painless as possible, submodules have their own context objects (the A
and B
s in the example) whenever possible. Immutably shared data should be stored as a reference as shown in the example.
The only thing I've come up with so far is explicitly calling each submodule's function with the reference (&a
) as argument which is kind of verbose.