Add AsFd implementations for stdio types on WASI.

This mirrors the implementations on Unix platforms, and also mirrors the
existing `AsRawFd` impls.
This commit is contained in:
Dan Gohman 2022-08-22 12:14:35 -07:00
parent e0dc8d7801
commit 2efe6b0d3d

View File

@ -4,7 +4,7 @@
use crate::io::{self, IoSlice, IoSliceMut}; use crate::io::{self, IoSlice, IoSliceMut};
use crate::mem::ManuallyDrop; use crate::mem::ManuallyDrop;
use crate::os::raw; use crate::os::raw;
use crate::os::wasi::io::{AsRawFd, FromRawFd}; use crate::os::wasi::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd};
pub struct Stdin; pub struct Stdin;
pub struct Stdout; pub struct Stdout;
@ -23,6 +23,13 @@ fn as_raw_fd(&self) -> raw::c_int {
} }
} }
impl AsFd for Stdin {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
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)])
@ -51,6 +58,13 @@ fn as_raw_fd(&self) -> raw::c_int {
} }
} }
impl AsFd for Stdout {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
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)])
@ -82,6 +96,13 @@ fn as_raw_fd(&self) -> raw::c_int {
} }
} }
impl AsFd for Stderr {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
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)])