Skip to content

Commit 4f0ff19

Browse files
committed
Implement TryFrom<std::process::Child> for tokio::process::Child
Currently, you can turn a `std::process::Command` into a `tokio::process::Command`, however, if you have a `std::process::Child` there is no way to turn it into a `tokio::process::Child`. This provides a mechanism. ## Context The use case I'm after is this: I have a small crate for running commands in Rust. I was wondering what a `tokio` feature might look like when I hit a bit of a snag. I can't find a reliable way to convert a `std::process:Child` into something `await`-able. The primary interface is this trait https://docs.rs/fun_run/0.6.0/fun_run/trait.CommandWithName.html. It is implemented for `Command` and `&mut Command`. If I want to add a feature flagged `async fn spawn` for `tokio::process::Command`, then I also need a way to reproduce the same interface in my other already-existing implementations. My thought was that for the default implementation, I can produce a regular `std::process::Child` and then, if I can turn it into a `tokio::process::Child`. Then I can standardize on that interface.
1 parent 2440d11 commit 4f0ff19

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

tokio/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ This fixes a regression on the wasm32-unknown-unknown target, where code that
44
previously did not panic due to calls to `Instant::now()` started failing. This
55
is due to the stabilization of the first time-based metric.
66

7+
### Added
8+
9+
- process: Implement `TryFrom<std::process::Child>` for `tokio::process::Child` ([#NNNN])
10+
11+
[#NNNN]: https://github.com/tokio-rs/tokio/pull/NNNN
12+
713
### Fixed
814

915
- Disable time-based metrics on wasm32-unknown-unknown ([#7322])

tokio/src/process/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,24 @@ pub struct Child {
12121212
pub stderr: Option<ChildStderr>,
12131213
}
12141214

1215+
impl TryFrom<StdChild> for Child {
1216+
type Error = io::Error;
1217+
1218+
fn try_from(value: StdChild) -> io::Result<Self> {
1219+
let spawned_child = imp::build_child(value)?;
1220+
1221+
Ok(Child {
1222+
child: FusedChild::Child(ChildDropGuard {
1223+
inner: spawned_child.child,
1224+
kill_on_drop: false,
1225+
}),
1226+
stdin: spawned_child.stdin.map(|inner| ChildStdin { inner }),
1227+
stdout: spawned_child.stdout.map(|inner| ChildStdout { inner }),
1228+
stderr: spawned_child.stderr.map(|inner| ChildStderr { inner }),
1229+
})
1230+
}
1231+
}
1232+
12151233
impl Child {
12161234
/// Returns the OS-assigned process identifier associated with this child
12171235
/// while it is still running.

0 commit comments

Comments
 (0)