Closed gakonst closed 5 years ago
I think the problem is that by calling self.bar
inside the closure you're trying to move the &self
reference into the closure. That closure needs to be 'static
because the Future will be executed by Tokio some time later. If MyStruct
implements Clone
, you could clone self
and move that into the closure.
Adding #[derive(Clone)]
to MyStruct
and instead of calling self.bar(...)
, cloning self at the beginning of foo
and using that fixed it. Thanks!
Another workaround would be to make it a static method and pass whatever fields it needs as arguments, possibly after cloning them.
But when await support lands you can probably revisit this, as borrowing self might work across awaits.
I am unable to get the following piece of code to compile (minimal example based on initial issue):
Initial error is about
bar
potentially living less thanfoo
:Changing
foo
's signature to:I'd have expected that enforcing
bar
's return value lifetime to be the same as itsself
lifetime, would fix this:note that I want to avoid inlining the function, as I might need to reuse it. Another option is to make
bar
an associated function and call it onSelf
, but that becomes unhandy if you want to use some of the Struct's fields (in the example there are none so it can be a tempting idea)Are there any potential clues as to where the lifetimes should be annotated?