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