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