Skip to main content

spawned_rt/tasks/
mod.rs

1//! Runtime wrapper to remove dependencies from code. Using this library will
2//! allow to set a tokio runtime or any other runtime, once implemented just by
3//! changing the enabled feature.
4//! May implement the `deterministic` version based on comonware.xyz's runtime:
5//! https://github.com/commonwarexyz/monorepo/blob/main/runtime/src/deterministic.rs
6//!
7//! Currently, only a very limited set of tokio functionality is reexported. We may want to
8//! extend this functionality as needed.
9
10mod tokio;
11
12use ::tokio::runtime::Handle;
13
14use crate::tracing::init_tracing;
15
16pub use crate::tasks::tokio::mpsc;
17pub use crate::tasks::tokio::oneshot;
18pub use crate::tasks::tokio::sleep;
19pub use crate::tasks::tokio::timeout;
20pub use crate::tasks::tokio::watch;
21pub use crate::tasks::tokio::CancellationToken;
22pub use crate::tasks::tokio::{spawn, spawn_blocking, task_id, JoinHandle, Runtime};
23pub use crate::tasks::tokio::{BroadcastStream, ReceiverStream};
24use std::future::Future;
25
26/// Create a tokio runtime, initialize tracing, and block on the given future.
27pub fn run<F: Future>(future: F) -> F::Output {
28    init_tracing();
29
30    let rt = Runtime::new().unwrap();
31    rt.block_on(future)
32}
33
34/// Block on a future using the current tokio runtime handle.
35pub fn block_on<F: Future>(future: F) -> F::Output {
36    Handle::current().block_on(future)
37}
38
39pub use crate::tasks::tokio::ctrl_c;