Skip to content

Commit

Permalink
Auto merge of rust-lang#101768 - sunfishcode:sunfishcode/wasi-stdio-l…
Browse files Browse the repository at this point in the history
…ock-asfd, r=joshtriplett

Add `AsFd` implementations for stdio lock types on WASI.

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
bors committed Oct 4, 2022
2 parents 01af504 + 905ebc3 commit d4846f9
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 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,15 @@ impl AsFd for Stdin {
}
}

#[stable(feature = "io_safety", since = "1.63.0")]
impl<'a> AsFd for io::StdinLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
// SAFETY: user code should not close stdin out from under the standard library
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 +74,15 @@ impl AsFd for Stdout {
}
}

#[stable(feature = "io_safety", since = "1.63.0")]
impl<'a> AsFd for io::StdoutLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
// SAFETY: user code should not close stdout out from under the standard library
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 +121,15 @@ impl AsFd for Stderr {
}
}

#[stable(feature = "io_safety", since = "1.63.0")]
impl<'a> AsFd for io::StderrLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
// SAFETY: user code should not close stderr out from under the standard library
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 d4846f9

Please sign in to comment.