-
Notifications
You must be signed in to change notification settings - Fork 107
io: implement [Read|Write]Volatile for &File and co #321
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<B>, | ||
) -> Result<usize, VolatileMemoryError> { | ||
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<B: BitmapSlice>( | ||
&mut self, | ||
buf: &mut VolatileSlice<B>, | ||
) -> Result<usize, VolatileMemoryError> { | ||
read_volatile_raw_fd(self.as_raw_fd(), buf) | ||
} | ||
} | ||
|
||
#[cfg(feature = "rawfd")] | ||
impl ReadVolatile for &mut $raw_fd_ty { | ||
fn read_volatile<B: BitmapSlice>( | ||
&mut self, | ||
buf: &mut VolatileSlice<B>, | ||
) -> Result<usize, VolatileMemoryError> { | ||
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<B>, | ||
) -> Result<usize, VolatileMemoryError> { | ||
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<B: BitmapSlice>( | ||
&mut self, | ||
buf: &VolatileSlice<B>, | ||
) -> Result<usize, VolatileMemoryError> { | ||
write_volatile_raw_fd(self.as_raw_fd(), buf) | ||
} | ||
} | ||
|
||
#[cfg(feature = "rawfd")] | ||
impl WriteVolatile for &mut $raw_fd_ty { | ||
fn write_volatile<B: BitmapSlice>( | ||
&mut self, | ||
buf: &VolatileSlice<B>, | ||
) -> Result<usize, VolatileMemoryError> { | ||
write_volatile_raw_fd(self.as_raw_fd(), buf) | ||
} | ||
} | ||
}; | ||
|
@@ -159,7 +199,17 @@ impl WriteVolatile for Stdout { | |
&mut self, | ||
buf: &VolatileSlice<B>, | ||
) -> Result<usize, VolatileMemoryError> { | ||
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<B: BitmapSlice>( | ||
&mut self, | ||
buf: &VolatileSlice<B>, | ||
) -> Result<usize, VolatileMemoryError> { | ||
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<Fd: AsRawFd>( | ||
raw_fd: &mut Fd, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think &mut is more appropriate since you change the seek position into the file, or consume data from a pipe/socket. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, and I think that's why originally I didn't do this. But today I ended up checking the standard library, and they have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, I've changed the signature of this function to just take a RawFd instead of any kind of reference, but that doesn't really address your feedback, as it just moves the But since the standard library has |
||
fn read_volatile_raw_fd( | ||
raw_fd: RawFd, | ||
buf: &mut VolatileSlice<impl BitmapSlice>, | ||
) -> Result<usize, VolatileMemoryError> { | ||
let fd = raw_fd.as_raw_fd(); | ||
|
@@ -205,8 +255,8 @@ fn read_volatile_raw_fd<Fd: AsRawFd>( | |
/// | ||
/// Returns the numbers of bytes written. | ||
#[cfg(feature = "rawfd")] | ||
fn write_volatile_raw_fd<Fd: AsRawFd>( | ||
raw_fd: &mut Fd, | ||
fn write_volatile_raw_fd( | ||
raw_fd: RawFd, | ||
buf: &VolatileSlice<impl BitmapSlice>, | ||
) -> Result<usize, VolatileMemoryError> { | ||
let fd = raw_fd.as_raw_fd(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need
WriteVolatile
forStdout
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vm-virtio is using this for virtio-console