thread-async 0.1.2

Execute a task in a new thread and await the result asynchronously
Documentation
  • Coverage
  • 100%
    3 out of 3 items documented1 out of 3 items with examples
  • Size
  • Source code size: 19.62 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.23 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • LiosK/thread-async-rs
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • LiosK

Crate thread_async

Crates.io License

Execute a task in a new thread and await the result asynchronously.

use std::{thread, time};

#[tokio::main]
async fn main() {
    let output = thread_async::run(|| {
        thread::sleep(time::Duration::from_millis(250));
        42
    })
    .await;

    assert_eq!(output, 42);
}

run() and its underlying primitive, run_with_builder(), execute the specified function in a separate thread and return a Future to .await the result. Each call to run() or run_with_builder() spawns a new thread that executes the specified function and wakes the current task upon completion. The specified function is triggered at the time of the call to run() or run_with_builder(), not at the time of .await.

This small crate is portable and works with any async executor, though it is suboptimal in performance as it creates a new thread for each task. Equivalent functions provided by async executors are usually recommended, unless a lightweight, executor-agnostic solution is specifically desired.