Skip to content

Commit

Permalink
Implement read_buf for Hermit
Browse files Browse the repository at this point in the history
  • Loading branch information
thaliaarchi committed Mar 10, 2025
1 parent 2c6a12e commit a372405
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion library/std/src/sys/fs/hermit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ impl File {
}

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

pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
Expand Down
14 changes: 13 additions & 1 deletion library/std/src/sys/pal/hermit/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::hermit_abi;
use crate::cmp;
use crate::io::{self, IoSlice, IoSliceMut, Read};
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read};
use crate::os::hermit::io::{FromRawFd, OwnedFd, RawFd, *};
use crate::sys::{cvt, unsupported};
use crate::sys_common::{AsInner, FromInner, IntoInner};
Expand All @@ -23,6 +23,18 @@ impl FileDesc {
Ok(result as usize)
}

pub fn read_buf(&self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
let result = cvt(unsafe {
hermit_abi::read(
self.fd.as_raw_fd(),
buf.as_mut().as_mut_ptr() as *mut u8,
buf.capacity(),
)
})?;
unsafe { buf.advance_unchecked(result as usize) };
Ok(())
}

pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
let ret = cvt(unsafe {
hermit_abi::readv(
Expand Down
5 changes: 1 addition & 4 deletions library/std/src/sys/stdio/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use hermit_abi::{EBADF, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
#[cfg(target_family = "unix")]
use libc::{EBADF, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};

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

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

0 comments on commit a372405

Please sign in to comment.