-
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?
Conversation
@@ -175,7 +205,7 @@ 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 comment
The 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 comment
The 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 impl Read for &File
, e.g. advancing the seek position/consuming data through an immutable reference is possible for the stdlib version of these traits. I suppose this is considered okay because read
-ing from a File doesn't change any Rust state (e.g. the File object itself remains unmodified), only kernel internal state.
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.
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 as_raw_fd()
call up to the caller.
But since the standard library has impl Read for &File
, do you still have any concerns about providing the same kind of impls here?
src/io.rs
Outdated
@@ -206,7 +236,7 @@ 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, | |||
raw_fd: &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.
Likewise.
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]>
01e9b7d
to
1e36e8a
Compare
#[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) |
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
for Stdout
?
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
Signed-off-by: Patrick Roy <[email protected]>
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.
There's probably something more clever we can do with the traits to avoid some repetition, but really, isn't not that bad imo.
Summary of the PR
Please summarize here why the changes in this PR are needed.
Requirements
Before submitting your PR, please make sure you addressed the following
requirements:
git commit -s
), and the commit message has max 60 characters for thesummary and max 75 characters for each description line.
test.
Release" section of CHANGELOG.md (if no such section exists, please create one).
unsafe
code is properly documented.