Skip to content
/ rust Public
forked from rust-lang/rust
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c1f2309

Browse files
authoredMar 18, 2025
Rollup merge of rust-lang#138301 - thaliaarchi:io-optional-methods/hermit, r=tgross35
Implement `read_buf` for Hermit Following hermit-os/kernel#1606, it is now safe to implement `Read::read_buf` for file descriptors on Hermit. cc `@mkroening`
2 parents 59ae71b + c5fc193 commit c1f2309

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed
 

‎library/std/src/sys/fs/hermit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ impl File {
396396
}
397397

398398
pub fn read_buf(&self, cursor: BorrowedCursor<'_>) -> io::Result<()> {
399-
crate::io::default_read_buf(|buf| self.read(buf), cursor)
399+
self.0.read_buf(cursor)
400400
}
401401

402402
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {

‎library/std/src/sys/pal/hermit/fd.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use super::hermit_abi;
44
use crate::cmp;
5-
use crate::io::{self, IoSlice, IoSliceMut, Read, SeekFrom};
5+
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read, SeekFrom};
66
use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
77
use crate::sys::{cvt, unsupported};
88
use crate::sys_common::{AsInner, FromInner, IntoInner};
@@ -23,6 +23,21 @@ impl FileDesc {
2323
Ok(result as usize)
2424
}
2525

26+
pub fn read_buf(&self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
27+
// SAFETY: The `read` syscall does not read from the buffer, so it is
28+
// safe to use `&mut [MaybeUninit<u8>]`.
29+
let result = cvt(unsafe {
30+
hermit_abi::read(
31+
self.fd.as_raw_fd(),
32+
buf.as_mut().as_mut_ptr() as *mut u8,
33+
buf.capacity(),
34+
)
35+
})?;
36+
// SAFETY: Exactly `result` bytes have been filled.
37+
unsafe { buf.advance_unchecked(result as usize) };
38+
Ok(())
39+
}
40+
2641
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
2742
let ret = cvt(unsafe {
2843
hermit_abi::readv(

‎library/std/src/sys/stdio/unix.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use hermit_abi::{EBADF, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
33
#[cfg(target_family = "unix")]
44
use libc::{EBADF, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
55

6-
#[cfg(target_family = "unix")]
7-
use crate::io::BorrowedCursor;
8-
use crate::io::{self, IoSlice, IoSliceMut};
6+
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
97
use crate::mem::ManuallyDrop;
108
#[cfg(target_os = "hermit")]
119
use crate::os::hermit::io::FromRawFd;
@@ -28,7 +26,6 @@ impl io::Read for Stdin {
2826
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDIN_FILENO)).read(buf) }
2927
}
3028

31-
#[cfg(not(target_os = "hermit"))]
3229
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> {
3330
unsafe { ManuallyDrop::new(FileDesc::from_raw_fd(STDIN_FILENO)).read_buf(buf) }
3431
}

0 commit comments

Comments
 (0)
Failed to load comments.