#impl-block #async-await #tokio #async-std #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

#22 in #impl-block

Download history 5041/week @ 2024-03-14 3950/week @ 2024-03-21 3449/week @ 2024-03-28 5454/week @ 2024-04-04 5346/week @ 2024-04-11 4288/week @ 2024-04-18 3991/week @ 2024-04-25 5257/week @ 2024-05-02 4295/week @ 2024-05-09 4638/week @ 2024-05-16 3795/week @ 2024-05-23 6504/week @ 2024-05-30 5703/week @ 2024-06-06 5167/week @ 2024-06-13 4815/week @ 2024-06-20 3403/week @ 2024-06-27

20,607 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
~34K SLoC