tokio/
blocking.rs

1cfg_rt! {
2    pub(crate) use crate::runtime::spawn_blocking;
3
4    cfg_fs! {
5        #[allow(unused_imports)]
6        pub(crate) use crate::runtime::spawn_mandatory_blocking;
7    }
8
9    pub(crate) use crate::task::JoinHandle;
10}
11
12cfg_not_rt! {
13    use std::fmt;
14    use std::future::Future;
15    use std::pin::Pin;
16    use std::task::{Context, Poll};
17
18    pub(crate) fn spawn_blocking<F, R>(_f: F) -> JoinHandle<R>
19    where
20        F: FnOnce() -> R + Send + 'static,
21        R: Send + 'static,
22    {
23        assert_send_sync::<JoinHandle<std::cell::Cell<()>>>();
24        panic!("requires the `rt` Tokio feature flag")
25    }
26
27    cfg_fs! {
28        pub(crate) fn spawn_mandatory_blocking<F, R>(_f: F) -> Option<JoinHandle<R>>
29        where
30            F: FnOnce() -> R + Send + 'static,
31            R: Send + 'static,
32        {
33            panic!("requires the `rt` Tokio feature flag")
34        }
35    }
36
37    pub(crate) struct JoinHandle<R> {
38        _p: std::marker::PhantomData<R>,
39    }
40
41    unsafe impl<T: Send> Send for JoinHandle<T> {}
42    unsafe impl<T: Send> Sync for JoinHandle<T> {}
43
44    impl<R> Future for JoinHandle<R> {
45        type Output = Result<R, std::io::Error>;
46
47        fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
48            unreachable!()
49        }
50    }
51
52    impl<T> fmt::Debug for JoinHandle<T>
53    where
54        T: fmt::Debug,
55    {
56        fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
57            fmt.debug_struct("JoinHandle").finish()
58        }
59    }
60
61    fn assert_send_sync<T: Send + Sync>() {
62    }
63}