1use std::{fmt, future::Future, future::poll_fn};
2
3pub use crate::rt::{Handle, Runtime};
4
5#[derive(Debug, Copy, Clone)]
6pub struct JoinError;
7
8impl fmt::Display for JoinError {
9 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
10 write!(f, "JoinError")
11 }
12}
13
14impl std::error::Error for JoinError {}
15
16pub fn spawn<F>(fut: F) -> crate::handle::JoinHandle<F::Output>
17where
18 F: Future + 'static,
19{
20 if let Some(mut data) = crate::task::Data::load() {
21 crate::rt::Runtime::with_current(|rt| {
22 rt.spawn(async move {
23 let mut f = std::pin::pin!(fut);
24 poll_fn(|cx| data.run(|| f.as_mut().poll(cx))).await
25 })
26 })
27 } else {
28 crate::rt::Runtime::with_current(|rt| rt.spawn(fut))
29 }
30}