From 1e36e8aea7b94c8b5bc51e247b5cfea7a4915564 Mon Sep 17 00:00:00 2001 From: Patrick Roy Date: Wed, 26 Mar 2025 10:27:06 +0000 Subject: [PATCH 1/2] io: implement [Read|Write]Volatile for &File and co Implement these traits for references to types that implement them, e.g. &File, &TcpStream, &UnixStream, &Stdout, etc. This mirrors the standard library implementations and allows calling read/write methods from contexts where no mutable reference is available. Signed-off-by: Patrick Roy --- src/io.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/src/io.rs b/src/io.rs index 5b9517ed..d9524b4d 100644 --- a/src/io.rs +++ b/src/io.rs @@ -12,7 +12,7 @@ use std::io::{Cursor, ErrorKind}; use std::io::Stdout; #[cfg(feature = "rawfd")] -use std::os::fd::AsRawFd; +use std::os::fd::{AsRawFd, RawFd}; macro_rules! retry_eintr { ($io_call: expr) => { @@ -127,7 +127,7 @@ pub trait WriteVolatile { // We explicitly implement our traits for [`std::fs::File`] and [`std::os::unix::net::UnixStream`] // instead of providing blanket implementation for [`AsRawFd`] due to trait coherence limitations: A // blanket implementation would prevent us from providing implementations for `&mut [u8]` below, as -// "an upstream crate could implement AsRawFd for &mut [u8]`. +// "an upstream crate could implement AsRawFd for &mut [u8]". macro_rules! impl_read_write_volatile_for_raw_fd { ($raw_fd_ty:ty) => { @@ -137,7 +137,27 @@ macro_rules! impl_read_write_volatile_for_raw_fd { &mut self, buf: &mut VolatileSlice, ) -> Result { - read_volatile_raw_fd(self, buf) + read_volatile_raw_fd(self.as_raw_fd(), buf) + } + } + + #[cfg(feature = "rawfd")] + impl ReadVolatile for &$raw_fd_ty { + fn read_volatile( + &mut self, + buf: &mut VolatileSlice, + ) -> Result { + read_volatile_raw_fd(self.as_raw_fd(), buf) + } + } + + #[cfg(feature = "rawfd")] + impl ReadVolatile for &mut $raw_fd_ty { + fn read_volatile( + &mut self, + buf: &mut VolatileSlice, + ) -> Result { + read_volatile_raw_fd(self.as_raw_fd(), buf) } } @@ -147,7 +167,27 @@ macro_rules! impl_read_write_volatile_for_raw_fd { &mut self, buf: &VolatileSlice, ) -> Result { - write_volatile_raw_fd(self, buf) + write_volatile_raw_fd(self.as_raw_fd(), buf) + } + } + + #[cfg(feature = "rawfd")] + impl WriteVolatile for &$raw_fd_ty { + fn write_volatile( + &mut self, + buf: &VolatileSlice, + ) -> Result { + write_volatile_raw_fd(self.as_raw_fd(), buf) + } + } + + #[cfg(feature = "rawfd")] + impl WriteVolatile for &mut $raw_fd_ty { + fn write_volatile( + &mut self, + buf: &VolatileSlice, + ) -> Result { + write_volatile_raw_fd(self.as_raw_fd(), buf) } } }; @@ -159,7 +199,17 @@ impl WriteVolatile for Stdout { &mut self, buf: &VolatileSlice, ) -> Result { - write_volatile_raw_fd(self, buf) + write_volatile_raw_fd(self.as_raw_fd(), buf) + } +} + +#[cfg(feature = "rawfd")] +impl WriteVolatile for &Stdout { + fn write_volatile( + &mut self, + buf: &VolatileSlice, + ) -> Result { + write_volatile_raw_fd(self.as_raw_fd(), buf) } } @@ -174,8 +224,8 @@ impl_read_write_volatile_for_raw_fd!(std::os::fd::BorrowedFd<'_>); /// /// Returns the numbers of bytes read. #[cfg(feature = "rawfd")] -fn read_volatile_raw_fd( - raw_fd: &mut Fd, +fn read_volatile_raw_fd( + raw_fd: RawFd, buf: &mut VolatileSlice, ) -> Result { let fd = raw_fd.as_raw_fd(); @@ -205,8 +255,8 @@ fn read_volatile_raw_fd( /// /// Returns the numbers of bytes written. #[cfg(feature = "rawfd")] -fn write_volatile_raw_fd( - raw_fd: &mut Fd, +fn write_volatile_raw_fd( + raw_fd: RawFd, buf: &VolatileSlice, ) -> Result { let fd = raw_fd.as_raw_fd(); From ac9bf670e0415070843258aa401a2fa45794ee74 Mon Sep 17 00:00:00 2001 From: Patrick Roy Date: Wed, 16 Apr 2025 07:14:31 +0100 Subject: [PATCH 2/2] chore: update coverage Signed-off-by: Patrick Roy --- coverage_config_x86_64.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coverage_config_x86_64.json b/coverage_config_x86_64.json index c55dbb37..2d04cea1 100644 --- a/coverage_config_x86_64.json +++ b/coverage_config_x86_64.json @@ -1,5 +1,5 @@ { - "coverage_score": 92.92, + "coverage_score": 92.28, "exclude_path": "mmap_windows.rs", "crate_features": "backend-mmap,backend-atomic,backend-bitmap" }