-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use std::{ | ||
io::Result, | ||
process::{Child, ExitStatus, Output}, | ||
}; | ||
|
||
use super::GroupChild; | ||
|
||
/// Wrapper around a process child, be it grouped or ungrouped. | ||
/// | ||
/// This is a helper which erases that a [`std::process::Child`] is a different type than a | ||
/// [`GroupChild`]. It forwards to the corresponding method on the inner type. | ||
#[derive(Debug)] | ||
pub enum ErasedChild { | ||
/// A grouped process child. | ||
Grouped(GroupChild), | ||
|
||
/// An ungrouped process child. | ||
Ungrouped(Child), | ||
} | ||
|
||
impl ErasedChild { | ||
/// Forces the child to exit. | ||
/// | ||
/// - Grouped: [`GroupChild::kill`] | ||
/// - Ungrouped: [`Child::kill`] | ||
pub fn kill(&mut self) -> Result<()> { | ||
match self { | ||
Self::Grouped(c) => c.kill(), | ||
Self::Ungrouped(c) => c.kill(), | ||
} | ||
} | ||
|
||
/// Attempts to collect the exit status of the child if it has already exited. | ||
/// | ||
/// - Grouped: [`GroupChild::try_wait`] | ||
/// - Ungrouped: [`Child::try_wait`] | ||
pub fn try_wait(&mut self) -> Result<Option<ExitStatus>> { | ||
match self { | ||
Self::Grouped(c) => c.try_wait(), | ||
Self::Ungrouped(c) => c.try_wait(), | ||
} | ||
} | ||
|
||
/// Waits for the process to exit, and returns its exit status. | ||
/// | ||
/// - Grouped: [`GroupChild::wait`] | ||
/// - Ungrouped: [`Child::wait`] | ||
pub fn wait(&mut self) -> Result<ExitStatus> { | ||
match self { | ||
Self::Grouped(c) => c.wait(), | ||
Self::Ungrouped(c) => c.wait(), | ||
} | ||
} | ||
|
||
/// Waits for the process to exit, and returns its exit status. | ||
/// | ||
/// - Grouped: [`GroupChild::wait_with_output`] | ||
/// - Ungrouped: [`Child::wait_with_output`] | ||
pub fn wait_with_output(self) -> Result<Output> { | ||
match self { | ||
Self::Grouped(c) => c.wait_with_output(), | ||
Self::Ungrouped(c) => c.wait_with_output(), | ||
} | ||
} | ||
|
||
/// Sends a Unix signal to the process. | ||
/// | ||
/// - Grouped: [`GroupChild::signal`] | ||
/// - Ungrouped: [`Child::signal`] | ||
#[cfg(unix)] | ||
pub fn signal(&mut self, sig: crate::Signal) -> Result<()> { | ||
use crate::UnixChildExt; | ||
|
||
match self { | ||
Self::Grouped(c) => c.signal(sig), | ||
Self::Ungrouped(c) => c.signal(sig), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use std::{ | ||
io::Result, | ||
process::{ExitStatus, Output}, | ||
}; | ||
|
||
use super::AsyncGroupChild; | ||
use tokio::process::Child; | ||
|
||
/// Wrapper around a process child, be it grouped or ungrouped. | ||
/// | ||
/// This is a helper which erases that a [`tokio::process::Child`] is a different type than an | ||
/// [`AsyncGroupChild`]. It forwards to the corresponding method on the inner type. | ||
#[derive(Debug)] | ||
pub enum ErasedChild { | ||
/// A grouped process child. | ||
Grouped(AsyncGroupChild), | ||
|
||
/// An ungrouped process child. | ||
Ungrouped(Child), | ||
} | ||
|
||
impl ErasedChild { | ||
/// Forces the child to exit. | ||
/// | ||
/// - Grouped: [`AsyncGroupChild::kill`] | ||
/// - Ungrouped: [`Child::kill`] | ||
pub async fn kill(&mut self) -> Result<()> { | ||
match self { | ||
Self::Grouped(c) => c.kill().await, | ||
Self::Ungrouped(c) => c.kill().await, | ||
} | ||
} | ||
|
||
/// Attempts to force the child to exit, but does not wait for the request to take effect. | ||
/// | ||
/// - Grouped: [`AsyncGroupChild::start_kill`] | ||
/// - Ungrouped: [`Child::start_kill`] | ||
pub fn start_kill(&mut self) -> Result<()> { | ||
match self { | ||
Self::Grouped(c) => c.start_kill(), | ||
Self::Ungrouped(c) => c.start_kill(), | ||
} | ||
} | ||
|
||
/// Attempts to collect the exit status of the child if it has already exited. | ||
/// | ||
/// - Grouped: [`AsyncGroupChild::try_wait`] | ||
/// - Ungrouped: [`Child::try_wait`] | ||
pub fn try_wait(&mut self) -> Result<Option<ExitStatus>> { | ||
match self { | ||
Self::Grouped(c) => c.try_wait(), | ||
Self::Ungrouped(c) => c.try_wait(), | ||
} | ||
} | ||
|
||
/// Waits for the process to exit, and returns its exit status. | ||
/// | ||
/// - Grouped: [`AsyncGroupChild::wait`] | ||
/// - Ungrouped: [`Child::wait`] | ||
pub async fn wait(&mut self) -> Result<ExitStatus> { | ||
match self { | ||
Self::Grouped(c) => c.wait().await, | ||
Self::Ungrouped(c) => c.wait().await, | ||
} | ||
} | ||
|
||
/// Waits for the process to exit, and returns its exit status. | ||
/// | ||
/// - Grouped: [`AsyncGroupChild::wait_with_output`] | ||
/// - Ungrouped: [`Child::wait_with_output`] | ||
pub async fn wait_with_output(self) -> Result<Output> { | ||
match self { | ||
Self::Grouped(c) => c.wait_with_output().await, | ||
Self::Ungrouped(c) => c.wait_with_output().await, | ||
} | ||
} | ||
|
||
/// Sends a Unix signal to the process. | ||
/// | ||
/// - Grouped: [`AsyncGroupChild::signal`] | ||
/// - Ungrouped: [`Child::signal`] | ||
#[cfg(unix)] | ||
pub fn signal(&mut self, sig: crate::Signal) -> Result<()> { | ||
use crate::UnixChildExt; | ||
|
||
match self { | ||
Self::Grouped(c) => c.signal(sig), | ||
Self::Ungrouped(c) => c.signal(sig), | ||
} | ||
} | ||
} |