[][src]Macro smol::unblock

macro_rules! unblock {
    ($($code:tt)*) => { ... };
}

Runs blocking code on a thread pool.

Desugaring

Note that unblock!(expr) is syntax sugar for:

This example is not tested
Unblock::new(()).with_mut(move |_| expr).await

The () value is not used - this is just a cute trick to call Unblock::with_mut() with a closure on the thread pool.

Examples

Read the contents of a file.

use blocking::unblock;
use std::fs;

let contents = unblock!(fs::read_to_string("file.txt"))?;

Spawn a process:

use blocking::unblock;
use std::process::Command;

let out = unblock!(Command::new("dir").output())?;