Implement AsRawHandle on Child on Windows

This commit is contained in:
Félix Saparelli 2021-07-31 18:43:36 +12:00
parent bc9719e64a
commit d91b557db3
2 changed files with 34 additions and 1 deletions

View File

@ -1,8 +1,9 @@
//! Windows-specific extensions.
use std::os::windows::io::{AsRawHandle, RawHandle};
use std::os::windows::process::CommandExt as _;
use crate::Command;
use crate::{Child, Command};
/// Windows-specific extensions to the [`Command`] builder.
pub trait CommandExt {
@ -20,3 +21,9 @@ impl CommandExt for Command {
self
}
}
impl AsRawHandle for Child {
fn as_raw_handle(&self) -> RawHandle {
self.child.lock().unwrap().get_mut().as_raw_handle()
}
}

View File

@ -381,3 +381,29 @@ fn child_status_preserved_with_kill_on_drop() {
assert!(res.unwrap().status.success());
})
}
#[test]
#[cfg(windows)]
fn child_as_raw_handle() {
use std::os::windows::io::AsRawHandle;
use winapi::um::processthreadsapi::GetProcessId;
future::block_on(async {
let p = Command::new("cmd.exe")
.arg("/C")
.arg("pause")
.kill_on_drop(true)
.spawn()
.unwrap();
let std_pid = p.id();
assert!(std_pid > 0);
let handle = p.as_raw_handle();
// We verify that we have the correct handle by obtaining the PID
// with the Windows API rather than via std:
let win_pid = unsafe { GetProcessId(handle as _) };
assert_eq!(win_pid, std_pid);
})
}