Skip to content

Commit d4846f9

Browse files
committed
Auto merge of rust-lang#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 rust-lang#100892, but is for the `*Lock` types.
2 parents 01af504 + 905ebc3 commit d4846f9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

library/std/src/sys/wasi/stdio.rs

+27
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ impl AsFd for Stdin {
3030
}
3131
}
3232

33+
#[stable(feature = "io_safety", since = "1.63.0")]
34+
impl<'a> AsFd for io::StdinLock<'a> {
35+
#[inline]
36+
fn as_fd(&self) -> BorrowedFd<'_> {
37+
// SAFETY: user code should not close stdin out from under the standard library
38+
unsafe { BorrowedFd::borrow_raw(0) }
39+
}
40+
}
41+
3342
impl io::Read for Stdin {
3443
fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
3544
self.read_vectored(&mut [IoSliceMut::new(data)])
@@ -65,6 +74,15 @@ impl AsFd for Stdout {
6574
}
6675
}
6776

77+
#[stable(feature = "io_safety", since = "1.63.0")]
78+
impl<'a> AsFd for io::StdoutLock<'a> {
79+
#[inline]
80+
fn as_fd(&self) -> BorrowedFd<'_> {
81+
// SAFETY: user code should not close stdout out from under the standard library
82+
unsafe { BorrowedFd::borrow_raw(1) }
83+
}
84+
}
85+
6886
impl io::Write for Stdout {
6987
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
7088
self.write_vectored(&[IoSlice::new(data)])
@@ -103,6 +121,15 @@ impl AsFd for Stderr {
103121
}
104122
}
105123

124+
#[stable(feature = "io_safety", since = "1.63.0")]
125+
impl<'a> AsFd for io::StderrLock<'a> {
126+
#[inline]
127+
fn as_fd(&self) -> BorrowedFd<'_> {
128+
// SAFETY: user code should not close stderr out from under the standard library
129+
unsafe { BorrowedFd::borrow_raw(2) }
130+
}
131+
}
132+
106133
impl io::Write for Stderr {
107134
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
108135
self.write_vectored(&[IoSlice::new(data)])

0 commit comments

Comments
 (0)