Skip to content

Commit

Permalink
Add AsFd implementations for stdio lock types on WASI.
Browse files Browse the repository at this point in the history
This mirrors the implementations on Unix platforms, and also mirrors the
existing `AsRawFd` impls.

This is similar to rust-lang#100892, but is for the `*Lock` types.
  • Loading branch information
sunfishcode committed Sep 22, 2022
1 parent e7119a0 commit ed812c7
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions library/std/src/sys/wasi/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ impl AsFd for Stdin {
}
}

impl<'a> AsFd for StdinLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(0) }
}
}

impl io::Read for Stdin {
fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
self.read_vectored(&mut [IoSliceMut::new(data)])
Expand Down Expand Up @@ -65,6 +72,13 @@ impl AsFd for Stdout {
}
}

impl<'a> AsFd for StdoutLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(1) }
}
}

impl io::Write for Stdout {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
self.write_vectored(&[IoSlice::new(data)])
Expand Down Expand Up @@ -103,6 +117,13 @@ impl AsFd for Stderr {
}
}

impl<'a> AsFd for StderrLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(2) }
}
}

impl io::Write for Stderr {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
self.write_vectored(&[IoSlice::new(data)])
Expand Down

0 comments on commit ed812c7

Please sign in to comment.