Futures/Tokio and references


I’m trying to port my toy program from mioco to tokio/futures but I have problems with the lifetime of references.
The main problem seems to be that threads/coroutines play nicely with lifetime because they have an implicit state on the stack.
The explicit state of futures seems to be much less flexible with regards to references. Especially tokio_core::reactor::Handle::spawn, which requires 'static bounds on the future.
With coroutines/threads, I can always use an Arc to get a “global” object into the closure and then borrow it for the entire lifetime of the closure, even through context switches.
With futures, I can also use an Arc, but I can only borrow it during a single poll.

I there a way to create keep an Arc/Rc an a reference in a struct, where both refer to the same object?

struct A<'a> {
    barc: Arc<B>,
    bref: &'a B,

It doesn’t have to be an Arc, just something to tie the lifetime of the B object to the lifetime of A.

Is something like this even possible?


The owning_ref crate seems to provide exactly this.
Unfortunately, I’ve over-simplified my actual problem. The crate only works for references, not for arbitrary borrows.

I need something like this:

struct A {
    owner: Arc<B>,
    borrowed: MyStruct<'?>,

Where borrowed borrows from the B instance held by barc.

Does anybody know if it’s possible to generalize owning_ref to arbitrary borrows? CC @Kimundi