tokio_rayon/lib.rs
1#![warn(
2 clippy::all,
3 clippy::pedantic,
4 clippy::nursery,
5 clippy::cargo,
6 missing_docs
7)]
8
9//! Tokio's [`spawn_blocking`][spawn_blocking] and
10//! [`block_in_place`][block_in_place] run blocking code on a potentially
11//! large number of Tokio-controlled threads. This is suitable for blocking
12//! I/O, but CPU-heavy operations are often better served by a fixed-size
13//! thread pool. The Rayon crate provides exactly this, so... Why not
14//! combine them? :)
15//!
16//! ```
17//! # #[derive(Debug, PartialEq)]
18//! # struct ExpensiveNft;
19//! # fn do_some_crypto_stuff() -> ExpensiveNft { ExpensiveNft }
20//! # tokio_test::block_on(async {
21//! let nft = tokio_rayon::spawn(|| {
22//! do_some_crypto_stuff()
23//! }).await;
24//!
25//! assert_eq!(nft, ExpensiveNft);
26//! # });
27//! ```
28//!
29//! [spawn_blocking]: tokio::task::spawn_blocking
30//! [block_in_place]: tokio::task::block_in_place
31
32mod async_handle;
33mod async_thread_pool;
34mod global;
35
36pub use async_handle::*;
37pub use async_thread_pool::*;
38pub use global::*;
39pub use rayon;
40
41#[cfg(test)]
42pub(crate) mod test {
43 use rayon::ThreadPoolBuilder;
44 use std::sync::Once;
45
46 static INIT: Once = Once::new();
47
48 pub fn init() {
49 INIT.call_once(|| {
50 ThreadPoolBuilder::new()
51 .num_threads(1)
52 .build_global()
53 .unwrap();
54 });
55 }
56}