Struct pwner::process::Duplex

source ·
pub struct Duplex(_, _);
Expand description

An implementation of Process that uses std::process as the launcher.

All read and write operations are sync.

Note: On *nix platforms, the owned process will have 2 seconds between signals, which is a blocking wait.

Implementations

Returns the OS-assigned process identifier associated with this child.

Examples

Basic usage:

use std::process::Command;
use pwner::Spawner;

let mut command = Command::new("ls");
if let Ok(child) = command.spawn_owned() {
    println!("Child's ID is {}", child.id());
} else {
    println!("ls command didn't start");
}

Choose which pipe to read form next.

Examples

Basic usage:

use std::io::Read;
use std::process::Command;
use pwner::Spawner;
use pwner::process::ReadSource;

let mut child = Command::new("ls").spawn_owned().unwrap();
let mut buffer = [0_u8; 1024];
child.read_from(ReadSource::Stdout).read(&mut buffer).unwrap();

Waits for the child to exit completely, returning the status with which it exited, stdout, and stderr.

The stdin handle to the child process, if any, will be closed before waiting. This helps avoid deadlock: it ensures that the child does not block waiting for input from the parent, while the parent waits for the child to exit.

Examples

Basic usage:

use pwner::Spawner;
use std::io::{BufReader, Read};
use std::process::Command;

let child = Command::new("ls").spawn_owned().unwrap();
let (status, stdout, stderr) = child.wait().unwrap();

let mut buffer = String::new();
if status.success() {
    let mut reader = BufReader::new(stdout);
    reader.read_to_string(&mut buffer).unwrap();
} else {
    let mut reader = BufReader::new(stderr);
    reader.read_to_string(&mut buffer).unwrap();
}
Errors

Relays the error from std::process::Child::wait()

Decomposes the handle into mutable references to the pipes.

Examples

Basic usage:

use std::io::{ Read, Write };
use std::process::Command;
use pwner::Spawner;

let mut child = Command::new("cat").spawn_owned().unwrap();
let mut buffer = [0_u8; 1024];
let (stdin, stdout, _) = child.pipes();

stdin.write_all(b"hello\n").unwrap();
stdout.read(&mut buffer).unwrap();

Separates the process and its input from the output pipes. Ownership is retained by a Simplex which still implements a graceful drop of the child process.

Examples

Basic usage:

use std::io::{Read, Write};
use std::process::Command;
use pwner::Spawner;

let child = Command::new("cat").spawn_owned().unwrap();
let (mut input_only_process, mut output) = child.decompose();

// Spawn printing thread
std::thread::spawn(move || {
    let mut buffer = [0; 1024];
    while let Ok(bytes) = output.read(&mut buffer) {
        if let Ok(string) = std::str::from_utf8(&buffer[..bytes]) {
            print!("{}", string);
        }
    }
});

// Interact normally with the child process
input_only_process.write_all(b"hello\n").unwrap();

Completely releases the ownership of the child process. The raw underlying process and pipes are returned and no wrapping function is applicable any longer.

Note: By ejecting the process, graceful drop will no longer be available.

Examples

Basic usage:

use std::io::{ Read, Write };
use std::process::Command;
use pwner::Spawner;

let mut child = Command::new("cat").spawn_owned().unwrap();
let mut buffer = [0_u8; 1024];
let (process, mut stdin, mut stdout, _) = child.eject();

stdin.write_all(b"hello\n").unwrap();
stdout.read(&mut buffer).unwrap();

// Graceful drop will not be executed for `child` as the ejected variable leaves scope here

Trait Implementations

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Like read, except that it reads into a slice of buffers. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
Read all bytes until EOF in this source, placing them into buf. Read more
Read all bytes until EOF in this source, appending them to buf. Read more
Read the exact number of bytes required to fill buf. Read more
🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
Creates a “by reference” adaptor for this instance of Read. Read more
Transforms this Read instance to an Iterator over its bytes. Read more
Creates an adapter which will chain this stream with another. Read more
Creates an adapter which will read at most limit bytes from it. Read more
Write a buffer into this writer, returning how many bytes were written. Read more
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
Like write, except that it writes from a slice of buffers. Read more
🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
Attempts to write an entire buffer into this writer. Read more
🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
Writes a formatted string into this writer, returning any error encountered. Read more
Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.