Skip to content

Commit 1b1466a

Browse files
authored
Update this crate to use the new polling breaking changes (#142)
Signed-off-by: John Nunley <[email protected]>
1 parent 63f3e14 commit 1b1466a

File tree

10 files changed

+365
-152
lines changed

10 files changed

+365
-152
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@ concurrent-queue = "2.2.0"
2929
futures-io = { version = "0.3.28", default-features = false, features = ["std"] }
3030
futures-lite = { version = "1.11.0", default-features = false }
3131
parking = "2.0.0"
32-
polling = "2.6.0"
32+
polling = "3.0.0"
3333
rustix = { version = "0.38.2", default-features = false, features = ["std", "fs"] }
3434
slab = "0.4.2"
3535
socket2 = { version = "0.5.3", features = ["all"] }
3636
tracing = { version = "0.1.37", default-features = false }
3737
waker-fn = "1.1.0"
3838

39-
[build-dependencies]
40-
autocfg = "1"
41-
4239
[dev-dependencies]
4340
async-channel = "1"
4441
async-net = "1"
@@ -54,3 +51,6 @@ timerfd = "1"
5451

5552
[target.'cfg(windows)'.dev-dependencies]
5653
uds_windows = "1"
54+
55+
[patch.crates-io]
56+
async-io = { path = "." }

build.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

examples/linux-inotify.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,21 @@ fn main() -> std::io::Result<()> {
3737
future::block_on(async {
3838
// Watch events in the current directory.
3939
let mut inotify = Async::new(Inotify::init()?)?;
40-
inotify
41-
.get_mut()
42-
.watches()
43-
.add(".", WatchMask::ALL_EVENTS)?;
40+
41+
// SAFETY: We do not move the inner file descriptor out.
42+
unsafe {
43+
inotify
44+
.get_mut()
45+
.watches()
46+
.add(".", WatchMask::ALL_EVENTS)?;
47+
}
4448
println!("Watching for filesystem events in the current directory...");
4549
println!("Try opening a file to trigger some events.");
4650
println!();
4751

4852
// Wait for events in a loop and print them on the screen.
4953
loop {
50-
for event in inotify.read_with_mut(read_op).await? {
54+
for event in unsafe { inotify.read_with_mut(read_op).await? } {
5155
println!("{:?}", event);
5256
}
5357
}

examples/windows-uds.rs

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,88 @@
88
99
#[cfg(windows)]
1010
fn main() -> std::io::Result<()> {
11+
use std::ops::Deref;
12+
use std::os::windows::io::{AsRawSocket, AsSocket, BorrowedSocket};
1113
use std::path::PathBuf;
1214

1315
use async_io::Async;
1416
use blocking::Unblock;
15-
use futures_lite::{future, io, prelude::*};
17+
use futures_lite::{future, prelude::*};
18+
use std::io;
1619
use tempfile::tempdir;
17-
use uds_windows::{UnixListener, UnixStream};
20+
21+
// n.b.: notgull: uds_windows does not support I/O safety uet, hence the wrapper types
22+
23+
struct UnixListener(uds_windows::UnixListener);
24+
25+
impl From<uds_windows::UnixListener> for UnixListener {
26+
fn from(ul: uds_windows::UnixListener) -> Self {
27+
Self(ul)
28+
}
29+
}
30+
31+
impl Deref for UnixListener {
32+
type Target = uds_windows::UnixListener;
33+
34+
fn deref(&self) -> &uds_windows::UnixListener {
35+
&self.0
36+
}
37+
}
38+
39+
impl AsSocket for UnixListener {
40+
fn as_socket(&self) -> BorrowedSocket<'_> {
41+
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
42+
}
43+
}
44+
45+
struct UnixStream(uds_windows::UnixStream);
46+
47+
impl From<uds_windows::UnixStream> for UnixStream {
48+
fn from(ul: uds_windows::UnixStream) -> Self {
49+
Self(ul)
50+
}
51+
}
52+
53+
impl Deref for UnixStream {
54+
type Target = uds_windows::UnixStream;
55+
56+
fn deref(&self) -> &uds_windows::UnixStream {
57+
&self.0
58+
}
59+
}
60+
61+
impl AsSocket for UnixStream {
62+
fn as_socket(&self) -> BorrowedSocket<'_> {
63+
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
64+
}
65+
}
66+
67+
impl io::Read for UnixStream {
68+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
69+
io::Read::read(&mut self.0, buf)
70+
}
71+
}
72+
73+
impl io::Write for UnixStream {
74+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
75+
io::Write::write(&mut self.0, buf)
76+
}
77+
78+
fn flush(&mut self) -> io::Result<()> {
79+
io::Write::flush(&mut self.0)
80+
}
81+
}
82+
83+
unsafe impl async_io::IoSafe for UnixStream {}
1884

1985
async fn client(addr: PathBuf) -> io::Result<()> {
2086
// Connect to the address.
21-
let stream = Async::new(UnixStream::connect(addr)?)?;
87+
let stream = Async::new(UnixStream::from(uds_windows::UnixStream::connect(addr)?))?;
2288
println!("Connected to {:?}", stream.get_ref().peer_addr()?);
2389

2490
// Pipe the stream to stdout.
2591
let mut stdout = Unblock::new(std::io::stdout());
26-
io::copy(&stream, &mut stdout).await?;
92+
futures_lite::io::copy(stream, &mut stdout).await?;
2793
Ok(())
2894
}
2995

@@ -32,7 +98,7 @@ fn main() -> std::io::Result<()> {
3298

3399
future::block_on(async {
34100
// Create a listener.
35-
let listener = Async::new(UnixListener::bind(&path)?)?;
101+
let listener = Async::new(UnixListener::from(uds_windows::UnixListener::bind(&path)?))?;
36102
println!("Listening on {:?}", listener.get_ref().local_addr()?);
37103

38104
future::try_zip(
@@ -42,7 +108,9 @@ fn main() -> std::io::Result<()> {
42108
println!("Accepted a client");
43109

44110
// Send a message, drop the stream, and wait for the client.
45-
Async::new(stream)?.write_all(b"Hello!\n").await?;
111+
Async::new(UnixStream::from(stream))?
112+
.write_all(b"Hello!\n")
113+
.await?;
46114
Ok(())
47115
},
48116
client(path),

0 commit comments

Comments
 (0)