Skip to content

Commit 1e36e8a

Browse files
committed
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 <[email protected]>
1 parent 3f2fd80 commit 1e36e8a

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

src/io.rs

+59-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::io::{Cursor, ErrorKind};
1212
use std::io::Stdout;
1313

1414
#[cfg(feature = "rawfd")]
15-
use std::os::fd::AsRawFd;
15+
use std::os::fd::{AsRawFd, RawFd};
1616

1717
macro_rules! retry_eintr {
1818
($io_call: expr) => {
@@ -127,7 +127,7 @@ pub trait WriteVolatile {
127127
// We explicitly implement our traits for [`std::fs::File`] and [`std::os::unix::net::UnixStream`]
128128
// instead of providing blanket implementation for [`AsRawFd`] due to trait coherence limitations: A
129129
// blanket implementation would prevent us from providing implementations for `&mut [u8]` below, as
130-
// "an upstream crate could implement AsRawFd for &mut [u8]`.
130+
// "an upstream crate could implement AsRawFd for &mut [u8]".
131131

132132
macro_rules! impl_read_write_volatile_for_raw_fd {
133133
($raw_fd_ty:ty) => {
@@ -137,7 +137,27 @@ macro_rules! impl_read_write_volatile_for_raw_fd {
137137
&mut self,
138138
buf: &mut VolatileSlice<B>,
139139
) -> Result<usize, VolatileMemoryError> {
140-
read_volatile_raw_fd(self, buf)
140+
read_volatile_raw_fd(self.as_raw_fd(), buf)
141+
}
142+
}
143+
144+
#[cfg(feature = "rawfd")]
145+
impl ReadVolatile for &$raw_fd_ty {
146+
fn read_volatile<B: BitmapSlice>(
147+
&mut self,
148+
buf: &mut VolatileSlice<B>,
149+
) -> Result<usize, VolatileMemoryError> {
150+
read_volatile_raw_fd(self.as_raw_fd(), buf)
151+
}
152+
}
153+
154+
#[cfg(feature = "rawfd")]
155+
impl ReadVolatile for &mut $raw_fd_ty {
156+
fn read_volatile<B: BitmapSlice>(
157+
&mut self,
158+
buf: &mut VolatileSlice<B>,
159+
) -> Result<usize, VolatileMemoryError> {
160+
read_volatile_raw_fd(self.as_raw_fd(), buf)
141161
}
142162
}
143163

@@ -147,7 +167,27 @@ macro_rules! impl_read_write_volatile_for_raw_fd {
147167
&mut self,
148168
buf: &VolatileSlice<B>,
149169
) -> Result<usize, VolatileMemoryError> {
150-
write_volatile_raw_fd(self, buf)
170+
write_volatile_raw_fd(self.as_raw_fd(), buf)
171+
}
172+
}
173+
174+
#[cfg(feature = "rawfd")]
175+
impl WriteVolatile for &$raw_fd_ty {
176+
fn write_volatile<B: BitmapSlice>(
177+
&mut self,
178+
buf: &VolatileSlice<B>,
179+
) -> Result<usize, VolatileMemoryError> {
180+
write_volatile_raw_fd(self.as_raw_fd(), buf)
181+
}
182+
}
183+
184+
#[cfg(feature = "rawfd")]
185+
impl WriteVolatile for &mut $raw_fd_ty {
186+
fn write_volatile<B: BitmapSlice>(
187+
&mut self,
188+
buf: &VolatileSlice<B>,
189+
) -> Result<usize, VolatileMemoryError> {
190+
write_volatile_raw_fd(self.as_raw_fd(), buf)
151191
}
152192
}
153193
};
@@ -159,7 +199,17 @@ impl WriteVolatile for Stdout {
159199
&mut self,
160200
buf: &VolatileSlice<B>,
161201
) -> Result<usize, VolatileMemoryError> {
162-
write_volatile_raw_fd(self, buf)
202+
write_volatile_raw_fd(self.as_raw_fd(), buf)
203+
}
204+
}
205+
206+
#[cfg(feature = "rawfd")]
207+
impl WriteVolatile for &Stdout {
208+
fn write_volatile<B: BitmapSlice>(
209+
&mut self,
210+
buf: &VolatileSlice<B>,
211+
) -> Result<usize, VolatileMemoryError> {
212+
write_volatile_raw_fd(self.as_raw_fd(), buf)
163213
}
164214
}
165215

@@ -174,8 +224,8 @@ impl_read_write_volatile_for_raw_fd!(std::os::fd::BorrowedFd<'_>);
174224
///
175225
/// Returns the numbers of bytes read.
176226
#[cfg(feature = "rawfd")]
177-
fn read_volatile_raw_fd<Fd: AsRawFd>(
178-
raw_fd: &mut Fd,
227+
fn read_volatile_raw_fd(
228+
raw_fd: RawFd,
179229
buf: &mut VolatileSlice<impl BitmapSlice>,
180230
) -> Result<usize, VolatileMemoryError> {
181231
let fd = raw_fd.as_raw_fd();
@@ -205,8 +255,8 @@ fn read_volatile_raw_fd<Fd: AsRawFd>(
205255
///
206256
/// Returns the numbers of bytes written.
207257
#[cfg(feature = "rawfd")]
208-
fn write_volatile_raw_fd<Fd: AsRawFd>(
209-
raw_fd: &mut Fd,
258+
fn write_volatile_raw_fd(
259+
raw_fd: RawFd,
210260
buf: &VolatileSlice<impl BitmapSlice>,
211261
) -> Result<usize, VolatileMemoryError> {
212262
let fd = raw_fd.as_raw_fd();

0 commit comments

Comments
 (0)