Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

roypat
Copy link
Member

@roypat roypat commented Mar 26, 2025

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:

  • All commits in this PR have Signed-Off-By trailers (with
    git commit -s), and the commit message has max 60 characters for the
    summary and max 75 characters for each description line.
  • All added/changed functionality has a corresponding unit/integration
    test.
  • All added/changed public-facing functionality has entries in the "Upcoming
    Release" section of CHANGELOG.md (if no such section exists, please create one).
  • Any newly added unsafe code is properly documented.

@@ -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,
Copy link
Member

@bonzini bonzini Mar 26, 2025

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.

Copy link
Member Author

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.

Copy link
Member Author

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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise.

@roypat roypat mentioned this pull request Mar 27, 2025
4 tasks
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]>
@roypat roypat force-pushed the read-write-by-ref branch from 01e9b7d to 1e36e8a Compare April 15, 2025 16:41
@roypat roypat requested a review from ShadowCurse as a code owner April 15, 2025 16:41
ShadowCurse
ShadowCurse previously approved these changes Apr 15, 2025
Comment on lines +206 to +212
#[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)
Copy link
Collaborator

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?

Copy link
Member Author

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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants