8 releases

0.1.6 Apr 21, 2020
0.1.5 Mar 3, 2020
0.1.4 Jan 8, 2020
0.1.2 Dec 24, 2019
0.0.0 Sep 13, 2019

#448 in Asynchronous

Download history 2476/week @ 2020-11-10 2253/week @ 2020-11-17 1687/week @ 2020-11-24 3096/week @ 2020-12-01 3178/week @ 2020-12-08 2298/week @ 2020-12-15 3191/week @ 2020-12-22 3260/week @ 2020-12-29 6374/week @ 2021-01-05 5153/week @ 2021-01-12 3983/week @ 2021-01-19 4117/week @ 2021-01-26 4214/week @ 2021-02-02 4235/week @ 2021-02-09 2641/week @ 2021-02-16 2704/week @ 2021-02-23

23,322 downloads per month
Used in 4 crates

MIT license

90KB
955 lines

Tokio Compat

Compatibility layers between tokio 0.2 and legacy versions.

Crates.io Documentation MIT licensed Build Status Discord

Website | Guides | API Docs | Chat

Overview

This crate provides compatibility runtimes that allow running both futures 0.1 futures that use tokio 0.1 runtime services and std::future futures that use tokio 0.2 runtime services.

Examples

Spawning both tokio 0.1 and tokio 0.2 futures:

use futures_01::future::lazy;

tokio_compat::run(lazy(|| {
    // spawn a `futures` 0.1 future using the `spawn` function from the
    // `tokio` 0.1 crate:
    tokio_01::spawn(lazy(|| {
        println!("hello from tokio 0.1!");
        Ok(())
    }));

    // spawn an `async` block future on the same runtime using `tokio`
    // 0.2's `spawn`:
    tokio_02::spawn(async {
        println!("hello from tokio 0.2!");
    });

    Ok(())
}))

Futures on the compat runtime can use timer APIs from both 0.1 and 0.2 versions of tokio:

use std::time::{Duration, Instant};
use futures_01::future::lazy;
use tokio_compat::prelude::*;

tokio_compat::run_std(async {
    // Wait for a `tokio` 0.1 `Delay`...
    let when = Instant::now() + Duration::from_millis(10);
    tokio_01::timer::Delay::new(when)
        // convert the delay future into a `std::future` that we can `await`.
        .compat()
        .await
        .expect("tokio 0.1 timer should work!");
    println!("10 ms have elapsed");

    // Wait for a `tokio` 0.2 `Delay`...
    let when = Instant::now() + Duration::from_millis(20);
    tokio_02::timer::delay(when).await;
    println!("20 ms have elapsed");
});

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Tokio by you, shall be licensed as MIT, without any additional terms or conditions.

Dependencies

~2.5MB
~41K SLoC