Tidy up the Pipe interface
This commit is contained in:
1 parent
ee9b5c7a68
commit
c0f4e4907d
3 files changed
+35
-29
No files matched your search
@@ -119,7 +119,9 @@ impl FileDescriptor {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Pipes {
|
||||
/// Represents the readable and writable ends of a pair of descriptors
|
||||
/// connected via a kernel pipe.
|
||||
pub struct Pipe {
|
||||
pub read: FileDescriptor,
|
||||
pub write: FileDescriptor,
|
||||
}
|
||||
+13
-11
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
AsRawFileDescriptor, FileDescriptor, FromRawFileDescriptor, IntoRawFileDescriptor, OwnedHandle,
|
||||
Pipes,
|
||||
Pipe,
|
||||
};
|
||||
use failure::{bail, Fallible};
|
||||
use std::os::unix::prelude::*;
|
||||
@@ -140,7 +140,17 @@ impl FromRawFd for FileDescriptor {
|
||||
}
|
||||
|
||||
impl FileDescriptor {
|
||||
pub fn pipe() -> Fallible<Pipes> {
|
||||
#[inline]
|
||||
pub(crate) fn as_stdio_impl(&self) -> Fallible<std::process::Stdio> {
|
||||
let duped = OwnedHandle::dup(self)?;
|
||||
let fd = duped.into_raw_fd();
|
||||
let stdio = unsafe { std::process::Stdio::from_raw_fd(fd) };
|
||||
Ok(stdio)
|
||||
}
|
||||
}
|
||||
|
||||
impl Pipe {
|
||||
pub fn new() -> Fallible<Pipe> {
|
||||
let mut fds = [-1i32; 2];
|
||||
let res = unsafe { libc::pipe(fds.as_mut_ptr()) };
|
||||
if res == -1 {
|
||||
@@ -157,15 +167,7 @@ impl FileDescriptor {
|
||||
};
|
||||
read.handle.cloexec()?;
|
||||
write.handle.cloexec()?;
|
||||
Ok(Pipes { read, write })
|
||||
Ok(Pipe { read, write })
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn as_stdio_impl(&self) -> Fallible<std::process::Stdio> {
|
||||
let duped = OwnedHandle::dup(self)?;
|
||||
let fd = duped.into_raw_fd();
|
||||
let stdio = unsafe { std::process::Stdio::from_raw_fd(fd) };
|
||||
Ok(stdio)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
AsRawFileDescriptor, FileDescriptor, FromRawFileDescriptor, IntoRawFileDescriptor, OwnedHandle,
|
||||
Pipes,
|
||||
Pipe,
|
||||
};
|
||||
use failure::{bail, Fallible};
|
||||
use std::io::{self, Error as IoError};
|
||||
@@ -104,22 +104,6 @@ impl FileDescriptor {
|
||||
let stdio = unsafe { std::process::Stdio::from_raw_handle(handle) };
|
||||
Ok(stdio)
|
||||
}
|
||||
|
||||
pub fn pipe() -> Fallible<Pipes> {
|
||||
let mut read: HANDLE = INVALID_HANDLE_VALUE;
|
||||
let mut write: HANDLE = INVALID_HANDLE_VALUE;
|
||||
if unsafe { CreatePipe(&mut read, &mut write, ptr::null_mut(), 0) } == 0 {
|
||||
bail!("CreatePipe failed: {}", IoError::last_os_error());
|
||||
}
|
||||
Ok(Pipes {
|
||||
read: FileDescriptor {
|
||||
handle: OwnedHandle { handle: read },
|
||||
},
|
||||
write: FileDescriptor {
|
||||
handle: OwnedHandle { handle: write },
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoRawHandle for FileDescriptor {
|
||||
@@ -184,3 +168,21 @@ impl io::Write for FileDescriptor {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Pipe {
|
||||
pub fn new() -> Fallible<Pipe> {
|
||||
let mut read: HANDLE = INVALID_HANDLE_VALUE;
|
||||
let mut write: HANDLE = INVALID_HANDLE_VALUE;
|
||||
if unsafe { CreatePipe(&mut read, &mut write, ptr::null_mut(), 0) } == 0 {
|
||||
bail!("CreatePipe failed: {}", IoError::last_os_error());
|
||||
}
|
||||
Ok(Pipe {
|
||||
read: FileDescriptor {
|
||||
handle: OwnedHandle { handle: read },
|
||||
},
|
||||
write: FileDescriptor {
|
||||
handle: OwnedHandle { handle: write },
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user