1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! The parent's end of a child's pipe: [`PipeReader`] and [`PipeWriter`].
//!
//! Each is `Send + Sync`, and one end may be used from several threads at once, through a shared
//! reference or a [`try_clone`](PipeReader::try_clone). On Unix they wrap std's pipe ends and add
//! nothing. On Windows an end is the parent end of a child's pipe (overlapped) as the spawn makes
//! it, or any pipe handle a caller converts in: std's ends, an `OwnedHandle`. Either way every read
//! and write waits on an event of its own, never on the pipe's handle, which is what keeps one
//! thread's completion from releasing another's wait (std's pipe ends abort the process when two
//! threads share an end).
//!
//! A raw handle taken out of an end and wrapped in std's types is the caller's own act: std's types
//! wait on the handle itself, and are not safe to share between threads.
//!
//! On Windows a wait that fails after the kernel took a buffer cannot be answered with an error;
//! it aborts the process instead.
use std::io::{self, IoSlice, IoSliceMut, Read, Write};
#[cfg(unix)]
mod unix;
#[cfg(unix)]
use unix as sys;
#[cfg(windows)]
pub(crate) mod windows;
#[cfg(windows)]
use windows as sys;
pub(crate) use sys::child_pipe;
#[cfg(unix)]
pub(crate) use sys::{pair, set_nonblocking};
/// A parent-side pipe end retained for a configured descriptor.
#[derive(Debug)]
pub(crate) enum ParentEnd {
Reader(PipeReader),
Writer(PipeWriter),
}
/// The parent's read end of a pipe to a child.
#[derive(Debug)]
pub struct PipeReader(sys::Reader);
/// The parent's write end of a pipe to a child.
#[derive(Debug)]
pub struct PipeWriter(sys::Writer);
impl PipeReader {
/// A second handle on the same end; reads through either take from the same stream.
pub fn try_clone(&self) -> io::Result<PipeReader> {
self.0.try_clone().map(PipeReader)
}
}
impl PipeWriter {
/// A second handle on the same end; writes through either go to the same stream.
pub fn try_clone(&self) -> io::Result<PipeWriter> {
self.0.try_clone().map(PipeWriter)
}
}
impl Read for PipeReader {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
(&*self).read(buf)
}
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
(&*self).read_vectored(bufs)
}
}
impl Read for &PipeReader {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
(&self.0).read(buf)
}
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
(&self.0).read_vectored(bufs)
}
}
impl Write for PipeWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
(&*self).write(buf)
}
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
(&*self).write_vectored(bufs)
}
fn flush(&mut self) -> io::Result<()> {
(&*self).flush()
}
}
impl Write for &PipeWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
(&self.0).write(buf)
}
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
(&self.0).write_vectored(bufs)
}
fn flush(&mut self) -> io::Result<()> {
(&self.0).flush()
}
}
#[cfg(test)]
#[path = "pipe_tests.rs"]
mod pipe_tests;