This commit is contained in:
Mackenzie Clark 2018-12-20 23:08:00 -08:00
parent bc63bb736a
commit cfb8442d6b
3 changed files with 12 additions and 11 deletions

View File

@ -1,8 +1,8 @@
use std::io::Read; use std::ffi::CString;
use std::io; use std::io;
use std::io::Error; use std::io::Error;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::ffi::CString; use std::io::Read;
pub struct FileDescriptor(libc::c_int); pub struct FileDescriptor(libc::c_int);
@ -15,13 +15,11 @@ impl FileDescriptor {
impl Read for FileDescriptor { impl Read for FileDescriptor {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let file_descriptor: libc::c_int = self.0; let file_descriptor: libc::c_int = self.0;
let count = unsafe { let count =
libc::read(file_descriptor, buf.as_mut_ptr() as *mut libc::c_void, 1) unsafe { libc::read(file_descriptor, buf.as_mut_ptr() as *mut libc::c_void, 1) };
};
if count < 0 { if count < 0 {
Err(Error::new(ErrorKind::Other, "read error")) Err(Error::new(ErrorKind::Other, "read error"))
} } else {
else {
Ok(count as usize) Ok(count as usize)
} }
} }

View File

@ -1,6 +1,6 @@
pub mod mmap; pub mod mmap;
pub mod slice; pub mod slice;
mod file_descriptor;
#[cfg(test)] #[cfg(test)]
pub mod stdio; pub mod stdio;
mod file_descriptor;

View File

@ -1,7 +1,7 @@
use crate::common::file_descriptor::FileDescriptor;
use libc; use libc;
use std::io::BufReader; use std::io::BufReader;
use std::io::Read; use std::io::Read;
use crate::common::file_descriptor::FileDescriptor;
// A struct to hold the references to the base stdout and the captured one // A struct to hold the references to the base stdout and the captured one
pub struct StdioCapturer { pub struct StdioCapturer {
@ -21,7 +21,10 @@ impl StdioCapturer {
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
assert_eq!(unsafe { libc::pipe(fds.as_mut_ptr()) }, 0); assert_eq!(unsafe { libc::pipe(fds.as_mut_ptr()) }, 0);
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
assert_eq!(unsafe { libc::pipe(fds.as_mut_ptr(), 1000, libc::O_TEXT) }, 0); assert_eq!(
unsafe { libc::pipe(fds.as_mut_ptr(), 1000, libc::O_TEXT) },
0
);
(fds[0], fds[1]) (fds[0], fds[1])
} }