#impl-block #async-await #async-std #tokio #generate #blocking #back-end

macro block_on_proc

Generate a blocking method for each async method in an impl block. Supports either tokio or async-std backend.

2 unstable releases

0.2.0 Jan 11, 2021
0.1.0 Nov 15, 2020

#24 in #impl-block

Download history 3319/week @ 2024-07-23 3323/week @ 2024-07-30 3055/week @ 2024-08-06 2659/week @ 2024-08-13 3113/week @ 2024-08-20 2734/week @ 2024-08-27 3285/week @ 2024-09-03 3033/week @ 2024-09-10 3256/week @ 2024-09-17 3487/week @ 2024-09-24 2160/week @ 2024-10-01 3055/week @ 2024-10-08 4970/week @ 2024-10-15 5470/week @ 2024-10-22 4914/week @ 2024-10-29 4703/week @ 2024-11-05

20,824 downloads per month
Used in 6 crates (2 directly)

MIT license

10KB
129 lines

block_on proc macro

Generate a blocking method for each async method in an impl block. Supports either tokio or async-std backend. Generated methods are suffixed with _blocking.

Example tokio

use block_on::block_on;

struct Tokio {}

#[block_on("tokio")]
impl Tokio {
    async fn test_async(&self) {}        
}

Generates the following impl block

async fn test_async(&self) {}
        
fn test_async_blocking(&self) {
    use tokio::runtime::Runtime;
    let mut rt = Runtime::new().unwrap();
    rt.block_on(self.test_async())
}

Example async-std

use block_on::block_on;

struct AsyncStd {}

#[block_on("async-std")]
impl AsyncStd {
    async fn test_async(&self) {}        
}

Generates the following method in the same impl block

async fn test_async(&self) {}        

fn test_async_blocking(&self) {
      use async_std::task;
      task::block_on(self.test_async())
}

Dependencies

~1.5MB
~37K SLoC