async-compat
Compatibility adapter between tokio and futures.
There are two kinds of compatibility issues between tokio and futures:
-
Tokio's types cannot be used outside the tokio context. Any attempt to use them will panic. When the
Compat
adapter is applied to a future, it will enter the tokio context. -
Tokio and futures have similar but different I/O traits
AsyncRead
,AsyncWrite
,AsyncBufRead
, andAsyncSeek
. When theCompat
adapter is applied to an I/O type, it will implement traits of the opposite kind.
You can apply the Compat
adapter using the Compat::new()
constructor or using any
method from the CompatExt
trait.
Examples
This program reads lines from stdin and echoes them into stdout:
To get around the compatibility issues, apply the Compat
adapter:
use CompatExt;
It is also possible to apply Compat
to the outer future passed to
futures::executor::block_on()
rather than futures::io::copy()
itself:
use ;
The compatibility adapter converts between tokio-based and futures-based I/O types in any direction. Here's how we can write the same program by using futures-based I/O types inside tokio:
use CompatExt;
use Unblock;
async
Finally, we can use any tokio-based crate from any other async runtime. Here are reqwest and warp as an example:
use ;
use Filter;
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.