1use std::future::Future;
2use tracing::instrument::WithSubscriber;
3use tracing::Dispatch;
4
5pub fn spawn<T>(task: T)
7where
8 T: Future + Send + 'static,
9 T::Output: Send + 'static,
10{
11 #[cfg(all(
12 feature = "tokio",
13 not(any(feature = "async-std", feature = "actix-rt"))
14 ))]
15 spawn_tokio(task);
16
17 #[cfg(all(feature = "async-std", not(feature = "actix-rt")))]
18 async_std::task::spawn(task);
19
20 #[cfg(all(feature = "actix-rt", not(feature = "async-std")))]
21 actix_rt::spawn(async {
22 task.await;
23 });
24}
25
26#[cfg(all(
27 feature = "tokio",
28 not(any(feature = "async-std", feature = "actix-rt"))
29))]
30pub fn spawn_tokio<T>(task: T)
31where
32 T: Future + Send + 'static,
33 T::Output: Send + 'static,
34{
35 let dispatcher = get_current_dispatcher();
36 tokio::spawn(task.with_subscriber(dispatcher));
37}
38
39#[cfg(all(
40 feature = "tokio",
41 not(any(feature = "async-std", feature = "actix-rt"))
42))]
43pub fn get_current_dispatcher() -> Dispatch {
44 tracing::dispatcher::get_default(|current| current.clone())
45}