How it works...

The join_all() function:

  • Collects results from several futures and returns a new future with the futures::future::JoinAll<F: Future> trait
  • The new future will perform commands for all of the aggregated futures within the futures::future::join_all call, returning a vector of Vec<T: Future::Item> in FIFO ordering
  • An error will return itself immediately and cancel the other related futures

And the shared() function:

  • futures::FutureExt::shared will create a handle that can be cloned, which resolves to the returning value of <T as futures::future::SharedItem> which can be deferred into T.
  • Useful for polling a future on more than one thread
  • This method is enabled only when Rust's std option is enabled (which it is by default)
  • The underlying result is futures::future::Shared<Future::Item>, which implements Send and Sync traits
  • Using futures::future::Shared::peek(&self) will return a value without blocking if any single shared handle has been completed

Next, the select_all_example() function:

  • futures::FutureExt::select_all returns a new future that selects from a list of vectors
  • The return value is futures::future::SelectAll, which allows us to iterate through the results
  • The future's item, index of execution, and a list of futures that still need to be processed will be returned by this function as soon as one of the futures completes its execution

Then the flatten() function:

  • futures::FutureExt::flatten will combine futures together with a returning result of their items being flattened
  • The resultant item must implement the futures::future::IntoFuture trait

Onto the fuse() function:

  • There is a small chance of undefined behavior, such as panicking or blocking forever, when polling a future that has already returned a futures::Async::Ready or Err value. The futures::FutureExt::fuse function allows us to poll the future again without worrying about undefined behavior, and will always return futures::Async::Pending.
  • The future that's being fused will be dropped upon completion in order to reclaim resources.

The inspect() function:

  • futures::FutureExt::inspect allows us to peek at an item of a future which is useful for when we are chaining combinators.

And then the chaining() function:

  • We first create a channel with three values, and spawn a thread to send those three values to the channel's receiver using the futures::FutureExt::and_then combinator.  We collect the results on line 130 from the channel.
  • Then we chain two streams together on line 134 and 135 with the collection occurring on line 140. The result of both streams should be chained together on lines 137 and 138.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.31.22