Skip to main content

Command

Struct Command 

Source
pub struct Command { /* private fields */ }
Available on crate feature tokio only.
Expand description

A command to run inside an asynchronous pseudoconsole session.

Mirrors std::process::Command: the builder methods take &mut self and return &mut Self, so a whole invocation can be written as one expression. The differences from the standard library are the ones a pseudoconsole forces:

  • There is no stdio configuration. The child’s console is the pseudoconsole; its standard handles are deliberately set to INVALID_HANDLE_VALUE so a redirected parent cannot leak its own stdio into the child.
  • No handles are inherited (bInheritHandles is FALSE), because a leaked copy of the output pipe would keep the session from ever reaching end-of-file.
  • The child and every descendant it creates join a job object, which is what makes Child::kill terminate the whole tree.

A command is intentionally not Clone: managed spawning must not copy or mutate its potentially large argument and environment buffers. Low-level lifecycle and unvalidated process flags are intentionally absent; hidden compile-fail doctests pin both boundaries.

Implementations§

Source§

impl Command

Source

pub fn new(program: impl AsRef<OsStr>) -> Self

Creates a builder for launching program.

The program is not resolved here; a missing executable surfaces as an error with crate::ErrorKind::Spawn and a std::io::ErrorKind::NotFound source.

Source

pub fn arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self

Appends one argument, quoted and escaped as the MSVC C runtime expects.

Source

pub fn args<I, S>(&mut self, args: I) -> &mut Self
where I: IntoIterator<Item = S>, S: AsRef<OsStr>,

Appends several arguments; equivalent to calling Command::arg for each one.

Source

pub fn raw_arg(&mut self, text: impl AsRef<OsStr>) -> &mut Self

Appends literal text to the command line, bypassing all quoting.

Same semantics as std::os::windows::process::CommandExt::raw_arg: intended for callees such as cmd.exe /c that parse the raw command line themselves.

Source

pub fn env( &mut self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>, ) -> &mut Self

Sets an environment variable for the child (case-insensitively, as Windows does).

Source

pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
where I: IntoIterator<Item = (K, V)>, K: AsRef<OsStr>, V: AsRef<OsStr>,

Sets several environment variables; equivalent to calling Command::env for each pair.

Source

pub fn env_remove(&mut self, key: impl AsRef<OsStr>) -> &mut Self

Removes an environment variable from the child’s environment.

Source

pub fn env_clear(&mut self) -> &mut Self

Clears the child’s environment, including modifications recorded so far. Variables set afterwards still apply.

Source

pub fn current_dir(&mut self, dir: impl AsRef<Path>) -> &mut Self

Sets the child’s working directory.

Source

pub fn spawn(&mut self) -> Result<Session>

Spawns a managed asynchronous session with default options.

This is synchronous because process creation itself does not block. The returned session owns a kill-on-close Job; dropping it before completion terminates the entire process tree.

§Errors

Returns an error when the backend or pipes cannot be initialized — including when no Tokio runtime with an enabled I/O driver is current, which surfaces as crate::ErrorKind::CreateConsole — or when the root process cannot be spawned.

Source

pub fn spawn_with(&mut self, options: SessionOptions) -> Result<Session>

Spawns a managed asynchronous session with explicit safe options.

§Errors

Returns an error when the selected backend or pipes cannot be initialized — including when no Tokio runtime with an enabled I/O driver is current, which surfaces as crate::ErrorKind::CreateConsole — or when the root process cannot be spawned.

Trait Implementations§

Source§

impl Debug for Command

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more