Skip to content
Permalink
Browse files

wasi: Use raw syscalls for stdio

I've since learned that the mapping between libc fds and wasi fds are
expected to be one-to-one, so we can use the raw syscalls for writing to
stdout/stderr and reading from stdin! This should help ensure that we
don't depend on a C library too unnecessarily.
  • Loading branch information...
alexcrichton committed Mar 31, 2019
1 parent 382f9a7 commit 60f6cbd0028c61bcca181318f48cdf0c6be61231
Showing with 9 additions and 14 deletions.
  1. +9 −14 src/libstd/sys/wasi/stdio.rs
@@ -1,6 +1,7 @@
use crate::io;
use crate::io::{self, IoVec, IoVecMut};
use crate::libc;
use crate::sys::cvt;
use crate::mem::ManuallyDrop;
use crate::sys::fd::WasiFd;

pub struct Stdin;
pub struct Stdout;
@@ -12,10 +13,8 @@ impl Stdin {
}

pub fn read(&self, data: &mut [u8]) -> io::Result<usize> {
let amt = cvt(unsafe {
libc::read(libc::STDIN_FILENO, data.as_mut_ptr() as *mut _, data.len())
})?;
Ok(amt as usize)
ManuallyDrop::new(unsafe { WasiFd::from_raw(libc::STDIN_FILENO as u32) })
.read(&mut [IoVecMut::new(data)])
}
}

@@ -25,10 +24,8 @@ impl Stdout {
}

pub fn write(&self, data: &[u8]) -> io::Result<usize> {
let amt = cvt(unsafe {
libc::write(libc::STDOUT_FILENO, data.as_ptr() as *const _, data.len())
})?;
Ok(amt as usize)
ManuallyDrop::new(unsafe { WasiFd::from_raw(libc::STDOUT_FILENO as u32) })
.write(&[IoVec::new(data)])
}

pub fn flush(&self) -> io::Result<()> {
@@ -42,10 +39,8 @@ impl Stderr {
}

pub fn write(&self, data: &[u8]) -> io::Result<usize> {
let amt = cvt(unsafe {
libc::write(libc::STDERR_FILENO, data.as_ptr() as *const _, data.len())
})?;
Ok(amt as usize)
ManuallyDrop::new(unsafe { WasiFd::from_raw(libc::STDERR_FILENO as u32) })
.write(&[IoVec::new(data)])
}

pub fn flush(&self) -> io::Result<()> {

0 comments on commit 60f6cbd

Please sign in to comment.
You can’t perform that action at this time.