From 163949bd26f8ab9c5fca19b4463485f48baae30c Mon Sep 17 00:00:00 2001 From: Frederick Mayle Date: Tue, 9 Apr 2024 14:00:15 -0700 Subject: [PATCH 1/2] tests: update nix dependency to 0.28 `nix::unistd::write` now requies an `trait AsFd`, which `RawFd` does not implement, so, the `struct FileDescriptor` field has been switched to `OwnedFd`. An alternative solution is to use `BorrowedFd::borrow_raw`, but that requires unsafe code. --- tokio/Cargo.toml | 2 +- tokio/tests/io_async_fd.rs | 31 +++++++++---------------------- tokio/tests/net_unix_pipe.rs | 4 +--- 3 files changed, 11 insertions(+), 26 deletions(-) diff --git a/tokio/Cargo.toml b/tokio/Cargo.toml index 020cc1e4ac2..49e245ebf75 100644 --- a/tokio/Cargo.toml +++ b/tokio/Cargo.toml @@ -118,7 +118,7 @@ signal-hook-registry = { version = "1.1.1", optional = true } [target.'cfg(unix)'.dev-dependencies] libc = { version = "0.2.149" } -nix = { version = "0.27.1", default-features = false, features = ["fs", "socket"] } +nix = { version = "0.28.0", default-features = false, features = ["fs", "socket"] } [target.'cfg(windows)'.dependencies.windows-sys] version = "0.48" diff --git a/tokio/tests/io_async_fd.rs b/tokio/tests/io_async_fd.rs index ea798b3067a..4e6f630eb93 100644 --- a/tokio/tests/io_async_fd.rs +++ b/tokio/tests/io_async_fd.rs @@ -1,7 +1,7 @@ #![warn(rust_2018_idioms)] #![cfg(all(unix, feature = "full"))] -use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd}; +use std::os::unix::io::{AsRawFd, RawFd}; use std::sync::{ atomic::{AtomicBool, Ordering}, Arc, @@ -13,7 +13,7 @@ use std::{ task::{Context, Waker}, }; -use nix::unistd::{close, read, write}; +use nix::unistd::{read, write}; use futures::poll; @@ -58,18 +58,18 @@ impl TestWaker { #[derive(Debug)] struct FileDescriptor { - fd: RawFd, + fd: std::os::fd::OwnedFd, } impl AsRawFd for FileDescriptor { fn as_raw_fd(&self) -> RawFd { - self.fd + self.fd.as_raw_fd() } } impl Read for &FileDescriptor { fn read(&mut self, buf: &mut [u8]) -> io::Result { - read(self.fd, buf).map_err(io::Error::from) + read(self.fd.as_raw_fd(), buf).map_err(io::Error::from) } } @@ -81,7 +81,7 @@ impl Read for FileDescriptor { impl Write for &FileDescriptor { fn write(&mut self, buf: &[u8]) -> io::Result { - write(self.fd, buf).map_err(io::Error::from) + write(&self.fd, buf).map_err(io::Error::from) } fn flush(&mut self) -> io::Result<()> { @@ -99,12 +99,6 @@ impl Write for FileDescriptor { } } -impl Drop for FileDescriptor { - fn drop(&mut self) { - let _ = close(self.fd); - } -} - fn set_nonblocking(fd: RawFd) { use nix::fcntl::{OFlag, F_GETFL, F_SETFL}; @@ -133,17 +127,10 @@ fn socketpair() -> (FileDescriptor, FileDescriptor) { SockFlag::empty(), ) .expect("socketpair"); - let fds = ( - FileDescriptor { - fd: fd_a.into_raw_fd(), - }, - FileDescriptor { - fd: fd_b.into_raw_fd(), - }, - ); + let fds = (FileDescriptor { fd: fd_a }, FileDescriptor { fd: fd_b }); - set_nonblocking(fds.0.fd); - set_nonblocking(fds.1.fd); + set_nonblocking(fds.0.fd.as_raw_fd()); + set_nonblocking(fds.1.fd.as_raw_fd()); fds } diff --git a/tokio/tests/net_unix_pipe.rs b/tokio/tests/net_unix_pipe.rs index 6706880ed1b..37b8b41bd31 100644 --- a/tokio/tests/net_unix_pipe.rs +++ b/tokio/tests/net_unix_pipe.rs @@ -489,12 +489,10 @@ async fn anon_pipe_spawn_echo() -> std::io::Result<()> { #[cfg(target_os = "linux")] async fn anon_pipe_from_owned_fd() -> std::io::Result<()> { use nix::fcntl::OFlag; - use std::os::unix::io::{FromRawFd, OwnedFd}; const DATA: &[u8] = b"this is some data to write to the pipe"; - let fds = nix::unistd::pipe2(OFlag::O_CLOEXEC | OFlag::O_NONBLOCK)?; - let (rx_fd, tx_fd) = unsafe { (OwnedFd::from_raw_fd(fds.0), OwnedFd::from_raw_fd(fds.1)) }; + let (rx_fd, tx_fd) = nix::unistd::pipe2(OFlag::O_CLOEXEC | OFlag::O_NONBLOCK)?; let mut rx = pipe::Receiver::from_owned_fd(rx_fd)?; let mut tx = pipe::Sender::from_owned_fd(tx_fd)?; From 6b8c485b60fbf0a334a82da620e7ec7e44c3b9de Mon Sep 17 00:00:00 2001 From: Alice Ryhl Date: Wed, 10 Apr 2024 08:10:55 +0200 Subject: [PATCH 2/2] Add aio feature to nix dependency --- tokio/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tokio/Cargo.toml b/tokio/Cargo.toml index 49e245ebf75..99e6b666de8 100644 --- a/tokio/Cargo.toml +++ b/tokio/Cargo.toml @@ -118,7 +118,7 @@ signal-hook-registry = { version = "1.1.1", optional = true } [target.'cfg(unix)'.dev-dependencies] libc = { version = "0.2.149" } -nix = { version = "0.28.0", default-features = false, features = ["fs", "socket"] } +nix = { version = "0.28.0", default-features = false, features = ["aio", "fs", "socket"] } [target.'cfg(windows)'.dependencies.windows-sys] version = "0.48"