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::<TcpStream>::connect(addr)
40 }
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn auto_traits() {
49 use crate::util::test::*;
50 let runtime = AsyncIO;
51 assert_send(&runtime);
52 assert_sync(&runtime);
53 assert_clone(&runtime);
54 }
55}