use std::process::{Child, ExitStatus};
pub struct Reaped(Option<Child>);
#[allow(dead_code)] impl Reaped {
pub fn new(child: Child) -> Self {
Reaped(Some(child))
}
pub fn child(&mut self) -> &mut Child {
self.0.as_mut().expect("child already taken")
}
pub fn wait(mut self) -> std::io::Result<ExitStatus> {
let status = self.child().wait();
if status.is_ok() {
self.0 = None;
}
status
}
pub fn into_inner(mut self) -> Child {
self.0.take().expect("child already taken")
}
}
impl Drop for Reaped {
fn drop(&mut self) {
if let Some(mut c) = self.0.take() {
if matches!(c.try_wait(), Ok(None)) {
let _ = c.kill();
}
let _ = c.wait();
}
}
}