diff options
| author | Jiahao XU <Jiahao_XU@outlook.com> | 2024-06-30 18:23:07 +1000 |
|---|---|---|
| committer | Jiahao XU <Jiahao_XU@outlook.com> | 2024-07-23 23:13:56 +1000 |
| commit | c9c8a14884c19e51a0eee54ccd98efa7f0f2bddd (patch) | |
| tree | 1b94b0d4990f0771508383ea979d012270473e53 /library/std/src/sys/pal/windows/pipe.rs | |
| parent | 52f3c71c8dc4aaed71e3035995fcbdd6d78c98c6 (diff) | |
| download | rust-c9c8a14884c19e51a0eee54ccd98efa7f0f2bddd.tar.gz rust-c9c8a14884c19e51a0eee54ccd98efa7f0f2bddd.zip | |
Initial implementation of anonymous_pipe
Co-authored-by: Alphyr <47725341+a1phyr@users.noreply.github.com> Co-authored-by: Jubilee <46493976+workingjubilee@users.noreply.github.com> Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
Diffstat (limited to 'library/std/src/sys/pal/windows/pipe.rs')
| -rw-r--r-- | library/std/src/sys/pal/windows/pipe.rs | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/library/std/src/sys/pal/windows/pipe.rs b/library/std/src/sys/pal/windows/pipe.rs index 7a309b9638b..d5e2356116f 100644 --- a/library/std/src/sys/pal/windows/pipe.rs +++ b/library/std/src/sys/pal/windows/pipe.rs @@ -1,7 +1,7 @@ use crate::os::windows::prelude::*; use crate::ffi::OsStr; -use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, Read}; +use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut}; use crate::mem; use crate::path::Path; use crate::ptr; @@ -39,6 +39,23 @@ pub struct Pipes { pub theirs: AnonPipe, } +/// Create true unnamed anonymous pipe. +pub fn unnamed_anon_pipe() -> io::Result<(AnonPipe, AnonPipe)> { + let mut read_pipe = c::INVALID_HANDLE_VALUE; + let mut write_pipe = c::INVALID_HANDLE_VALUE; + + let ret = unsafe { c::CreatePipe(&mut read_pipe, &mut write_pipe, ptr::null_mut(), 0) }; + + if ret == 0 { + Err(io::Error::last_os_error()) + } else { + Ok(( + AnonPipe::from_inner(unsafe { Handle::from_raw_handle(read_pipe) }), + AnonPipe::from_inner(unsafe { Handle::from_raw_handle(write_pipe) }), + )) + } +} + /// Although this looks similar to `anon_pipe` in the Unix module it's actually /// subtly different. Here we'll return two pipes in the `Pipes` return value, /// but one is intended for "us" where as the other is intended for "someone @@ -181,7 +198,7 @@ pub fn spawn_pipe_relay( their_handle_inheritable: bool, ) -> io::Result<AnonPipe> { // We need this handle to live for the lifetime of the thread spawned below. - let source = source.duplicate()?; + let source = source.try_clone()?; // create a new pair of anon pipes. let Pipes { theirs, ours } = anon_pipe(ours_readable, their_handle_inheritable)?; @@ -237,7 +254,8 @@ impl AnonPipe { pub fn into_handle(self) -> Handle { self.inner } - fn duplicate(&self) -> io::Result<Self> { + + pub fn try_clone(&self) -> io::Result<Self> { self.inner.duplicate(0, false, c::DUPLICATE_SAME_ACCESS).map(|inner| AnonPipe { inner }) } |
