|
| 1 | +//! Module for anonymous pipe |
| 2 | +//! |
| 3 | +//! ``` |
| 4 | +//! # #![cfg(not(miri))] |
| 5 | +//! #![feature(anonymous_pipe)] |
| 6 | +//! # fn main() -> std::io::Result<()> { |
| 7 | +//! let (reader, writer) = std::pipe::pipe()?; |
| 8 | +//! # Ok(()) |
| 9 | +//! # } |
| 10 | +//! ``` |
| 11 | +
|
| 12 | +use crate::{io, sys::pipe::AnonPipe}; |
| 13 | + |
| 14 | +/// Create anonymous pipe that is close-on-exec and blocking. |
| 15 | +#[unstable(feature = "anonymous_pipe", issue = "127154")] |
| 16 | +#[inline] |
| 17 | +pub fn pipe() -> io::Result<(PipeReader, PipeWriter)> { |
| 18 | + cfg_if::cfg_if! { |
| 19 | + if #[cfg(unix)] { |
| 20 | + unix::pipe() |
| 21 | + } else if #[cfg(windows)] { |
| 22 | + windows::pipe() |
| 23 | + } else { |
| 24 | + Err(io::Error::UNSUPPORTED_PLATFORM) |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/// Read end of the anonymous pipe. |
| 30 | +#[unstable(feature = "anonymous_pipe", issue = "127154")] |
| 31 | +#[derive(Debug)] |
| 32 | +pub struct PipeReader(AnonPipe); |
| 33 | + |
| 34 | +/// Write end of the anonymous pipe. |
| 35 | +#[unstable(feature = "anonymous_pipe", issue = "127154")] |
| 36 | +#[derive(Debug)] |
| 37 | +pub struct PipeWriter(AnonPipe); |
| 38 | + |
| 39 | +impl PipeReader { |
| 40 | + /// Create a new [`PipeReader`] instance that shares the same underlying file description. |
| 41 | + #[unstable(feature = "anonymous_pipe", issue = "127154")] |
| 42 | + pub fn try_clone(&self) -> io::Result<Self> { |
| 43 | + self.0.try_clone().map(Self) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl PipeWriter { |
| 48 | + /// Create a new [`PipeWriter`] instance that shares the same underlying file description. |
| 49 | + #[unstable(feature = "anonymous_pipe", issue = "127154")] |
| 50 | + pub fn try_clone(&self) -> io::Result<Self> { |
| 51 | + self.0.try_clone().map(Self) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[unstable(feature = "anonymous_pipe", issue = "127154")] |
| 56 | +impl io::Read for &PipeReader { |
| 57 | + fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 58 | + self.0.read(buf) |
| 59 | + } |
| 60 | + fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> { |
| 61 | + self.0.read_vectored(bufs) |
| 62 | + } |
| 63 | + #[inline] |
| 64 | + fn is_read_vectored(&self) -> bool { |
| 65 | + self.0.is_read_vectored() |
| 66 | + } |
| 67 | + fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { |
| 68 | + self.0.read_to_end(buf) |
| 69 | + } |
| 70 | + fn read_buf(&mut self, buf: io::BorrowedCursor<'_>) -> io::Result<()> { |
| 71 | + self.0.read_buf(buf) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +#[unstable(feature = "anonymous_pipe", issue = "127154")] |
| 76 | +impl io::Read for PipeReader { |
| 77 | + fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 78 | + self.0.read(buf) |
| 79 | + } |
| 80 | + fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> { |
| 81 | + self.0.read_vectored(bufs) |
| 82 | + } |
| 83 | + #[inline] |
| 84 | + fn is_read_vectored(&self) -> bool { |
| 85 | + self.0.is_read_vectored() |
| 86 | + } |
| 87 | + fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { |
| 88 | + self.0.read_to_end(buf) |
| 89 | + } |
| 90 | + fn read_buf(&mut self, buf: io::BorrowedCursor<'_>) -> io::Result<()> { |
| 91 | + self.0.read_buf(buf) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +#[unstable(feature = "anonymous_pipe", issue = "127154")] |
| 96 | +impl io::Write for &PipeWriter { |
| 97 | + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 98 | + self.0.write(buf) |
| 99 | + } |
| 100 | + #[inline] |
| 101 | + fn flush(&mut self) -> io::Result<()> { |
| 102 | + Ok(()) |
| 103 | + } |
| 104 | + |
| 105 | + fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> { |
| 106 | + self.0.write_vectored(bufs) |
| 107 | + } |
| 108 | + |
| 109 | + #[inline] |
| 110 | + fn is_write_vectored(&self) -> bool { |
| 111 | + self.0.is_write_vectored() |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +#[unstable(feature = "anonymous_pipe", issue = "127154")] |
| 116 | +impl io::Write for PipeWriter { |
| 117 | + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 118 | + self.0.write(buf) |
| 119 | + } |
| 120 | + #[inline] |
| 121 | + fn flush(&mut self) -> io::Result<()> { |
| 122 | + Ok(()) |
| 123 | + } |
| 124 | + |
| 125 | + fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> { |
| 126 | + self.0.write_vectored(bufs) |
| 127 | + } |
| 128 | + |
| 129 | + #[inline] |
| 130 | + fn is_write_vectored(&self) -> bool { |
| 131 | + self.0.is_write_vectored() |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +#[cfg(unix)] |
| 136 | +mod unix; |
| 137 | + |
| 138 | +#[cfg(windows)] |
| 139 | +mod windows; |
| 140 | + |
| 141 | +#[cfg(all(test, not(miri)))] |
| 142 | +mod tests; |
0 commit comments