Skip to content

Commit

Permalink
Rollup merge of #109722 - hermitcore:read, r=Mark-Simulacrum
Browse files Browse the repository at this point in the history
Implement read_buf for RustHermit

In principle, this PR extends #108326 for RustyHermit.
  • Loading branch information
matthiaskrgr committed Apr 3, 2023
2 parents cc6a279 + a9aaf3f commit f2f5efc
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions library/std/src/sys/hermit/net.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code)]

use crate::cmp;
use crate::io::{self, IoSlice, IoSliceMut};
use crate::io::{self, BorrowedBuf, BorrowedCursor, IoSlice, IoSliceMut};
use crate::mem;
use crate::net::{Shutdown, SocketAddr};
use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, RawFd};
Expand Down Expand Up @@ -146,18 +146,35 @@ impl Socket {
Ok(Socket(unsafe { FileDesc::from_raw_fd(fd) }))
}

fn recv_with_flags(&self, buf: &mut [u8], flags: i32) -> io::Result<usize> {
let ret =
cvt(unsafe { netc::recv(self.0.as_raw_fd(), buf.as_mut_ptr(), buf.len(), flags) })?;
Ok(ret as usize)
fn recv_with_flags(&self, mut buf: BorrowedCursor<'_>, flags: i32) -> io::Result<()> {
let ret = cvt(unsafe {
netc::recv(
self.0.as_raw_fd(),
buf.as_mut().as_mut_ptr() as *mut u8,
buf.capacity(),
flags,
)
})?;
unsafe {
buf.advance(ret as usize);
}
Ok(())
}

pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
self.recv_with_flags(buf, 0)
let mut buf = BorrowedBuf::from(buf);
self.recv_with_flags(buf.unfilled(), 0)?;
Ok(buf.len())
}

pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
self.recv_with_flags(buf, netc::MSG_PEEK)
let mut buf = BorrowedBuf::from(buf);
self.recv_with_flags(buf.unfilled(), netc::MSG_PEEK)?;
Ok(buf.len())
}

pub fn read_buf(&self, buf: BorrowedCursor<'_>) -> io::Result<()> {
self.recv_with_flags(buf, 0)
}

pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
Expand Down

0 comments on commit f2f5efc

Please sign in to comment.