Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow waiting on process exit without requiring a mutable borrow and without dropping stdin #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl Child {
/// }
/// # std::io::Result::Ok(()) });
/// ```
pub fn try_status(&mut self) -> io::Result<Option<ExitStatus>> {
pub fn try_status(&self) -> io::Result<Option<ExitStatus>> {
self.child.lock().unwrap().get_mut().try_wait()
}

Expand All @@ -352,6 +352,30 @@ impl Child {
/// ```
pub fn status(&mut self) -> impl Future<Output = io::Result<ExitStatus>> {
self.stdin.take();
self.status_no_drop()
}

/// Waits for the process to exit.
///
/// Unlike `status`, does not drop the stdin handle. You are responsible
/// for avoiding deadlocks caused by the child blocking on stdin while the
/// parent blocks on waiting for the process to exit.
///
/// # Examples
///
/// ```no_run
/// # futures_lite::future::block_on(async {
/// use async_process::{Command, Stdio};
///
/// let child = Command::new("cp")
/// .arg("a.txt")
/// .arg("b.txt")
/// .spawn()?;
///
/// println!("exit status: {}", child.status_no_drop().await?);
/// # std::io::Result::Ok(()) });
/// ```
pub fn status_no_drop(&self) -> impl Future<Output = io::Result<ExitStatus>> {
let child = self.child.clone();

async move {
Expand Down