Chaining futures

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 move presents 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 use move, 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:

  1. access_cmd: A String in 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 is borrowed value must be valid for the static lifetime...

  2. o: An Output passed 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.

  3. duration: A u64, in the scope of main(). Needed once (2nd future). Because it's used only once, the closure that uses it can simply be a move closure and the value will be consumed. This solution is sufficient because duration is needed nowhere else.

  4. handle: A Handle in the scope of main(). 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 a move closure 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, named hc and hc2 and use them in the two closures than needed it. This seems naive.

  5. req_addr: a String in the scope of the for_each loop. Needed twice (1st and 3rd futures). Using it in the 3rd future's closure results in a similar problem as handle. req_addr is inadvertently made inaccessible by an earlier closure that moved 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.