From 60f6cbd0028c61bcca181318f48cdf0c6be61231 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 31 Mar 2019 14:56:56 -0700 Subject: [PATCH] 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. --- src/libstd/sys/wasi/stdio.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/libstd/sys/wasi/stdio.rs b/src/libstd/sys/wasi/stdio.rs index f6a4958897d..19294788666 100644 --- a/src/libstd/sys/wasi/stdio.rs +++ b/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 @@ pub fn new() -> io::Result { } pub fn read(&self, data: &mut [u8]) -> io::Result { - 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 @@ pub fn new() -> io::Result { } pub fn write(&self, data: &[u8]) -> io::Result { - 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 @@ pub fn new() -> io::Result { } pub fn write(&self, data: &[u8]) -> io::Result { - 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<()> { -- 2.44.0