fut-compat

Crates.io docs.rs MIT licensed

Offers compatibility between the tokio and async_std runtimes by providing their most important functionalities through an unified interface.

The goal of this crate is to enable the developement of async crates which can make use of all the important objects (e.g. File, TcpListener, etc.) and functions (e.g. spawn for spawning new tasks) provided by fully-fledged async runtimes without relying on a concrete runtime. This enables downstream developers to choose the runtime which fits their needs best.

This is archieved by providing abstractions over all important functionalities and implementing them for the respective runtimes by using their respective objects and methods. We either make us of traits already given by the futures crate maintained by the rust team, or we define our own.

Features

By default no runtime specific implementation is included to reduce the number of crates you have to import. In order to use a specific runtime you must activate the corresponding feature:

|Feature|Runtime| |---------|--------| | tokio-rt | tokio | | async-std-rt | async_std |

Example

The futures crate defines the Spawn trait which abstracts away the spawn methods provided by the tokio and async_std runtimes which are used to spawn new tasks.

This crate provides two objects TokioExecutor and AsyncStdExecutor which implement the Spawn trait by internally calling the spawn method of the respective runtime. Now you can write code which internally requires a way of spawning new tasks without relying on a particular runtime.

```rust use futures::task::{Spawn, SpawnExt, SpawnError}; // Replace with AsyncStdExecutor for making use of the asyncstd runtime instead. use futcompat::task::TokioExecutor;

struct MustStartBackgroundTasks { executor: E, }

impl MustStartBackgroundTasks where E: Spawn, { async fn add(&self, x: u64, y: u64) -> Result { // Start a new task, just like you would with e.g. tokio::spawn. // spawn_with_handle must be used if you want to get a return value // from the task. let handle = self.executor.spawnwithhandle(async move { let z = x + y;

        z
    })?;

    let z = handle.await;

    Ok(z)
}

}

// Replace with [async_std::main] if needed so.

[tokio::main]

async fn main() { let executor = TokioExecutor::default();

let main_object = MustStartBackgroundTasks {
    executor,
};

let z = main_object.add(2, 2).await.unwrap();

println!("2 + 2 = {}", z);

}

```

TODO

License

This library is licensed under the MIT license.

Contribution

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