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 pub fn noop() -> Self {
27 Self::new(Noop)
28 }
29}
30
31#[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 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 type Sleep = Ready<()>;
64
65 fn register<H: Read + Write + AsSysFd + Send + 'static>(
66 &self,
67 _socket: H,
68 ) -> io::Result<impl AsyncRead + AsyncWrite + Send + Unpin + 'static> {
69 Ok(DummyIO)
70 }
71
72 fn sleep(&self, _dur: Duration) -> Self::Sleep {
73 future::ready(())
74 }
75
76 fn interval(&self, _dur: Duration) -> impl Stream<Item = Instant> + Send + 'static {
77 DummyStream(PhantomData)
78 }
79
80 fn tcp_connect_addr(
81 &self,
82 _addr: SocketAddr,
83 ) -> impl Future<Output = io::Result<Self::TcpStream>> + Send + 'static {
84 async { Ok(DummyIO) }
85 }
86}
87
88mod task {
89 use crate::util::TaskImpl;
90 use async_trait::async_trait;
91 use std::{
92 future::Future,
93 marker::PhantomData,
94 pin::Pin,
95 task::{Context, Poll},
96 };
97
98 #[derive(Debug)]
100 pub struct NTask<T: Send + 'static>(pub(super) PhantomData<T>);
101
102 impl<T: Send + 'static> Unpin for NTask<T> {}
103
104 #[async_trait]
105 impl<T: Send + 'static> TaskImpl for NTask<T> {}
106
107 impl<T: Send + 'static> Future for NTask<T> {
108 type Output = T;
109
110 fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
111 Poll::Pending
112 }
113 }
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
121 fn auto_traits() {
122 use crate::util::test::*;
123 let runtime = Runtime::noop();
124 assert_send(&runtime);
125 assert_sync(&runtime);
126 assert_clone(&runtime);
127 }
128}