Skip to content

Commit 86b6534

Browse files
committed
tokio: fix windows build
1 parent 6c5bf68 commit 86b6534

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ tokio = ["dep:tokio", "dep:tokio-stream"]
2121

2222
[dependencies]
2323
async-trait = "^0.1.89"
24+
cfg-if = "^1.0"
2425
futures-core = "^0.3"
2526
futures-io = "^0.3"
2627

src/implementors/tokio.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use crate::{AsyncIOHandle, Executor, IOHandle, Reactor, Runtime, RuntimeKit, Task, sys::IO};
44
use async_trait::async_trait;
5+
use cfg_if::cfg_if;
56
use futures_core::Stream;
67
use std::{
78
future::Future,
@@ -11,7 +12,7 @@ use std::{
1112
task::{Context, Poll},
1213
time::{Duration, Instant},
1314
};
14-
use tokio::{io::unix::AsyncFd, net::TcpStream, runtime::Handle};
15+
use tokio::{net::TcpStream, runtime::Handle};
1516
use tokio_stream::{StreamExt, wrappers::IntervalStream};
1617
use tokio_util::compat::TokioAsyncReadCompatExt;
1718

@@ -117,12 +118,16 @@ impl Reactor for Tokio {
117118
socket: IOHandle<H>,
118119
) -> io::Result<impl AsyncIOHandle + Send> {
119120
let _enter = self.handle().as_ref().map(|handle| handle.enter());
120-
if cfg!(unix) {
121-
Ok(Box::new(unix::AsyncFdWrapper(AsyncFd::new(socket)?)))
122-
} else {
123-
Err(io::Error::other(
124-
"Registering FD on tokio reactor is only supported on unix",
125-
))
121+
cfg_if! {
122+
if #[cfg(unix)] {
123+
Ok(Box::new(unix::AsyncFdWrapper(
124+
tokio::io::unix::AsyncFd::new(socket)?,
125+
)))
126+
} else {
127+
Err::<windows::Dummy, _>(io::Error::other(
128+
"Registering FD on tokio reactor is only supported on unix",
129+
))
130+
}
126131
}
127132
}
128133

@@ -149,6 +154,7 @@ mod unix {
149154
use super::*;
150155
use futures_io::{AsyncRead, AsyncWrite};
151156
use std::io::{IoSlice, IoSliceMut, Read, Write};
157+
use tokio::io::unix::AsyncFd;
152158

153159
pub(super) struct AsyncFdWrapper<H: IO + Send + 'static>(pub(super) AsyncFd<IOHandle<H>>);
154160

@@ -260,6 +266,42 @@ mod unix {
260266
}
261267
}
262268

269+
#[cfg(windows)]
270+
mod windows {
271+
use super::*;
272+
use futures_io::{AsyncRead, AsyncWrite};
273+
274+
struct Dummy;
275+
276+
impl AsyncRead for Dummy {
277+
fn poll_read(
278+
self: Pin<&mut Self>,
279+
cx: &mut Context<'_>,
280+
buf: &mut [u8],
281+
) -> Poll<io::Result<usize>> {
282+
Poll::Pending
283+
}
284+
}
285+
286+
impl AsyncWrite for Dummy {
287+
fn poll_write(
288+
self: Pin<&mut Self>,
289+
cx: &mut Context<'_>,
290+
buf: &[u8],
291+
) -> Poll<io::Result<usize>> {
292+
Poll::Pending
293+
}
294+
295+
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
296+
Poll::Pending
297+
}
298+
299+
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
300+
Poll::Pending
301+
}
302+
}
303+
}
304+
263305
#[cfg(test)]
264306
mod tests {
265307
use super::*;

0 commit comments

Comments
 (0)