std::process (windows): refactor Stdio::to_handle slightly

We're going to want to reuse this bit of code.
This commit is contained in:
Ian Jackson 2023-01-03 15:01:19 +00:00
parent fb7f1a8127
commit deda7d3301

View File

@ -521,17 +521,18 @@ fn program_exists(path: &Path) -> Option<Vec<u16>> {
impl Stdio {
fn to_handle(&self, stdio_id: c::DWORD, pipe: &mut Option<AnonPipe>) -> io::Result<Handle> {
match *self {
Stdio::Inherit => match stdio::get_handle(stdio_id) {
Ok(io) => unsafe {
let io = Handle::from_raw_handle(io);
let ret = io.duplicate(0, true, c::DUPLICATE_SAME_ACCESS);
io.into_raw_handle();
ret
},
// If no stdio handle is available, then propagate the null value.
Err(..) => unsafe { Ok(Handle::from_raw_handle(ptr::null_mut())) },
let use_stdio_id = |stdio_id| match stdio::get_handle(stdio_id) {
Ok(io) => unsafe {
let io = Handle::from_raw_handle(io);
let ret = io.duplicate(0, true, c::DUPLICATE_SAME_ACCESS);
io.into_raw_handle();
ret
},
// If no stdio handle is available, then propagate the null value.
Err(..) => unsafe { Ok(Handle::from_raw_handle(ptr::null_mut())) },
};
match *self {
Stdio::Inherit => use_stdio_id(stdio_id),
Stdio::MakePipe => {
let ours_readable = stdio_id != c::STD_INPUT_HANDLE;