Struct pwner::tokio::Duplex

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

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

All read and write operations are async.

Note: On *nix platforms, the owned process will have 2 seconds between signals, which is run in a spawned task.

Panics

When, on *nix platforms, a process gets dropped without a runtime.

Implementations

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

Examples

Basic usage:

use tokio::process::Command;
use pwner::Spawner;

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

Choose which pipe to read form next.

Examples

Basic usage:

use tokio::io::AsyncReadExt;
use tokio::process::Command;
use pwner::Spawner;
use pwner::tokio::ReadSource;

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

child.read_from(ReadSource::Both).read(&mut buffer).await.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 tokio::io::{AsyncReadExt, BufReader};
use tokio::process::Command;

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

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

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

Decomposes the handle into mutable references to the pipes.

Examples

Basic usage:

use tokio::io::{ AsyncReadExt, AsyncWriteExt };
use tokio::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").await.unwrap();
stdout.read(&mut buffer).await.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 tokio::io::{ AsyncReadExt, AsyncWriteExt };
use tokio::process::Command;
use pwner::Spawner;

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

// Spawn printing task
tokio::spawn(async move {
    let mut buffer = [0; 1024];
    while let Ok(bytes) = output.read(&mut buffer).await {
        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").await.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 tokio::io::{ AsyncReadExt, AsyncWriteExt };
use tokio::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").await.unwrap();
stdout.read(&mut buffer).await.unwrap();

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

Consumes the process to allow awaiting for shutdown.

This method is essentially the same as a drop, however it return a Future which allows the parent to await the shutdown.

Examples

Basic usage:

use tokio::process::Command;
use pwner::Spawner;

let child = Command::new("top").spawn_owned().unwrap();
child.shutdown().await.unwrap();
Errors

Trait Implementations

Attempts to read from the AsyncRead into buf. Read more
Attempt to write bytes from buf into the object. Read more
Attempts to flush the object, ensuring that any buffered data reach their destination. Read more
Initiates or attempts to shut down this writer, returning success when the I/O connection has completely shut down. Read more
Like poll_write, except that it writes from a slice of buffers. Read more
Determines if this writer has an efficient poll_write_vectored implementation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Creates a new AsyncRead instance that chains this stream with next. Read more
Pulls some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Pulls some bytes from this source into the specified buffer, advancing the buffer’s internal cursor. Read more
Reads the exact number of bytes required to fill buf. Read more
Reads an unsigned 8 bit integer from the underlying reader. Read more
Reads a signed 8 bit integer from the underlying reader. Read more
Reads an unsigned 16-bit integer in big-endian order from the underlying reader. Read more
Reads a signed 16-bit integer in big-endian order from the underlying reader. Read more
Reads an unsigned 32-bit integer in big-endian order from the underlying reader. Read more
Reads a signed 32-bit integer in big-endian order from the underlying reader. Read more
Reads an unsigned 64-bit integer in big-endian order from the underlying reader. Read more
Reads an signed 64-bit integer in big-endian order from the underlying reader. Read more
Reads an unsigned 128-bit integer in big-endian order from the underlying reader. Read more
Reads an signed 128-bit integer in big-endian order from the underlying reader. Read more
Reads an 32-bit floating point type in big-endian order from the underlying reader. Read more
Reads an 64-bit floating point type in big-endian order from the underlying reader. Read more
Reads an unsigned 16-bit integer in little-endian order from the underlying reader. Read more
Reads a signed 16-bit integer in little-endian order from the underlying reader. Read more
Reads an unsigned 32-bit integer in little-endian order from the underlying reader. Read more
Reads a signed 32-bit integer in little-endian order from the underlying reader. Read more
Reads an unsigned 64-bit integer in little-endian order from the underlying reader. Read more
Reads an signed 64-bit integer in little-endian order from the underlying reader. Read more
Reads an unsigned 128-bit integer in little-endian order from the underlying reader. Read more
Reads an signed 128-bit integer in little-endian order from the underlying reader. Read more
Reads an 32-bit floating point type in little-endian order from the underlying reader. Read more
Reads an 64-bit floating point type in little-endian order from the underlying reader. Read more
Reads all bytes until EOF in this source, placing them into buf. Read more
Reads all bytes until EOF in this source, appending them to buf. Read more
Creates an adaptor which reads at most limit bytes from it. Read more
Writes a buffer into this writer, returning how many bytes were written. Read more
Like write, except that it writes from a slice of buffers. Read more
Writes a buffer into this writer, advancing the buffer’s internal cursor. Read more
Attempts to write an entire buffer into this writer. Read more
Attempts to write an entire buffer into this writer. Read more
Writes an unsigned 8-bit integer to the underlying writer. Read more
Writes an unsigned 8-bit integer to the underlying writer. Read more
Writes an unsigned 16-bit integer in big-endian order to the underlying writer. Read more
Writes a signed 16-bit integer in big-endian order to the underlying writer. Read more
Writes an unsigned 32-bit integer in big-endian order to the underlying writer. Read more
Writes a signed 32-bit integer in big-endian order to the underlying writer. Read more
Writes an unsigned 64-bit integer in big-endian order to the underlying writer. Read more
Writes an signed 64-bit integer in big-endian order to the underlying writer. Read more
Writes an unsigned 128-bit integer in big-endian order to the underlying writer. Read more
Writes an signed 128-bit integer in big-endian order to the underlying writer. Read more
Writes an 32-bit floating point type in big-endian order to the underlying writer. Read more
Writes an 64-bit floating point type in big-endian order to the underlying writer. Read more
Writes an unsigned 16-bit integer in little-endian order to the underlying writer. Read more
Writes a signed 16-bit integer in little-endian order to the underlying writer. Read more
Writes an unsigned 32-bit integer in little-endian order to the underlying writer. Read more
Writes a signed 32-bit integer in little-endian order to the underlying writer. Read more
Writes an unsigned 64-bit integer in little-endian order to the underlying writer. Read more
Writes an signed 64-bit integer in little-endian order to the underlying writer. Read more
Writes an unsigned 128-bit integer in little-endian order to the underlying writer. Read more
Writes an signed 128-bit integer in little-endian order to the underlying writer. Read more
Writes an 32-bit floating point type in little-endian order to the underlying writer. Read more
Writes an 64-bit floating point type in little-endian order to the underlying writer. Read more
Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
Shuts down the output stream, ensuring that the value can be dropped cleanly. 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.