clonablechild

Crates.ioclonablechild
lib.rsclonablechild
version0.1.1
sourcesrc
created_at2016-12-23 16:36:14.060627
updated_at2017-01-05 13:33:48.492744
descriptionExtends and wraps `std::process::Child` to make it clonable
homepagehttps://github.com/faern/clonablechild
repositoryhttps://github.com/faern/clonablechild
max_upload_size
id7749
size25,662
Linus Färnstrand (faern)

documentation

https://docs.rs/clonablechild/

README

ClonableChild

Extends and wraps std::process::Child to make it clonable. Thus eliminating the problem that libstd does not have a cross platfrom and simple way to kill a child while also waiting for it.

Getting Started

Add the dependency to your Cargo.toml:

[dependencies]
clonablechild = "0.1"

Example

Use it in your program to kill a sleep process before it terminates naturally:

fn main() {
    // This command is specific to unix systems. See tests for Windows examples.
    let child = Command::new("sleep").arg("10").spawn().unwrap();
    let clonable_child = child.into_clonable();

    kill_async(clonable_child.clone());
    let exit_status = clonable_child.wait().unwrap();

    // Assert child was killed by a signal and did not exit cleanly
    assert_eq!(None, exit_status.code());
    assert!(!exit_status.success());
}

fn kill_async(child: ClonableChild) {
    thread::spawn(move || {
        thread::sleep(Duration::new(1, 0));
        child.kill().expect("Expected to be able to kill subprocess");
    });
}
Commit count: 16

cargo fmt