Crate wait

Source
Expand description

§Wait For Rust

The classic problem with async is the way that it colors every function it touches. Maybe your application is not I/O bound, so you don’t want to make it async, but you want to use some library that happens to only export async functions.

What do you do?

You could make your application fully async, but that seems like overkill for an application that is cpu-bound. You could call block_on(async {}) from an async library, but that seems like a lot of overhead and boilerplate just to avoid coloring your functions.

On top of that, it can be dangerous to hold a Mutex lock across an async boundary. This can be frustrating when you have a Mutex that you want to hold for the duration of a function scope, but you need to call a function in an external library with only async functions.

That’s where this library comes in. It provides a little syntactic sugar that lets you easily wait on the results of an async function without coloring your functions with async.

Is this library necessary? Probably not. But this is more convenient than some alternatives.

§Usage

It’s as easy as 1, 2, 3.

Step 1: Add this to your Cargo.toml:

[dependencies]
wait = "0.1"

Step 2: Add the preamble to the top of any file where you want to use it:

use wait::preamble::*;

Step 3: Use the .wait() method on any async function instead of .await.

§Example

use wait::preamble::*;

async fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn main() {
    let val = add(2, 2).wait();
    println!("Result: {}", val);
}

§License

This library is distributed under the terms of either the MIT license, or the Apache 2.0 license, at your option.

Modules§

  • This is a convenience module that re-exports the Waitable trait so that every async function can be resolved with a call to the .wait() method outside of an async context.

Traits§

  • The Waitable trait defines a method that is implemented for all Future types. This allows you to call the .wait() method on any Future outside of an async context.