Struct Child

Source
pub struct Child { /* private fields */ }
Expand description

Wraps std::process::Child, kills and waits for the process on Drop. It will log the fact that std::process::Child::kill() was called in Drop. You should use Child::wait() for the process to finish with any of the available methods if you want to handle the error, otherwise it will be ignored.

Beware that Child holds an invariant that is not propagated to the type system. The invariant is that if Child was not spawned via Cmd::spawn_piped(), then any methods that read the child’s stdout will panic.

Implementations§

Source§

impl Child

Source

pub fn cmd(&self) -> Cmd

Returns the Cmd that was originally used to create this Child

Source

pub fn wait(&mut self) -> Result<()>

Waits for the process to finish. Returns an error if the process has finished with non-zero exit code.

You should use this method for processes spawned via Cmd::spawn() since the output of the command won’t be read and returned, but just written to this process’s stdout (as stdout is inherited with Cmd::spawn())

Source

pub fn read_bytes(self) -> Result<Vec<u8>>

Same as Child::read() but reads any bytes sequence from the child process stdout.

§Panics

Same as for Child::read().

Source

pub fn read(self) -> Result<String>

Waits for the process to finish and returns all that it has written to stdout. Returns an error if the process has finished with non-zero exit code. Expects a valid utf8 bytes sequence (since it returns a Rust String), if the process is not guaranteed to output valid utf8 you might want to use Child::read_bytes() instead.

If Cmd::log_cmd() has been set to some log::Level then prints captured output via log crate.

§Panics

Panics if the process was spawned with non-piped stdout. This method is expected to be used only for processes spawned via Cmd::spawn_piped().

Source

pub fn read_no_wait(&mut self, ostream: Ostream) -> Result<String>

Same as Child::read(), but doesn’t wait for the process to finish and doesn’t take the exit status of the process into account.

Source

pub fn read_bytes_no_wait(&mut self, ostream: Ostream) -> Result<Vec<u8>>

Same as Child::read_no_wait(), but reads raw bytes.

Source

pub fn stdout_lines(&mut self) -> impl Iterator<Item = String> + '_

Returns an iterator over the lines of data output to stdout by the child process. Beware that the iterator buffers the output, thus when the it is dropped the buffered data will be discarded and following reads won’t restore it. The returned line of output is logged via log crate according to Cmd::log_cmd() configuration.

§Panics

Panics if some std::io::Error happens during the reading. All invariants from Child::read_bytes() apply here too.

Trait Implementations§

Source§

impl Display for Child

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for Child

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for Child

§

impl RefUnwindSafe for Child

§

impl Send for Child

§

impl Sync for Child

§

impl Unpin for Child

§

impl UnwindSafe for Child

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, 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.