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
sourceimpl Duplex
impl Duplex
sourcepub fn id(&self) -> u32
pub fn id(&self) -> u32
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");
}
sourcepub fn read_from(&mut self, read_source: ReadSource) -> &mut Self
pub fn read_from(&mut self, read_source: ReadSource) -> &mut Self
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();
sourcepub fn wait(self) -> Result<(ExitStatus, ChildStdout, ChildStderr), Error>
pub fn wait(self) -> Result<(ExitStatus, ChildStdout, ChildStderr), Error>
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()
sourcepub fn pipes(&mut self) -> (&mut ChildStdin, &mut ChildStdout, &mut ChildStderr)
pub fn pipes(&mut self) -> (&mut ChildStdin, &mut ChildStdout, &mut ChildStderr)
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();
sourcepub fn decompose(self) -> (Simplex, Output)
pub fn decompose(self) -> (Simplex, Output)
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();
sourcepub fn eject(self) -> (Child, ChildStdin, ChildStdout, ChildStderr)
pub fn eject(self) -> (Child, ChildStdin, ChildStdout, ChildStderr)
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
sourceimpl Read for Duplex
impl Read for Duplex
sourcefn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · sourcefn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresourcefn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · sourcefn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
buf
. Read more1.0.0 · sourcefn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · sourcefn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresourcefn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)sourcefn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · sourcefn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moresourceimpl Write for Duplex
impl Write for Duplex
sourcefn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
sourcefn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
sourcefn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · sourcefn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
sourcefn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)