about summary refs log tree commit diff
path: root/src/libstd/sys/wasi
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2019-03-31 14:56:56 -0700
committerAlex Crichton <alex@alexcrichton.com>2019-04-01 05:31:48 -0700
commit60f6cbd0028c61bcca181318f48cdf0c6be61231 (patch)
tree65010d199b9a0ece6c8aa57f99ea6b2d06027122 /src/libstd/sys/wasi
parent382f9a7a3d15580c15531428f39bf47f55093d42 (diff)
downloadrust-60f6cbd0028c61bcca181318f48cdf0c6be61231.tar.gz
rust-60f6cbd0028c61bcca181318f48cdf0c6be61231.zip
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.
Diffstat (limited to 'src/libstd/sys/wasi')
-rw-r--r--src/libstd/sys/wasi/stdio.rs23
1 files 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 @@ 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<()> {