async_rs/implementors/
noop.rs

1//! noop implementation of async runtime definition traits
2
3use crate::{
4    Runtime,
5    sys::AsSysFd,
6    traits::{Executor, Reactor, RuntimeKit},
7    util::{self, DummyIO, DummyStream, Task},
8};
9use futures_core::Stream;
10use futures_io::{AsyncRead, AsyncWrite};
11use std::{
12    future::Future,
13    io::{self, Read, Write},
14    marker::PhantomData,
15    net::SocketAddr,
16    time::{Duration, Instant},
17};
18
19use task::NTask;
20
21/// Type alias for the noop runtime
22pub type NoopRuntime = Runtime<Noop>;
23
24impl NoopRuntime {
25    /// Create a new NoopRuntime
26    pub fn noop() -> Self {
27        Self::new(Noop)
28    }
29}
30
31/// Dummy object implementing async common interfaces on top of smol
32#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
33pub struct Noop;
34
35impl RuntimeKit for Noop {}
36
37impl Executor for Noop {
38    type Task<T: Send + 'static> = NTask<T>;
39
40    fn block_on<T, F: Future<Output = T>>(&self, f: F) -> T {
41        // We cannot fake something unless we require T: Default, which we don't want.
42        // Let's get a minimalist implementation for this one.
43        util::simple_block_on(f)
44    }
45
46    fn spawn<T: Send + 'static, F: Future<Output = T> + Send + 'static>(
47        &self,
48        _f: F,
49    ) -> Task<Self::Task<T>> {
50        NTask(PhantomData).into()
51    }
52
53    fn spawn_blocking<T: Send + 'static, F: FnOnce() -> T + Send + 'static>(
54        &self,
55        _f: F,
56    ) -> Task<Self::Task<T>> {
57        NTask(PhantomData).into()
58    }
59}
60
61impl Reactor for Noop {
62    type TcpStream = DummyIO;
63
64    fn register<H: Read + Write + AsSysFd + Send + 'static>(
65        &self,
66        _socket: H,
67    ) -> io::Result<impl AsyncRead + AsyncWrite + Send + Unpin + 'static> {
68        Ok(DummyIO)
69    }
70
71    fn sleep(&self, _dur: Duration) -> impl Future<Output = ()> + Send + 'static {
72        async {}
73    }
74
75    fn interval(&self, _dur: Duration) -> impl Stream<Item = Instant> + Send + 'static {
76        DummyStream(PhantomData)
77    }
78
79    fn tcp_connect_addr(
80        &self,
81        _addr: SocketAddr,
82    ) -> impl Future<Output = io::Result<Self::TcpStream>> + Send + 'static {
83        async { Ok(DummyIO) }
84    }
85}
86
87mod task {
88    use crate::util::TaskImpl;
89    use async_trait::async_trait;
90    use std::{
91        future::Future,
92        marker::PhantomData,
93        pin::Pin,
94        task::{Context, Poll},
95    };
96
97    /// A noop task
98    #[derive(Debug)]
99    pub struct NTask<T: Send + 'static>(pub(super) PhantomData<T>);
100
101    impl<T: Send + 'static> Unpin for NTask<T> {}
102
103    #[async_trait]
104    impl<T: Send + 'static> TaskImpl for NTask<T> {}
105
106    impl<T: Send + 'static> Future for NTask<T> {
107        type Output = T;
108
109        fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
110            Poll::Pending
111        }
112    }
113}
114
115#[cfg(test)]
116mod tests {
117    use super::*;
118
119    #[test]
120    fn auto_traits() {
121        use crate::util::test::*;
122        let runtime = Runtime::noop();
123        assert_send(&runtime);
124        assert_sync(&runtime);
125        assert_clone(&runtime);
126    }
127}