]> git.lizzy.rs Git - rust.git/blobdiff - src/libstd/io/process.rs
std: Support consuming a Process without waiting
[rust.git] / src / libstd / io / process.rs
index 38d8475ddf7a3550ab058f1775f428cb671ac749..1dcf08b2322bbf802a03d79ae0f1a992a3327868 100644 (file)
@@ -59,6 +59,7 @@
 /// ```
 pub struct Process {
     handle: Box<RtioProcess + Send>,
+    forget: bool,
 
     /// Handle to the child's stdin, if the `stdin` field of this process's
     /// `ProcessConfig` was `CreatePipe`. By default, this handle is `Some`.
@@ -262,6 +263,7 @@ fn to_rtio(p: StdioContainer) -> rtio::StdioContainer {
                 });
                 Process {
                     handle: p,
+                    forget: false,
                     stdin: io.next().unwrap(),
                     stdout: io.next().unwrap(),
                     stderr: io.next().unwrap(),
@@ -540,10 +542,23 @@ fn read(stream: Option<io::PipeStream>) -> Receiver<IoResult<Vec<u8>>> {
             error:  stderr.recv().ok().unwrap_or(Vec::new()),
         })
     }
+
+    /// Forgets this process, allowing it to outlive the parent
+    ///
+    /// This function will forcefully prevent calling `wait()` on the child
+    /// process in the destructor, allowing the child to outlive the
+    /// parent. Note that this operation can easily lead to leaking the
+    /// resources of the child process, so care must be taken when
+    /// invoking this method.
+    pub fn forget(mut self) {
+        self.forget = true;
+    }
 }
 
 impl Drop for Process {
     fn drop(&mut self) {
+        if self.forget { return }
+
         // Close all I/O before exiting to ensure that the child doesn't wait
         // forever to print some text or something similar.
         drop(self.stdin.take());
@@ -933,4 +948,12 @@ pub fn sleeper() -> Process {
         rx.recv();
         rx.recv();
     })
+
+    iotest!(fn forget() {
+        let p = sleeper();
+        let id = p.id();
+        p.forget();
+        assert!(Process::kill(id, 0).is_ok());
+        assert!(Process::kill(id, PleaseExitSignal).is_ok());
+    })
 }