Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump all subcrates to their newest versions #283

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ exclude = ["/.*"]
async-channel = "2.0.0"
async-executor = "1.5.0"
async-fs = "2.0.0"
async-io = "1.12.0"
async-io = "2.1.0"
async-lock = "3.0.0"
async-net = "2.0.0"
blocking = "1.3.0"
futures-lite = "1.11.0"
futures-lite = "2.0.0"

[target.'cfg(not(target_os = "espidf"))'.dependencies]
async-process = "1.6.0"
async-process = "2.0.0"

[dev-dependencies]
anyhow = "1"
Expand Down
11 changes: 5 additions & 6 deletions examples/linux-inotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#[cfg(target_os = "linux")]
fn main() -> std::io::Result<()> {
use std::ffi::OsString;
use std::os::unix::io::AsFd;

use inotify::{EventMask, Inotify, WatchMask};
use smol::{io, Async};
Expand All @@ -34,18 +35,16 @@ fn main() -> std::io::Result<()> {

smol::block_on(async {
// Watch events in the current directory.
let mut inotify = Async::new(Inotify::init()?)?;
inotify
.get_mut()
.watches()
.add(".", WatchMask::ALL_EVENTS)?;
let mut inotify = Inotify::init()?;
let source = Async::new(inotify.as_fd().try_clone_to_owned()?)?;
inotify.watches().add(".", WatchMask::ALL_EVENTS)?;
println!("Watching for filesystem events in the current directory...");
println!("Try opening a file to trigger some events.");
println!();

// Wait for events in a loop and print them on the screen.
loop {
for event in inotify.read_with_mut(read_op).await? {
for event in source.read_with(|_| read_op(&mut inotify)).await? {
println!("{:?}", event);
}
}
Expand Down
101 changes: 86 additions & 15 deletions examples/windows-uds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,112 @@

#[cfg(windows)]
fn main() -> std::io::Result<()> {
use std::ops::Deref;
use std::os::windows::io::{AsRawSocket, AsSocket, BorrowedSocket};
use std::path::PathBuf;

use smol::{io, prelude::*, Async, Unblock};
use smol::{future, prelude::*, Async, Unblock};
use std::io;
use tempfile::tempdir;
use uds_windows::{UnixListener, UnixStream};

// n.b.: notgull: uds_windows does not support I/O safety yet, hence the wrapper types

struct UnixListener(uds_windows::UnixListener);

impl From<uds_windows::UnixListener> for UnixListener {
fn from(ul: uds_windows::UnixListener) -> Self {
Self(ul)
}
}

impl Deref for UnixListener {
type Target = uds_windows::UnixListener;

fn deref(&self) -> &uds_windows::UnixListener {
&self.0
}
}

impl AsSocket for UnixListener {
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}

struct UnixStream(uds_windows::UnixStream);

impl From<uds_windows::UnixStream> for UnixStream {
fn from(ul: uds_windows::UnixStream) -> Self {
Self(ul)
}
}

impl Deref for UnixStream {
type Target = uds_windows::UnixStream;

fn deref(&self) -> &uds_windows::UnixStream {
&self.0
}
}

impl AsSocket for UnixStream {
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}

impl io::Read for UnixStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
io::Read::read(&mut self.0, buf)
}
}

impl io::Write for UnixStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
io::Write::write(&mut self.0, buf)
}

fn flush(&mut self) -> io::Result<()> {
io::Write::flush(&mut self.0)
}
}

unsafe impl async_io::IoSafe for UnixStream {}

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

// Pipe the stream to stdout.
let mut stdout = Unblock::new(std::io::stdout());
io::copy(&stream, &mut stdout).await?;
futures_lite::io::copy(stream, &mut stdout).await?;
Ok(())
}

let dir = tempdir()?;
let path = dir.path().join("socket");

smol::block_on(async {
future::block_on(async {
// Create a listener.
let listener = Async::new(UnixListener::bind(&path)?)?;
let listener = Async::new(UnixListener::from(uds_windows::UnixListener::bind(&path)?))?;
println!("Listening on {:?}", listener.get_ref().local_addr()?);

// Spawn a client task.
let task = smol::spawn(client(path));

// Accept the client.
let (stream, _) = listener.read_with(|l| l.accept()).await?;
println!("Accepted a client");
future::try_zip(
async {
// Accept the client.
let (stream, _) = listener.read_with(|l| l.accept()).await?;
println!("Accepted a client");

// Send a message, drop the stream, and wait for the client.
Async::new(stream)?.write_all(b"Hello!\n").await?;
task.await?;
// Send a message, drop the stream, and wait for the client.
Async::new(UnixStream::from(stream))?
.write_all(b"Hello!\n")
.await?;
Ok(())
},
client(path),
)
.await?;

Ok(())
})
Expand Down