diff --git a/src/child_context.rs b/src/child_context.rs index 9d8b456..1d0b9dc 100644 --- a/src/child_context.rs +++ b/src/child_context.rs @@ -18,8 +18,8 @@ use crate::OutputContext; /// /// See: [`OutputContext`]. pub struct ChildContext { - pub(crate) child: C, - pub(crate) command: Box, + child: C, + command: Box, } impl ChildContext { @@ -47,6 +47,19 @@ impl ChildContext { pub fn command(&self) -> &(dyn CommandDisplay + Send + Sync + 'static) { self.command.borrow() } + + /// Get a reference to the [`Box`] containing the command which produced this child process. + /// + /// This value, unlike the return value from [`ChildContext::command`], can be [`Clone`]d. + #[expect(clippy::borrowed_box)] + pub fn command_boxed(&self) -> &Box { + &self.command + } + + /// Get the child and command which produced the child. + pub fn into_child_and_command(self) -> (C, Box) { + (self.child, self.command) + } } impl Debug for ChildContext diff --git a/src/child_ext.rs b/src/child_ext.rs index a0333d1..df6f602 100644 --- a/src/child_ext.rs +++ b/src/child_ext.rs @@ -1,4 +1,3 @@ -use std::borrow::Borrow; use std::fmt::Debug; use std::fmt::Display; use std::process::Child; @@ -234,8 +233,8 @@ impl ChildExt for ChildContext { E: From, { self.log()?; - let command = dyn_clone::clone_box(self.command.borrow()); - match self.child.wait_with_output() { + let (child, command) = self.into_child_and_command(); + match child.wait_with_output() { Ok(output) => match output.try_into() { Ok(output) => succeeded(OutputContext::new(output, command)), Err(error) => { @@ -253,8 +252,8 @@ impl ChildExt for ChildContext { where E: From, { - let command = dyn_clone::clone_box(self.command.borrow()); - match self.child.try_wait() { + let command = self.command_boxed().clone(); + match self.child_mut().try_wait() { Ok(status) => succeeded(TryWaitContext::new(status, command)), Err(inner) => Err(Error::from(WaitError::new(command, inner)).into()), } @@ -268,8 +267,8 @@ impl ChildExt for ChildContext { E: From, { self.log()?; - let command = dyn_clone::clone_box(self.command.borrow()); - match self.child.wait() { + let command = self.command_boxed().clone(); + match self.child_mut().wait() { Ok(status) => succeeded(OutputContext::new(status, command)), Err(inner) => Err(Error::from(ExecError::new(command, inner)).into()), }