compio_compat/sys/unix/
tokio.rs1use std::{io, ops::Deref, time::Duration};
2
3use compio_runtime::Runtime;
4use tokio::io::{Interest, unix::AsyncFd};
5
6use crate::{Adapter, sys::unix::UnixAdapter};
7
8pub struct TokioAdapter(AsyncFd<UnixAdapter>);
10
11impl Adapter for TokioAdapter {
12 fn new(runtime: Runtime) -> io::Result<Self> {
13 Ok(Self(unsafe {
14 AsyncFd::register_with_interest(UnixAdapter::new(runtime)?, Interest::READABLE)?
15 }))
16 }
17
18 async fn wait(&self, timeout: Option<Duration>) -> io::Result<()> {
19 let fut = self.0.readable();
20 let mut guard = if let Some(timeout) = timeout {
21 tokio::time::timeout(timeout, fut).await??
22 } else {
23 fut.await?
24 };
25 guard.clear_ready();
26 Ok(())
27 }
28
29 fn clear(&self) -> io::Result<()> {
30 self.0.get_ref().clear()
31 }
32}
33
34impl Deref for TokioAdapter {
35 type Target = Runtime;
36
37 fn deref(&self) -> &Self::Target {
38 self.0.get_ref()
39 }
40}