interprocess/unnamed_pipe/
tokio.rs

1//! Tokio-based asynchronous unnamed pipes.
2//!
3//! See the [parent-level documentation](super) for more.
4//!
5//! # Examples
6//! See [`pipe()`].
7
8impmod! {unnamed_pipe::tokio,
9    Recver as RecverImpl,
10    Sender as SenderImpl,
11    pipe_impl,
12}
13use std::io;
14
15/// Creates a new pipe with the default creation settings and returns Tokio-based handles to its
16/// sending end and receiving end.
17///
18/// The platform-specific builders in the `os` module of the crate might be more helpful if extra
19/// configuration for the pipe is needed.
20///
21/// # Examples
22/// ## Basic communication
23/// In a parent process, within a Tokio runtime:
24/// ```no_run
25#[doc = doctest_file::include_doctest!("examples/unnamed_pipe/sync/side_a.rs")]
26/// ```
27/// In a child process, within a Tokio runtime:
28/// ```no_run
29#[doc = doctest_file::include_doctest!("examples/unnamed_pipe/sync/side_b.rs")]
30/// ```
31#[inline]
32pub fn pipe() -> io::Result<(Sender, Recver)> { pipe_impl() }
33
34/// Tokio-based handle to the receiving end of an unnamed pipe, created by the [`pipe()`] function
35/// together with the [sending end](Sender).
36///
37/// The core functionality is exposed via the [`AsyncRead`](tokio::io::AsyncRead) trait. The type
38/// is convertible to and from handles/file descriptors and allows its internal handle/FD to be
39/// borrowed. On Windows, the `ShareHandle` trait is also implemented.
40///
41/// The handle/file descriptor is inheritable. See [module-level documentation](self) for more on
42/// how this can be used.
43// field is pub(crate) to allow platform builders to create the public-facing pipe types
44pub struct Recver(pub(crate) RecverImpl);
45multimacro! {
46    Recver,
47    pinproj_for_unpin(RecverImpl),
48    forward_tokio_read,
49    forward_as_handle,
50    forward_try_handle(io::Error),
51    forward_debug,
52    derive_asraw,
53}
54
55/// Handle to the sending end of an unnamed pipe, created by the [`pipe()`] function together with
56/// the [receiving end](Recver).
57///
58/// The core functionality is exposed via the [`AsyncWrite`](tokio::io::AsyncWrite) trait. The
59/// type is convertible to and from handles/file descriptors and allows its internal handle/FD to
60/// be borrowed. On Windows, the `ShareHandle` trait is also implemented.
61///
62/// The handle/file descriptor is inheritable. See [module-level documentation](self) for more on
63/// how this can be used.
64pub struct Sender(pub(crate) SenderImpl);
65multimacro! {
66    Sender,
67    pinproj_for_unpin(SenderImpl),
68    forward_rbv(SenderImpl, &),
69    forward_tokio_write,
70    forward_as_handle,
71    forward_try_handle(io::Error),
72    forward_debug,
73    derive_asraw,
74}