#[node]
Expand description
Use this macro to mark an impl
block on a node. This will alter
the new
and start
methods to return a tokio::task::JoinHandle
with the provided
runtime. The parameter must be a function that takes an async
closure and returns
a JoinHandle
.
static DEFAULT_TOKIO_RUNTIME: std::sync::LazyLock<tokio::runtime::Runtime> =
std::sync::LazyLock::new(|| {
tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime")
});
fn default_runtime<T: Send + 'static>(
task: impl Future<Output = T> + Send + 'static,
) -> tokio::task::JoinHandle<T> {
match tokio::runtime::Handle::try_current() {
Ok(handle) => handle.spawn(task),
Err(_) => DEFAULT_TOKIO_RUNTIME.spawn(task),
}
}