#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

#23 in #impl-block

Download history 3073/week @ 2023-12-07 2278/week @ 2023-12-14 1110/week @ 2023-12-21 1060/week @ 2023-12-28 2097/week @ 2024-01-04 3734/week @ 2024-01-11 3537/week @ 2024-01-18 4026/week @ 2024-01-25 2550/week @ 2024-02-01 3303/week @ 2024-02-08 3558/week @ 2024-02-15 4251/week @ 2024-02-22 3918/week @ 2024-02-29 4896/week @ 2024-03-07 5041/week @ 2024-03-14 3315/week @ 2024-03-21

18,150 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
~35K SLoC