Auto merge of #101768 - sunfishcode:sunfishcode/wasi-stdio-lock-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 #100892, but is for the `*Lock` types.
This commit is contained in:
bors 2022-10-04 23:22:16 +00:00
commit d4846f9d03

View File

@ -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 { impl io::Read for Stdin {
fn read(&mut self, data: &mut [u8]) -> io::Result<usize> { fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
self.read_vectored(&mut [IoSliceMut::new(data)]) self.read_vectored(&mut [IoSliceMut::new(data)])
@ -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 { impl io::Write for Stdout {
fn write(&mut self, data: &[u8]) -> io::Result<usize> { fn write(&mut self, data: &[u8]) -> io::Result<usize> {
self.write_vectored(&[IoSlice::new(data)]) self.write_vectored(&[IoSlice::new(data)])
@ -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 { impl io::Write for Stderr {
fn write(&mut self, data: &[u8]) -> io::Result<usize> { fn write(&mut self, data: &[u8]) -> io::Result<usize> {
self.write_vectored(&[IoSlice::new(data)]) self.write_vectored(&[IoSlice::new(data)])