Wait For Rust
Wait For Rust simplifies the integration of asynchronous code into
synchronous applications without the need to rewrite your entire application
as async.
Problem Statement
In Rust, using async can be a double-edged sword. While it's powerful for
I/O-bound operations, it can introduce unnecessary complexity in CPU-bound
applications. You might want to use an external library that only provides
async functions without making your entire application asynchronous.
Common solutions include:
- Making the entire application
async, which adds overhead and complexity. - Using
block_on(async {}), which introduces dependencies and boilerplate code.
Additionally, holding a Mutex lock across an async boundary can be
dangerous and lead to deadlocks or other concurrency issues.
Solution
The Wait For Rust crate provides a simple and elegant solution. It allows
you to call async functions from synchronous contexts without coloring your
functions with async.
Usage
Getting started with Wait For Rust is straightforward:
-
Either install the crate from the command line or add the dependency to your
Cargo.tomlmanually:cargo add wait[] = "0.2" -
Use the
.wait()method on anyasyncfunction instead of.await:// The prelude attaches the `.wait()` method to all `async` functions use *; // Define an async function or use one from an external library async -
????
-
Profit
Building with no_std
This crate is no_std compatible. To use it in a no_std environment,
disable the default features in your Cargo.toml:
[]
= { = "0.2", = false }
This is not an ideal solution because it has to busy wait. The crate attempts
to reduce energy consumption by letting the CPU know that it is in a busy
loop, but it is still not as efficient as a proper async runtime can be.
Is This Library Necessary?
While you might not need this library for every project, it provides a
convenient way to integrate async functions into synchronous code. This
crate has no dependencies, so it won't add significantly to your build time.
It also reduces the boilerplate and complexity that often comes with other
solutions.
Acknowledgements
This crate is built on the shoulders of giants. Rust futures are complicated,
but popular libraries like tokio, async-std, futures-rs, and embassy
are incredible resources for learning how futures work. We thank the
maintainers and contributors of these libraries and the broader Rust
community for all of their hard work and dedication. Additionally, the CI
workflow for this repository is heavily based on the one in the futures-rs
repository.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
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.