Skip to main content

pidfd_util/
pidfd_async.rs

1// SPDX-FileCopyrightText: 2026 The pidfd-util-rs authors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use super::PidFd;
5use async_io::Async;
6use std::io;
7use std::process::ExitStatus;
8
9impl TryFrom<PidFd> for AsyncPidFd {
10    type Error = io::Error;
11
12    fn try_from(pifd: PidFd) -> Result<Self, io::Error> {
13        Ok(Self(Async::new(pifd)?))
14    }
15}
16
17/// An async wrapper around [`PidFd`] for asynchronous process waiting.
18///
19/// This type allows waiting for a process to exit asynchronously, integrating with
20/// async runtimes via the `async-io` crate. The pidfd becomes readable when the
21/// process exits.
22///
23/// Only available when the `async` feature is enabled (which is the default).
24pub struct AsyncPidFd(Async<PidFd>);
25
26impl AsyncPidFd {
27    /// Asynchronously waits for the process to exit and returns its exit status.
28    ///
29    /// This method waits until the pidfd becomes readable (which happens when the
30    /// process exits), then retrieves the exit status.
31    ///
32    /// # Examples
33    ///
34    /// ```no_run
35    /// #![cfg_attr(feature = "nightly", feature(linux_pidfd))]
36    /// use pidfd_util::{PidFd, PidFdExt, AsyncPidFd};
37    ///
38    /// # async fn example() -> std::io::Result<()> {
39    /// let pidfd = PidFd::from_pid(1234)?;
40    /// let async_pidfd: AsyncPidFd = pidfd.try_into()?;
41    /// let status = async_pidfd.wait().await?;
42    /// # Ok(())
43    /// # }
44    /// ```
45    pub async fn wait(&self) -> io::Result<ExitStatus> {
46        self.0.readable().await?;
47        self.0.get_ref().wait()
48    }
49}