Agnostik
Agnostik is a layer between your application and the executor for your async stuff. It lets you switch the executors smooth and easy without having to change your applications code.
Features
- Run futures and wait for them to finish
- Spawn Futures using the underlying executor
- Spawn blocking tasks using special threads that are able to execute blocking code
Get started
Check the tests for simple examples.
If you have cargo-edit installed, you can just execute this:
cargo add agnostik
otherwise, add this to your Cargo.toml file
agnostik = "0.1.0"
Usage
Switching executors
You can choose the executor, by using cargo features.
The default runtime is the bastion-executor.
To use another executor you just have to disable the default features, and choose one of the valid features.
Valid features are:
runtime_bastion(default) to use the Bastion Executorruntime_tokioto use the Tokio runtimeruntime_asyncstdto use the AsyncStd runtimeruntime_smolto use the new and awesome smol runtime
E.g. to use the Tokio runtime, add the following line to your Cargo.toml
agnostik = { version = "0.1.0", default-features = false, features = ["runtime_tokio"]}
Examples
Agnostiks API is very easy and only has a few methods to use. Here's an example with the bastion-executor.
use *;
There's also a global executor instance that can be used to spawn futures without creating and storing your own executor.
If you want to use another executor, you just have to replace the Agnostik::bastion()
method call, with the method that corresponds to your executor.
Use
Agnostik::bastion()for bastionAgnostik::async_std()for async stdAgnostik::tokio()for tokio. Warning: See "How to use tokio runtime"Agnostik::tokio_with_runtime(runtime)if you want to use your owntokio::runtime::Runtimeobject. Warning: See "How to use tokio runtime"Agnostik::no_std()(coming soon) to create an exeutor that works in a nostd environment
How to use tokio runtime
It's not supported to use the tokio::main macro together with agnostik,
because Agnostik requires a Runtime object, which is created by calling Runtime::new().
If your are using the tokio::main macro, there will be a panic, because you can't create a runtime
inside a runtime.
Here's how to fix it:
use *;
async
This would fail with a panic. How to do it correctly:
use *;
use Runtime;
You can replace 1 and 2 with Agnostik::tokio(), because this method call will
create a Runtime object using Runtime::new().
Getting Help
Please head to our Discord.
License
This project is licensed under the Apache2 or MIT License.