use std::future::Future;
use tokio::task::JoinHandle;
use crate::{Key, Registry, Span, ToRootSpan};
impl Registry {
pub fn spawn<T>(
&self,
key: impl Key,
root_span: impl Into<Span>,
future: T,
) -> JoinHandle<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
tokio::spawn(self.register(key, root_span).instrument(future))
}
pub fn spawn_derived_root<T>(
&self,
key: impl Key + ToRootSpan,
future: T,
) -> JoinHandle<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
let root_span = key.to_root_span();
self.spawn(key, root_span, future)
}
pub fn spawn_anonymous<T>(&self, root_span: impl Into<Span>, future: T) -> JoinHandle<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
tokio::spawn(self.register_anonymous(root_span).instrument(future))
}
}
pub fn spawn<T>(key: impl Key, root_span: impl Into<Span>, future: T) -> JoinHandle<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
if let Some(registry) = Registry::try_current() {
registry.spawn(key, root_span, future)
} else {
tokio::spawn(future)
}
}
pub fn spawn_derived_root<T>(key: impl Key + ToRootSpan, future: T) -> JoinHandle<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
let root_span = key.to_root_span();
spawn(key, root_span, future)
}
pub fn spawn_anonymous<T>(root_span: impl Into<Span>, future: T) -> JoinHandle<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
if let Some(registry) = Registry::try_current() {
registry.spawn_anonymous(root_span, future)
} else {
tokio::spawn(future)
}
}