#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

#31 in #impl-block

Download history 4112/week @ 2024-11-17 3085/week @ 2024-11-24 3888/week @ 2024-12-01 4625/week @ 2024-12-08 3341/week @ 2024-12-15 971/week @ 2024-12-22 1478/week @ 2024-12-29 3090/week @ 2025-01-05 3291/week @ 2025-01-12 3698/week @ 2025-01-19 4760/week @ 2025-01-26 2837/week @ 2025-02-02 4182/week @ 2025-02-09 3739/week @ 2025-02-16 4052/week @ 2025-02-23 4043/week @ 2025-03-02

16,148 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
~38K SLoC