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
12#[cfg(feature = "backend-experiments")]
13mod dispatch_gate;
14
15#[cfg(feature = "backend-experiments")]
16#[doc(hidden)]
17pub mod experimental;
18
19use crate::tracing::init_tracing;
20
21pub use crate::tasks::tokio::mpsc;
22pub use crate::tasks::tokio::oneshot;
23pub use crate::tasks::tokio::sleep;
24pub use crate::tasks::tokio::timeout;
25pub use crate::tasks::tokio::watch;
26pub use crate::tasks::tokio::CancellationToken;
27pub use crate::tasks::tokio::{
28    block_in_place, spawn, spawn_blocking, task_id, Handle, JoinHandle, Runtime, RuntimeFlavor,
29};
30pub use crate::tasks::tokio::{BroadcastStream, ReceiverStream};
31use std::future::Future;
32
33/// Create a tokio runtime, initialize tracing, and block on the given future.
34pub fn run<F: Future>(future: F) -> F::Output {
35    init_tracing();
36
37    let rt = Runtime::new().unwrap();
38    rt.block_on(future)
39}
40
41/// Block on a future using the current tokio runtime handle.
42/// Panics if no tokio runtime is active.
43pub fn block_on<F: Future>(future: F) -> F::Output {
44    Handle::current().block_on(future)
45}
46
47pub use crate::tasks::tokio::ctrl_c;