async_rs/implementors/
async_io.rs1use crate::{sys::AsSysFd, traits::Reactor, util::IOHandle};
2use async_io::{Async, Timer};
3use futures_core::Stream;
4use futures_io::{AsyncRead, AsyncWrite};
5use std::{
6 future::Future,
7 io::{self, Read, Write},
8 net::{SocketAddr, TcpStream},
9 time::{Duration, Instant},
10};
11
12#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
14pub struct AsyncIO;
15
16impl Reactor for AsyncIO {
17 type TcpStream = Async<TcpStream>;
18 type Sleep = Timer;
19
20 fn register<H: Read + Write + AsSysFd + Send + 'static>(
21 &self,
22 socket: H,
23 ) -> io::Result<impl AsyncRead + AsyncWrite + Send + Unpin + 'static> {
24 Async::new(IOHandle::new(socket))
25 }
26
27 fn sleep(&self, dur: Duration) -> Self::Sleep {
28 Timer::after(dur)
29 }
30
31 fn interval(&self, dur: Duration) -> impl Stream<Item = Instant> + Send + 'static {
32 Timer::interval(dur)
33 }
34
35 fn tcp_connect_addr(
36 &self,
37 addr: SocketAddr,
38 ) -> impl Future<Output = io::Result<Self::TcpStream>> + Send + 'static {
39 async move {
40 let stream = Async::<TcpStream>::connect(addr).await?;
41 stream.get_ref().set_nodelay(true)?;
42 Ok(stream)
43 }
44 }
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 #[test]
52 fn auto_traits() {
53 use crate::util::test::*;
54 let runtime = AsyncIO;
55 assert_send(&runtime);
56 assert_sync(&runtime);
57 assert_clone(&runtime);
58 }
59}