use std::io;
use std::os::windows::io::{AsRawHandle, BorrowedHandle};
use windows::Win32::Foundation::{HANDLE, WAIT_OBJECT_0, WAIT_TIMEOUT};
use windows::Win32::System::Threading::WaitForSingleObject;
use super::Target;
use crate::os_process::child::{ExitRecord, PeekOutcome};
fn handle<'a>(target: &Target<'a>) -> BorrowedHandle<'a> {
match target {
Target::Handle(h) => *h,
}
}
pub(crate) fn exit_status(h: BorrowedHandle<'_>) -> io::Result<std::process::ExitStatus> {
crate::child::spawn::windows_raw::exit_status(HANDLE(h.as_raw_handle()))
}
pub(super) fn peek(target: &Target<'_>) -> io::Result<PeekOutcome> {
let h = handle(target);
let r = unsafe { WaitForSingleObject(HANDLE(h.as_raw_handle()), 0) };
if r == WAIT_OBJECT_0 {
Ok(PeekOutcome::Exit(ExitRecord::Status(exit_status(h)?)))
} else if r == WAIT_TIMEOUT {
Ok(PeekOutcome::Running)
} else {
let e = io::Error::last_os_error();
debug_assert!(
false,
"WaitForSingleObject on our own process handle returned {r:?}: {e}"
);
Err(e)
}
}