Skip to content

Commit aef4ffe

Browse files
authored
ChildContext: Add command_boxed (#37)
This lets us make the `ChildContext` fields private, making it possible for users to implement (e.g.) `ChildExt`.
1 parent 57f6796 commit aef4ffe

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

src/child_context.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use crate::OutputContext;
1818
///
1919
/// See: [`OutputContext`].
2020
pub struct ChildContext<C> {
21-
pub(crate) child: C,
22-
pub(crate) command: Box<dyn CommandDisplay + Send + Sync>,
21+
child: C,
22+
command: Box<dyn CommandDisplay + Send + Sync>,
2323
}
2424

2525
impl<C> ChildContext<C> {
@@ -47,6 +47,19 @@ impl<C> ChildContext<C> {
4747
pub fn command(&self) -> &(dyn CommandDisplay + Send + Sync + 'static) {
4848
self.command.borrow()
4949
}
50+
51+
/// Get a reference to the [`Box`] containing the command which produced this child process.
52+
///
53+
/// This value, unlike the return value from [`ChildContext::command`], can be [`Clone`]d.
54+
#[expect(clippy::borrowed_box)]
55+
pub fn command_boxed(&self) -> &Box<dyn CommandDisplay + Send + Sync> {
56+
&self.command
57+
}
58+
59+
/// Get the child and command which produced the child.
60+
pub fn into_child_and_command(self) -> (C, Box<dyn CommandDisplay + Send + Sync>) {
61+
(self.child, self.command)
62+
}
5063
}
5164

5265
impl<C> Debug for ChildContext<C>

src/child_ext.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::borrow::Borrow;
21
use std::fmt::Debug;
32
use std::fmt::Display;
43
use std::process::Child;
@@ -234,8 +233,8 @@ impl ChildExt for ChildContext<Child> {
234233
E: From<Self::Error>,
235234
{
236235
self.log()?;
237-
let command = dyn_clone::clone_box(self.command.borrow());
238-
match self.child.wait_with_output() {
236+
let (child, command) = self.into_child_and_command();
237+
match child.wait_with_output() {
239238
Ok(output) => match output.try_into() {
240239
Ok(output) => succeeded(OutputContext::new(output, command)),
241240
Err(error) => {
@@ -253,8 +252,8 @@ impl ChildExt for ChildContext<Child> {
253252
where
254253
E: From<Self::Error>,
255254
{
256-
let command = dyn_clone::clone_box(self.command.borrow());
257-
match self.child.try_wait() {
255+
let command = self.command_boxed().clone();
256+
match self.child_mut().try_wait() {
258257
Ok(status) => succeeded(TryWaitContext::new(status, command)),
259258
Err(inner) => Err(Error::from(WaitError::new(command, inner)).into()),
260259
}
@@ -268,8 +267,8 @@ impl ChildExt for ChildContext<Child> {
268267
E: From<Self::Error>,
269268
{
270269
self.log()?;
271-
let command = dyn_clone::clone_box(self.command.borrow());
272-
match self.child.wait() {
270+
let command = self.command_boxed().clone();
271+
match self.child_mut().wait() {
273272
Ok(status) => succeeded(OutputContext::new(status, command)),
274273
Err(inner) => Err(Error::from(ExecError::new(command, inner)).into()),
275274
}

0 commit comments

Comments
 (0)