Skip to content

Commit 2efe6b0

Browse files
committed
Add AsFd implementations for stdio types on WASI.
This mirrors the implementations on Unix platforms, and also mirrors the existing `AsRawFd` impls.
1 parent e0dc8d7 commit 2efe6b0

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::fd::WasiFd;
44
use crate::io::{self, IoSlice, IoSliceMut};
55
use crate::mem::ManuallyDrop;
66
use crate::os::raw;
7-
use crate::os::wasi::io::{AsRawFd, FromRawFd};
7+
use crate::os::wasi::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd};
88

99
pub struct Stdin;
1010
pub struct Stdout;
@@ -23,6 +23,13 @@ impl AsRawFd for Stdin {
2323
}
2424
}
2525

26+
impl AsFd for Stdin {
27+
#[inline]
28+
fn as_fd(&self) -> BorrowedFd<'_> {
29+
unsafe { BorrowedFd::borrow_raw(0) }
30+
}
31+
}
32+
2633
impl io::Read for Stdin {
2734
fn read(&mut self, data: &mut [u8]) -> io::Result<usize> {
2835
self.read_vectored(&mut [IoSliceMut::new(data)])
@@ -51,6 +58,13 @@ impl AsRawFd for Stdout {
5158
}
5259
}
5360

61+
impl AsFd for Stdout {
62+
#[inline]
63+
fn as_fd(&self) -> BorrowedFd<'_> {
64+
unsafe { BorrowedFd::borrow_raw(1) }
65+
}
66+
}
67+
5468
impl io::Write for Stdout {
5569
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
5670
self.write_vectored(&[IoSlice::new(data)])
@@ -82,6 +96,13 @@ impl AsRawFd for Stderr {
8296
}
8397
}
8498

99+
impl AsFd for Stderr {
100+
#[inline]
101+
fn as_fd(&self) -> BorrowedFd<'_> {
102+
unsafe { BorrowedFd::borrow_raw(2) }
103+
}
104+
}
105+
85106
impl io::Write for Stderr {
86107
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
87108
self.write_vectored(&[IoSlice::new(data)])

0 commit comments

Comments
 (0)