Zero-cost Futures in Rust
This library is an implementation of futures in Rust which aims to provide a robust implementation of handling asynchronous computations, ergonomic composition and usage, and zero-cost abstractions over what would otherwise be written by hand.
Futures are a concept for an object which is a proxy for another value that may not be ready yet. For example issuing an HTTP request may return a future for the HTTP response, as it probably hasn't arrived yet. With an object representing a value that will eventually be available, futures allow for powerful composition of tasks through basic combinators that can perform operations like chaining computations, changing the types of futures, or waiting for two futures to complete at the same time.
You can find extensive tutorials and documentations at https://tokio.rs for both this crate (asynchronous programming in general) as well as the Tokio stack to perform async I/O with.
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Examples
Let's take a look at a few examples of how futures might be used:
extern crate futures;
use io;
use Duration;
use *;
use Map;
// A future is actually a trait implementation, so we can generically take a
// future of any integer and return back a future that will resolve to that
// value plus 10 more.
//
// Note here that like iterators, we're returning the `Map` combinator in
// the futures crate, not a boxed abstraction. This is a zero-cost
// construction of a future.
// Not only can we modify one future, but we can even compose them together!
// Here we have a function which takes two futures as input, and returns a
// future that will calculate the sum of their two values.
//
// Above we saw a direct return value of the `Map` combinator, but
// performance isn't always critical and sometimes it's more ergonomic to
// return a trait object like we do here. Note though that there's only one
// allocation here, not any for the intermediate futures.
// Futures also allow chaining computations together, starting another after
// the previous finishes. Here we wait for the first computation to finish,
// and then decide what to do depending on the result.
#
Some more information can also be found in the README for now, but otherwise feel free to jump in to the docs below!