forked from compio-rs/compio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfutures.rs
More file actions
41 lines (33 loc) · 1.06 KB
/
futures.rs
File metadata and controls
41 lines (33 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use std::{io, ops::Deref, time::Duration};
use async_io::Async;
use compio_runtime::Runtime;
use futures_util::FutureExt;
use crate::{Adapter, sys::unix::UnixAdapter};
/// Adapter for general runtime. It is driven by `async-io`.
pub struct FuturesAdapter(Async<UnixAdapter>);
impl Adapter for FuturesAdapter {
fn new(runtime: Runtime) -> io::Result<Self> {
Ok(Self(Async::new_nonblocking(UnixAdapter::new(runtime)?)?))
}
async fn wait(&self, timeout: Option<Duration>) -> io::Result<()> {
let fut = self.0.readable();
if let Some(timeout) = timeout {
let timer = async_io::Timer::after(timeout);
futures_util::select! {
res = fut.fuse() => res,
_ = timer.fuse() => Err(io::ErrorKind::TimedOut.into()),
}
} else {
fut.await
}
}
fn clear(&self) -> io::Result<()> {
self.0.get_ref().clear()
}
}
impl Deref for FuturesAdapter {
type Target = Runtime;
fn deref(&self) -> &Self::Target {
self.0.get_ref()
}
}