async_rs/implementors/
noop.rs1use 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::{self, Future, Ready},
13 io::{self, Read, Write},
14 marker::PhantomData,
15 net::SocketAddr,
16 time::{Duration, Instant},
17};
18
19use task::NTask;
20
21pub type NoopRuntime = Runtime<Noop>;
23
24impl NoopRuntime {
25 #[must_use]
27 pub fn noop() -> Self {
28 Self::new(Noop)
29 }
30}
31
32#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
51pub struct Noop;
52
53impl RuntimeKit for Noop {}
54
55impl Executor for Noop {
56 type Task<T: Send + 'static> = NTask<T>;
57
58 fn block_on<T, F: Future<Output = T>>(&self, f: F) -> T {
59 util::simple_block_on(f)
62 }
63
64 fn spawn<T: Send + 'static, F: Future<Output = T> + Send + 'static>(
65 &self,
66 _f: F,
67 ) -> Task<Self::Task<T>> {
68 NTask(PhantomData).into()
69 }
70
71 fn spawn_blocking<T: Send + 'static, F: FnOnce() -> T + Send + 'static>(
72 &self,
73 _f: F,
74 ) -> Task<Self::Task<T>> {
75 NTask(PhantomData).into()
76 }
77}
78
79impl Reactor for Noop {
80 type TcpStream = DummyIO;
81 type Sleep = Ready<()>;
82
83 fn register<H: Read + Write + AsSysFd + Send + 'static>(
84 &self,
85 _socket: H,
86 ) -> io::Result<impl AsyncRead + AsyncWrite + Send + Unpin + 'static> {
87 Ok(DummyIO)
88 }
89
90 fn sleep(&self, _dur: Duration) -> Self::Sleep {
91 future::ready(())
92 }
93
94 fn interval(&self, _dur: Duration) -> impl Stream<Item = Instant> + Send + 'static {
95 DummyStream(PhantomData)
96 }
97
98 fn tcp_connect_addr(
99 &self,
100 _addr: SocketAddr,
101 ) -> impl Future<Output = io::Result<Self::TcpStream>> + Send + 'static {
102 async { Ok(DummyIO) }
103 }
104}
105
106mod task {
107 use crate::util::TaskImpl;
108 use async_trait::async_trait;
109 use std::{
110 future::Future,
111 marker::PhantomData,
112 pin::Pin,
113 task::{Context, Poll},
114 };
115
116 #[derive(Debug)]
118 pub struct NTask<T: Send + 'static>(pub(super) PhantomData<T>);
119
120 impl<T: Send + 'static> Unpin for NTask<T> {}
121
122 #[async_trait]
123 impl<T: Send + 'static> TaskImpl for NTask<T> {}
124
125 impl<T: Send + 'static> Future for NTask<T> {
126 type Output = T;
127
128 fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
129 Poll::Pending
130 }
131 }
132}
133
134#[cfg(test)]
135mod tests {
136 use super::*;
137
138 #[test]
139 fn auto_traits() {
140 use crate::util::test::*;
141 let runtime = Runtime::noop();
142 assert_send(&runtime);
143 assert_sync(&runtime);
144 assert_clone(&runtime);
145 }
146}