I tried to transfer the state data through my futures instead of creating a bunch of clones, but got lost in all the attempts at making that work.
At a high level, the problems seem to be:
-
Some of the data are in the scope of main(), which may not outlive the closure functions. I don't understand how this is possible, but assuming it is, I don't know how to say: it's ok it reference this essentially global data.
-
using a value more than once is tricky. The use of
movepresents a conundrum: it moves all the variables in a scope into the closure, making them inaccessible to subsequent closures. You might even "inadvertently" move things that you'd want to move later. But if you don't usemove, then there are lifetime-related problems detailed below.
In this program there are 4 futures, an initial one and 3 more chained to the first. Future 1 has access to the data it needs and is not a problem. Futures 2 and 3 are connected with the then or and_then combinators. The closures therein are where the problems lie. The 4th closure needs no data other than the result of the previous future. That's passed to the closure, so it is not a problem.
This is the data:
-
access_cmd: AStringin scope of main(). Needed twice (1st and 3rd future). Trying to access it in a closure is forbidden because it borrows the value from the main() scope and that may not live long enough. The specific error isborrowed value must be valid for the static lifetime... -
o: AnOutputpassed as a closure. needed twice (2nd and 4th futures). This one poses no ownership problem because the combinator passes the value as a closure argument. -
duration: Au64, in the scope ofmain(). Needed once (2nd future). Because it's used only once, the closure that uses it can simply be amoveclosure and the value will be consumed. This solution is sufficient becausedurationis needed nowhere else. -
handle: AHandlein the scope ofmain(). Needed three times (1st, 2nd and 3rd futures). Trying to access it in the closures of the 2nd and 3rd is troublesome. The closure may outlive the current function (main()). Converting the closure to amoveclosure results in this error message:cannot move out of captured outer variable in an FnMut closure, which I don't understand. Finally creating a single clone and attempting to use it in two different closures doesn't work because the second closure attempts to capture the value after it moved into the first closure. The solution here was to create two clones of handle, namedhcandhc2and use them in the two closures than needed it. This seems naive. -
req_addr: aStringin the scope of thefor_eachloop. Needed twice (1st and 3rd futures). Using it in the 3rd future's closure results in a similar problem ashandle.req_addris inadvertently made inaccessible by an earlier closure thatmoved the data, so isn't accessible.
So, a host of problems that all seem like they need different solutions. I'm not sure if each one should be solved individually or if there's a good technique to apply to all of them.