Struct devela::_std::process::Stdio

1.0.0 · source ·
pub struct Stdio(/* private fields */);
Expand description

Describes what to do with a standard I/O stream for a child process when passed to the stdin, stdout, and stderr methods of Command.

Implementations§

source§

impl Stdio

source

pub fn piped() -> Stdio

A new pipe should be arranged to connect the parent and child processes.

Examples

With stdout:

use std::process::{Command, Stdio};

let output = Command::new("echo")
    .arg("Hello, world!")
    .stdout(Stdio::piped())
    .output()
    .expect("Failed to execute command");

assert_eq!(String::from_utf8_lossy(&output.stdout), "Hello, world!\n");
// Nothing echoed to console

With stdin:

use std::io::Write;
use std::process::{Command, Stdio};

let mut child = Command::new("rev")
    .stdin(Stdio::piped())
    .stdout(Stdio::piped())
    .spawn()
    .expect("Failed to spawn child process");

let mut stdin = child.stdin.take().expect("Failed to open stdin");
std::thread::spawn(move || {
    stdin.write_all("Hello, world!".as_bytes()).expect("Failed to write to stdin");
});

let output = child.wait_with_output().expect("Failed to read stdout");
assert_eq!(String::from_utf8_lossy(&output.stdout), "!dlrow ,olleH");

Writing more than a pipe buffer’s worth of input to stdin without also reading stdout and stderr at the same time may cause a deadlock. This is an issue when running any program that doesn’t guarantee that it reads its entire stdin before writing more than a pipe buffer’s worth of output. The size of a pipe buffer varies on different targets.

source

pub fn inherit() -> Stdio

The child inherits from the corresponding parent descriptor.

Examples

With stdout:

use std::process::{Command, Stdio};

let output = Command::new("echo")
    .arg("Hello, world!")
    .stdout(Stdio::inherit())
    .output()
    .expect("Failed to execute command");

assert_eq!(String::from_utf8_lossy(&output.stdout), "");
// "Hello, world!" echoed to console

With stdin:

use std::process::{Command, Stdio};
use std::io::{self, Write};

let output = Command::new("rev")
    .stdin(Stdio::inherit())
    .stdout(Stdio::piped())
    .output()
    .expect("Failed to execute command");

print!("You piped in the reverse of: ");
io::stdout().write_all(&output.stdout).unwrap();
source

pub fn null() -> Stdio

This stream will be ignored. This is the equivalent of attaching the stream to /dev/null.

Examples

With stdout:

use std::process::{Command, Stdio};

let output = Command::new("echo")
    .arg("Hello, world!")
    .stdout(Stdio::null())
    .output()
    .expect("Failed to execute command");

assert_eq!(String::from_utf8_lossy(&output.stdout), "");
// Nothing echoed to console

With stdin:

use std::process::{Command, Stdio};

let output = Command::new("rev")
    .stdin(Stdio::null())
    .stdout(Stdio::piped())
    .output()
    .expect("Failed to execute command");

assert_eq!(String::from_utf8_lossy(&output.stdout), "");
// Ignores any piped-in input
source

pub fn makes_pipe(&self) -> bool

🔬This is a nightly-only experimental API. (stdio_makes_pipe)

Returns true if this requires Command to create a new pipe.

Example
#![feature(stdio_makes_pipe)]
use std::process::Stdio;

let io = Stdio::piped();
assert_eq!(io.makes_pipe(), true);

Trait Implementations§

1.16.0 · source§

impl Debug for Stdio

source§

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

Formats the value using the given formatter. Read more
1.20.0 · source§

impl From<ChildStderr> for Stdio

source§

fn from(child: ChildStderr) -> Stdio

Converts a ChildStderr into a Stdio.

Examples
use std::process::{Command, Stdio};

let reverse = Command::new("rev")
    .arg("non_existing_file.txt")
    .stderr(Stdio::piped())
    .spawn()
    .expect("failed reverse command");

let cat = Command::new("cat")
    .arg("-")
    .stdin(reverse.stderr.unwrap()) // Converted into a Stdio here
    .output()
    .expect("failed echo command");

assert_eq!(
    String::from_utf8_lossy(&cat.stdout),
    "rev: cannot open non_existing_file.txt: No such file or directory\n"
);
1.20.0 · source§

impl From<ChildStdin> for Stdio

source§

fn from(child: ChildStdin) -> Stdio

Converts a ChildStdin into a Stdio.

Examples

ChildStdin will be converted to Stdio using Stdio::from under the hood.

use std::process::{Command, Stdio};

let reverse = Command::new("rev")
    .stdin(Stdio::piped())
    .spawn()
    .expect("failed reverse command");

let _echo = Command::new("echo")
    .arg("Hello, world!")
    .stdout(reverse.stdin.unwrap()) // Converted into a Stdio here
    .output()
    .expect("failed echo command");

// "!dlrow ,olleH" echoed to console
1.20.0 · source§

impl From<ChildStdout> for Stdio

source§

fn from(child: ChildStdout) -> Stdio

Converts a ChildStdout into a Stdio.

Examples

ChildStdout will be converted to Stdio using Stdio::from under the hood.

use std::process::{Command, Stdio};

let hello = Command::new("echo")
    .arg("Hello, world!")
    .stdout(Stdio::piped())
    .spawn()
    .expect("failed echo command");

let reverse = Command::new("rev")
    .stdin(hello.stdout.unwrap())  // Converted into a Stdio here
    .output()
    .expect("failed reverse command");

assert_eq!(reverse.stdout, b"!dlrow ,olleH\n");
1.20.0 · source§

impl From<File> for Stdio

source§

fn from(file: File) -> Stdio

Converts a File into a Stdio.

Examples

File will be converted to Stdio using Stdio::from under the hood.

use std::fs::File;
use std::process::Command;

// With the `foo.txt` file containing "Hello, world!"
let file = File::open("foo.txt").unwrap();

let reverse = Command::new("rev")
    .stdin(file)  // Implicit File conversion into a Stdio
    .output()
    .expect("failed reverse command");

assert_eq!(reverse.stdout, b"!dlrow ,olleH");
1.63.0 · source§

impl From<OwnedFd> for Stdio

source§

fn from(fd: OwnedFd) -> Stdio

Converts to this type from the input type.
1.74.0 · source§

impl From<Stderr> for Stdio

source§

fn from(inherit: Stderr) -> Stdio

Redirect command stdout/stderr to our stderr

Examples
#![feature(exit_status_error)]
use std::io;
use std::process::Command;

let output = Command::new("whoami")
    .stdout(io::stderr())
    .output()?;
output.status.exit_ok()?;
assert!(output.stdout.is_empty());
1.74.0 · source§

impl From<Stdout> for Stdio

source§

fn from(inherit: Stdout) -> Stdio

Redirect command stdout/stderr to our stdout

Examples
#![feature(exit_status_error)]
use std::io;
use std::process::Command;

let output = Command::new("whoami")
    .stdout(io::stdout())
    .output()?;
output.status.exit_ok()?;
assert!(output.stdout.is_empty());
1.2.0 · source§

impl FromRawFd for Stdio

source§

unsafe fn from_raw_fd(fd: i32) -> Stdio

Constructs a new instance of Self from the given raw file descriptor. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Stdio

§

impl Send for Stdio

§

impl Sync for Stdio

§

impl Unpin for Stdio

§

impl UnwindSafe for Stdio

Blanket Implementations§

source§

impl<T> Also for T

source§

fn also_mut<F: FnOnce(&mut Self)>(self, f: F) -> Self

Available on crate feature result only.
Applies a function which takes the parameter by exclusive reference, and then returns the (possibly) modified owned value. Read more
source§

fn also_ref<F: FnOnce(&Self)>(self, f: F) -> Self

Available on crate feature result only.
Applies a function which takes the parameter by shared reference, and then returns the (possibly) modified owned value. Read more
source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> AnyExt for Twhere T: Any,

source§

fn type_of(&self) -> TypeId

Available on crate feature any only.
Returns the TypeId of self. Read more
source§

fn type_name(&self) -> &'static str

Available on crate feature any only.
Returns the type name of self. Read more
source§

fn type_is<T: 'static>(&self) -> bool

Available on crate feature any only.
Returns true if Self is of type T. Read more
source§

fn as_any_ref(&self) -> &dyn Anywhere Self: Sized,

Available on crate feature any only.
Upcasts &self as &dyn Any. Read more
source§

fn as_any_mut(&mut self) -> &mut dyn Anywhere Self: Sized,

Available on crate feature any only.
Upcasts &mut self as &mut dyn Any. Read more
source§

fn as_any_box(self: Box<Self>) -> Box<dyn Any>where Self: Sized,

Available on crate feature any only.
Upcasts Box<self> as Box<dyn Any>. Read more
source§

impl<T, Res> Apply<Res> for Twhere T: ?Sized,

source§

fn apply<F: FnOnce(Self) -> Res>(self, f: F) -> Reswhere Self: Sized,

Available on crate feature result only.
Apply a function which takes the parameter by value.
source§

fn apply_ref<F: FnOnce(&Self) -> Res>(&self, f: F) -> Res

Available on crate feature result only.
Apply a function which takes the parameter by shared reference.
source§

fn apply_mut<F: FnOnce(&mut Self) -> Res>(&mut self, f: F) -> Res

Available on crate feature result only.
Apply a function which takes the parameter by exclusive reference.
source§

impl<T> Az for T

source§

fn az<Dst>(self) -> Dstwhere T: Cast<Dst>,

Casts the value.
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<Src, Dst> CastFrom<Src> for Dstwhere Src: Cast<Dst>,

source§

fn cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> CheckedAs for T

source§

fn checked_as<Dst>(self) -> Option<Dst>where T: CheckedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere Src: CheckedCast<Dst>,

source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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> Mem for Twhere T: ?Sized,

source§

const NEEDS_DROP: bool = _

Available on crate feature mem only.
Whether dropping values of this type matters.
source§

fn mem_needs_drop(&self) -> bool

Available on crate feature mem only.
Returns true if dropping values of this type matters.
source§

fn mem_drop(self)where Self: Sized,

Available on crate feature mem only.
Drops self by running its destructor.
source§

fn mem_forget(self)where Self: Sized,

Available on crate feature mem only.
Forgets about self without running its destructor.
source§

fn mem_replace(&mut self, other: Self) -> Selfwhere Self: Sized,

Available on crate feature mem only.
Replaces self with other, returning the previous value of self.
source§

fn mem_take(&mut self) -> Selfwhere Self: Default,

Available on crate feature mem only.
Replaces self with its default value, returning the previous value of self.
source§

fn mem_swap(&mut self, other: &mut Self)where Self: Sized,

Available on crate feature mem only.
Swaps the value of self and other without deinitializing either one.
source§

fn mem_as_bytes(&self) -> &[u8] where Self: Sync + Unpin,

Available on crate features mem and unsafe_mem only.
View a Sync + Unpin self as &[u8]. Read more
source§

fn mem_as_bytes_mut(&mut self) -> &mut [u8] where Self: Sync + Unpin,

Available on crate features mem and unsafe_mem only.
View a Sync + Unpin self as &mut [u8].
source§

impl<T> OverflowingAs for T

source§

fn overflowing_as<Dst>(self) -> (Dst, bool)where T: OverflowingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere Src: OverflowingCast<Dst>,

source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
source§

impl<T> SaturatingAs for T

source§

fn saturating_as<Dst>(self) -> Dstwhere T: SaturatingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere Src: SaturatingCast<Dst>,

source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> Size for T

source§

const BYTE_ALIGN: usize = _

Available on crate feature mem only.
The alignment of this type in bytes.
source§

const BYTE_SIZE: usize = _

Available on crate feature mem only.
The size of this type in bytes.
source§

const PTR_SIZE: usize = 4usize

Available on crate feature mem only.
The size of a pointer in bytes, for the current platform.
source§

fn byte_align(&self) -> usize

Available on crate feature mem only.
Returns the alignment of this type in bytes.
source§

fn byte_size(&self) -> usize

Available on crate feature mem only.
Returns the size of this type in bytes. Read more
source§

fn ptr_ratio(&self) -> (usize, usize)

Available on crate feature mem only.
Returns the size ratio between PTR_SIZE and BYTE_SIZE. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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> UnwrappedAs for T

source§

fn unwrapped_as<Dst>(self) -> Dstwhere T: UnwrappedCast<Dst>,

Casts the value.
source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dstwhere Src: UnwrappedCast<Dst>,

source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
source§

impl<T> WrappingAs for T

source§

fn wrapping_as<Dst>(self) -> Dstwhere T: WrappingCast<Dst>,

Casts the value.
source§

impl<Src, Dst> WrappingCastFrom<Src> for Dstwhere Src: WrappingCast<Dst>,

source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.