Skip to content

Commit

Permalink
wip: add with_new for ergonomics
Browse files Browse the repository at this point in the history
  • Loading branch information
passcod committed Mar 9, 2024
1 parent 3c1b517 commit 397ed9e
Show file tree
Hide file tree
Showing 3 changed files with 327 additions and 26 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ process-wrap = "6.0.0"
use std::process::Command;
use process_wrap::std::*;

let mut child = StdCommandWrap::new(Command::new("watch").arg("ls"))
let mut child = StdCommandWrap::with_new("watch", |command| { command.arg("ls"); })
.wrap(ProcessGroup::leader())
.spawn()?;
let status = child.wait()?;
Expand All @@ -51,7 +51,7 @@ dbg!(status);
use tokio::process::Command;
use process_wrap::tokio::*;

let mut child = TokioCommandWrap::new(Command::new("watch").arg("ls"))
let mut child = TokioCommandWrap::with_new("watch", |command| { command.arg("ls"); })
.wrap(ProcessGroup::leader())
.spawn()?;
let status = child.wait().await?;
Expand All @@ -64,7 +64,7 @@ dbg!(status);
use tokio::process::Command;
use process_wrap::tokio::*;

let mut child = TokioCommandWrap::new(Command::new("watch").arg("ls"))
let mut child = TokioCommandWrap::with_new("watch", |command| { command.arg("ls"); })
.wrap(JobObject::new())
.spawn()?;
let status = child.wait().await?;
Expand All @@ -77,7 +77,7 @@ dbg!(status);
use tokio::process::Command;
use process_wrap::tokio::*;

let mut child = TokioCommandWrap::new(Command::new("watch").arg("ls"))
let mut child = TokioCommandWrap::with_new("watch", |command| { command.arg("ls"); })
.wrap(ProcessSession::leader())
.spawn()?;
let status = child.wait().await?;
Expand All @@ -90,7 +90,7 @@ dbg!(status);
use tokio::process::Command;
use process_wrap::tokio::*;

let mut child = TokioCommandWrap::new(Command::new("watch").arg("ls"))
let mut child = TokioCommandWrap::with_new("watch", |command| { command.arg("ls"); })
.wrap(ProcessSession)
.wrap(KillOnDrop)
.spawn()?;
Expand All @@ -113,15 +113,15 @@ dbg!(status);
- Feature: `process-group` (default)

```rust
TokioCommandWrap::new(Command::new("watch").arg("ls"))
TokioCommandWrap::with_new("watch", |command| { command.arg("ls"); })
.wrap(ProcessGroup::leader())
.spawn()?;
```

Or join a different group instead:

```rust
TokioCommandWrap::new(Command::new("watch").arg("ls"))
TokioCommandWrap::with_new("watch", |command| { command.arg("ls"); })
.wrap(ProcessGroup::attach_to(pgid))
.spawn()?;
```
Expand All @@ -138,7 +138,7 @@ This combines creating a new session and a new group, and setting this process a
To join the session from another process, use `ProcessGroup::attach_to()` instead.

```rust
TokioCommandWrap::new(Command::new("watch").arg("ls"))
TokioCommandWrap::with_new("watch", |command| { command.arg("ls"); })
.wrap(ProcessSession)
.spawn()?;
```
Expand All @@ -159,7 +159,7 @@ This is a shim to allow setting Windows process creation flags with this API, as
Note the `CREATE_SUSPENDED` will always be set, as it is required for the crate to function.

```rust
TokioCommandWrap::new(Command::new("watch").arg("ls"))
TokioCommandWrap::with_new("watch", |command| { command.arg("ls"); })
.wrap(CreationFlags::NO_WINDOW | CreationFlags::DETACHED)
.wrap(JobObject::new())
.spawn()?;
Expand All @@ -168,7 +168,7 @@ TokioCommandWrap::new(Command::new("watch").arg("ls"))
Or with custom flags:

```rust
TokioCommandWrap::new(Command::new("watch").arg("ls"))
TokioCommandWrap::with_new("watch", |command| { command.arg("ls"); })
.wrap(CreationFlags::custom(CREATE_NEW_CONSOLE | CREATE_PROTECTED_PROCESS))
.wrap(JobObject::new())
.spawn()?;
Expand All @@ -183,7 +183,7 @@ TokioCommandWrap::new(Command::new("watch").arg("ls"))
This is a shim to allow wrappers to handle the kill-on-drop flag, as it can't be read from Command.

```rust
let child = TokioCommandWrap::new(Command::new("watch").arg("ls"))
let child = TokioCommandWrap::with_new("watch", |command| { command.arg("ls"); })
.wrap(KillOnDrop)
.wrap(ProcessGroup::leader())
.spawn()?;
Expand Down
21 changes: 16 additions & 5 deletions src/tokio/core.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
any::TypeId,
ffi::OsStr,
future::Future,
io::{Error, Result},
process::ExitStatus,
Expand All @@ -20,7 +21,9 @@ pub struct TokioCommandWrap {
}

impl TokioCommandWrap {
pub fn new(command: Command) -> Self {
pub fn with_new(program: impl AsRef<OsStr>, init: impl FnOnce(&mut Command)) -> Self {
let mut command = Command::new(program);
init(&mut command);
Self {
command,
wrappers: IndexMap::new(),
Expand All @@ -41,14 +44,13 @@ impl TokioCommandWrap {
self
}

pub fn spawn(mut self) -> Result<Box<dyn TokioChildWrapper>> {
let mut command = self.command;
pub fn spawn(&mut self) -> Result<Box<dyn TokioChildWrapper>> {
for (id, wrapper) in &mut self.wrappers {
debug!(?id, "pre_spawn");
wrapper.pre_spawn(&mut command)?;
wrapper.pre_spawn(&mut self.command)?;
}

let mut child = command.spawn()?;
let mut child = self.command.spawn()?;
for (id, wrapper) in &mut self.wrappers {
debug!(?id, "post_spawn");
wrapper.post_spawn(&mut child)?;
Expand All @@ -64,6 +66,15 @@ impl TokioCommandWrap {
}
}

impl From<Command> for TokioCommandWrap {
fn from(command: Command) -> Self {
Self {
command,
wrappers: IndexMap::new(),
}
}
}

pub trait TokioCommandWrapper: std::fmt::Debug {
// process-wrap guarantees that `other` will be of the same type as `self`
// note that other crates that may use this trait should guarantee this, but
Expand Down
Loading

0 comments on commit 397ed9e

Please sign in to comment.