interprocess_docfix/unnamed_pipe.rs
1//! Creation and usage of unnamed pipes.
2//!
3//! The distinction between named and unnamed pipes is concisely expressed by their names: where named pipes have names, unnamed pipes have handles. This can both be useful or problematic, depending on the use case. Unnamed pipes work best when a child process is used. With the fork model on Unix-like systems, the handle can be transferred to the child process thanks to the cloned address space; on Windows, inheritable handles can be used.
4//!
5//! Another way to use unnamed pipes is to use a named pipe or a Unix domain socket to establish an unnamed pipe connection. It just so happens that this crate supports all three.
6
7impmod! {unnamed_pipe,
8 UnnamedPipeReader as UnnamedPipeReaderImpl,
9 UnnamedPipeWriter as UnnamedPipeWriterImpl,
10 pipe as pipe_impl,
11}
12use std::{
13 fmt::{self, Formatter},
14 io::{self, Read, Write},
15};
16
17/// Creates a new pipe with the default creation settings and returns the handles to its writing end and reading end.
18///
19/// The platform-specific builders in the `os` module of the crate might be more helpful if a configuration process for the pipe is needed.
20pub fn pipe() -> io::Result<(UnnamedPipeWriter, UnnamedPipeReader)> {
21 pipe_impl()
22}
23
24/// A handle to the reading end of an unnamed pipe, created by the [`pipe`] function together with the [writing end].
25///
26/// The core functionality is exposed in a file-like [`Read`] interface. On Windows, the [`ShareHandle`] and [`As-`][`AsRawHandle`]/[`Into-`][`IntoRawHandle`]/[`FromRawHandle`] traits are also implemented, along with [`As-`][`AsRawFd`]/[`Into-`][`IntoRawFd`]/[`FromRawFd`] on Unix.
27///
28/// [`pipe`]: fn.pipe.html " "
29/// [writing end]: struct.UnnamedPipeWriter.html " "
30/// [`Read`]: https://doc.rust-lang.org/std/io/trait.Read.html " "
31/// [`ShareHandle`]: ../os/windows/trait.ShareHandle.html " "
32/// [`AsRawHandle`]: https://doc.rust-lang.org/std/os/windows/io/trait.AsRawHandle.html " "
33/// [`IntoRawHandle`]: https://doc.rust-lang.org/std/os/windows/io/trait.IntoRawHandle.html " "
34/// [`FromRawHandle`]: https://doc.rust-lang.org/std/os/windows/io/trait.FromRawHandle.html " "
35/// [`AsRawFd`]: https://doc.rust-lang.org/std/os/unix/io/trait.AsRawFd.html " "
36/// [`IntoRawFd`]: https://doc.rust-lang.org/std/os/unix/io/trait.IntoRawFd.html " "
37/// [`FromRawFd`]: https://doc.rust-lang.org/std/os/unix/io/trait.FromRawFd.html " "
38pub struct UnnamedPipeReader {
39 // pub(crate) to allow the platform specific builders to create the public-facing pipe types
40 pub(crate) inner: UnnamedPipeReaderImpl,
41}
42impl Read for UnnamedPipeReader {
43 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
44 self.inner.read(buf)
45 }
46}
47impl fmt::Debug for UnnamedPipeReader {
48 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
49 fmt::Debug::fmt(&self.inner, f)
50 }
51}
52impl_handle_manip!(UnnamedPipeReader);
53
54/// A handle to the writing end of an unnamed pipe, created by the [`pipe`] function together with the [reading end].
55///
56/// The core functionality is exposed in a file-like [`Write`] interface. On Windows, the [`ShareHandle`] and [`As-`][`AsRawHandle`]/[`Into-`][`IntoRawHandle`]/[`FromRawHandle`] traits are also implemented, along with [`As-`][`AsRawFd`]/[`Into-`][`IntoRawFd`]/[`FromRawFd`] on Unix.
57///
58/// [`pipe`]: fn.pipe.html " "
59/// [reading end]: struct.UnnamedPipeReader.html " "
60/// [`Write`]: https://doc.rust-lang.org/std/io/trait.Write.html " "
61/// [`ShareHandle`]: ../os/windows/trait.ShareHandle.html " "
62/// [`AsRawHandle`]: https://doc.rust-lang.org/std/os/windows/io/trait.AsRawHandle.html " "
63/// [`IntoRawHandle`]: https://doc.rust-lang.org/std/os/windows/io/trait.IntoRawHandle.html " "
64/// [`FromRawHandle`]: https://doc.rust-lang.org/std/os/windows/io/trait.FromRawHandle.html " "
65/// [`AsRawFd`]: https://doc.rust-lang.org/std/os/unix/io/trait.AsRawFd.html " "
66/// [`IntoRawFd`]: https://doc.rust-lang.org/std/os/unix/io/trait.IntoRawFd.html " "
67/// [`FromRawFd`]: https://doc.rust-lang.org/std/os/unix/io/trait.FromRawFd.html " "
68pub struct UnnamedPipeWriter {
69 pub(crate) inner: UnnamedPipeWriterImpl,
70}
71impl Write for UnnamedPipeWriter {
72 fn write(&mut self, data: &[u8]) -> io::Result<usize> {
73 self.inner.write(data)
74 }
75 fn flush(&mut self) -> io::Result<()> {
76 self.inner.flush()
77 }
78}
79impl fmt::Debug for UnnamedPipeWriter {
80 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
81 fmt::Debug::fmt(&self.inner, f)
82 }
83}
84impl_handle_manip!(UnnamedPipeWriter);