Function smol::unblock

source · []
pub fn unblock<T, F>(f: F) -> Task<T>Notable traits for Task<T>impl<T> Future for Task<T>    type Output = T; where
    F: 'static + FnOnce() -> T + Send,
    T: 'static + Send
Expand description

Runs blocking code on a thread pool.

Examples

Read the contents of a file:

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

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

Spawn a process:

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

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