Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tokio::process::Command::as_std_mut() method. #6523

Open
Jisu-Woniu opened this issue Apr 30, 2024 · 6 comments · May be fixed by #6608
Open

Add tokio::process::Command::as_std_mut() method. #6523

Jisu-Woniu opened this issue Apr 30, 2024 · 6 comments · May be fixed by #6608
Labels
A-tokio Area: The main tokio crate C-feature-accepted Category: A feature request that has been accepted pending implementation. C-feature-request Category: A feature request. M-process Module: tokio/process

Comments

@Jisu-Woniu
Copy link

Jisu-Woniu commented Apr 30, 2024

Is your feature request related to a problem? Please describe.
Sometimes I want to access a mutable reference of std::process::Command out of the tokio counterpart, so I can access some unstable functions in std to update Command, but tokio only provides an as_std method returning a shared reference.

Describe the solution you'd like
Add a tokio::process::Command::as_std_mut() method, maybe like:

pub fn as_std_mut(&mut self) -> &mut StdCommand {
    &mut self.std
}

Describe alternatives you've considered
Implement From<tokio::process::Command> for std::process::Command? But we may lose information such as kill_on_drop.

Implement tokio::process::Command::groups() method. Seems like a better solution, but I may want to use other functions as well in the future.

Additional context
screenshot

@Jisu-Woniu Jisu-Woniu added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Apr 30, 2024
@Darksonn Darksonn added the M-process Module: tokio/process label Apr 30, 2024
@Darksonn
Copy link
Contributor

@ipetkov Thoughts on this?

@ipetkov
Copy link
Member

ipetkov commented May 1, 2024

Hi @Jisu-Woniu we already have a From impl to convert std::process::Command to tokio::process::Command which should do what you are asking for!

For example:

let mut cmd = std::process::Command::new("echo");
cmd.args(["hello", "world"]).groups(groups);

tokio::process::Command::from(cmd)
  .kill_on_drop(true)
  .spawn()
  .expect("failed to start")
  .await
  .expect("failed to run");

@Jisu-Woniu
Copy link
Author

Jisu-Woniu commented May 1, 2024

But I have a function taking a tokio &mut Command, and I'm trying to run .groups(xxx) on that.

So I was wondering: can I convert between tokio &mut Command to std &mut Command easily?

@Darksonn
Copy link
Contributor

Darksonn commented May 1, 2024

Biggest concern is that we prevent ourselves from making other changes in the future due to backwards compatibility, but since we already have the From impl and as_std, I don't think that's very likely. @ipetkov Thoughts?

@ipetkov
Copy link
Member

ipetkov commented May 1, 2024

My initial thought was that exposing as_std_mut() could allow the caller to make changes to the underlying Command without our version knowing. I thought we used to have logic whenever each method was called (particularly around stdio handling) but that doesn't seem to be the case (anymore?) since all the fd handling is done when we call spawn

Even if that wasn't the case, you could do pretty much anything you wanted to a std::process::Command and then use the From impl to convert it so we'd still be none-the-wiser. I guess exposing it could lead to less flexibility for us in the future, though these APIs have been stable for a few years without any major issues so it might not be a big deal...

(Another upside would be that callers can use the latest methods on std::process::Command without having to wait for tokio's MSRV to catch up or use --cfg tokio_unstable)

@Darksonn
Copy link
Contributor

Darksonn commented May 5, 2024

I think it's okay to add this.

@Darksonn Darksonn added the C-feature-accepted Category: A feature request that has been accepted pending implementation. label May 5, 2024
@tglane tglane linked a pull request Jun 2, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-feature-accepted Category: A feature request that has been accepted pending implementation. C-feature-request Category: A feature request. M-process Module: tokio/process
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants