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>
This commit is contained in:
parent
52f3c71c8d
commit
c9c8a14884
17 changed files with 551 additions and 16 deletions
18
library/std/src/sys/anonymous_pipe/mod.rs
Normal file
18
library/std/src/sys/anonymous_pipe/mod.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
cfg_if::cfg_if! {
|
||||
if #[cfg(unix)] {
|
||||
mod unix;
|
||||
pub(crate) use unix::{AnonPipe, pipe};
|
||||
|
||||
#[cfg(all(test, not(miri)))]
|
||||
mod tests;
|
||||
} else if #[cfg(windows)] {
|
||||
mod windows;
|
||||
pub(crate) use windows::{AnonPipe, pipe};
|
||||
|
||||
#[cfg(all(test, not(miri)))]
|
||||
mod tests;
|
||||
} else {
|
||||
mod unsupported;
|
||||
pub(crate) use unsupported::{AnonPipe, pipe};
|
||||
}
|
||||
}
|
||||
20
library/std/src/sys/anonymous_pipe/tests.rs
Normal file
20
library/std/src/sys/anonymous_pipe/tests.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
use crate::{
|
||||
io::{Read, Write},
|
||||
pipe::pipe,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn pipe_creation_clone_and_rw() {
|
||||
let (rx, tx) = pipe().unwrap();
|
||||
|
||||
tx.try_clone().unwrap().write_all(b"12345").unwrap();
|
||||
drop(tx);
|
||||
|
||||
let mut rx2 = rx.try_clone().unwrap();
|
||||
drop(rx);
|
||||
|
||||
let mut s = String::new();
|
||||
rx2.read_to_string(&mut s).unwrap();
|
||||
drop(rx2);
|
||||
assert_eq!(s, "12345");
|
||||
}
|
||||
103
library/std/src/sys/anonymous_pipe/unix.rs
Normal file
103
library/std/src/sys/anonymous_pipe/unix.rs
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
use crate::{
|
||||
io,
|
||||
os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd},
|
||||
pipe::{PipeReader, PipeWriter},
|
||||
process::Stdio,
|
||||
sys::{fd::FileDesc, pipe::anon_pipe},
|
||||
sys_common::{FromInner, IntoInner},
|
||||
};
|
||||
|
||||
pub(crate) type AnonPipe = FileDesc;
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn pipe() -> io::Result<(AnonPipe, AnonPipe)> {
|
||||
anon_pipe().map(|(rx, wx)| (rx.into_inner(), wx.into_inner()))
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl AsFd for PipeReader {
|
||||
fn as_fd(&self) -> BorrowedFd<'_> {
|
||||
self.0.as_fd()
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl AsRawFd for PipeReader {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.0.as_raw_fd()
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<PipeReader> for OwnedFd {
|
||||
fn from(pipe: PipeReader) -> Self {
|
||||
FileDesc::into_inner(pipe.0)
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl FromRawFd for PipeReader {
|
||||
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
|
||||
Self(FileDesc::from_raw_fd(raw_fd))
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl IntoRawFd for PipeReader {
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
self.0.into_raw_fd()
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<PipeReader> for Stdio {
|
||||
fn from(pipe: PipeReader) -> Self {
|
||||
Self::from(OwnedFd::from(pipe))
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl AsFd for PipeWriter {
|
||||
fn as_fd(&self) -> BorrowedFd<'_> {
|
||||
self.0.as_fd()
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl AsRawFd for PipeWriter {
|
||||
fn as_raw_fd(&self) -> RawFd {
|
||||
self.0.as_raw_fd()
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<PipeWriter> for OwnedFd {
|
||||
fn from(pipe: PipeWriter) -> Self {
|
||||
FileDesc::into_inner(pipe.0)
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl FromRawFd for PipeWriter {
|
||||
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
|
||||
Self(FileDesc::from_raw_fd(raw_fd))
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl IntoRawFd for PipeWriter {
|
||||
fn into_raw_fd(self) -> RawFd {
|
||||
self.0.into_raw_fd()
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<PipeWriter> for Stdio {
|
||||
fn from(pipe: PipeWriter) -> Self {
|
||||
Self::from(OwnedFd::from(pipe))
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<OwnedFd> for PipeReader {
|
||||
fn from(owned_fd: OwnedFd) -> Self {
|
||||
Self(FileDesc::from_inner(owned_fd))
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<OwnedFd> for PipeWriter {
|
||||
fn from(owned_fd: OwnedFd) -> Self {
|
||||
Self(FileDesc::from_inner(owned_fd))
|
||||
}
|
||||
}
|
||||
26
library/std/src/sys/anonymous_pipe/unsupported.rs
Normal file
26
library/std/src/sys/anonymous_pipe/unsupported.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
use crate::{
|
||||
io,
|
||||
pipe::{PipeReader, PipeWriter},
|
||||
process::Stdio,
|
||||
};
|
||||
|
||||
pub(crate) use crate::sys::pipe::AnonPipe;
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn pipe() -> io::Result<(AnonPipe, AnonPipe)> {
|
||||
Err(io::Error::UNSUPPORTED_PLATFORM)
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<PipeReader> for Stdio {
|
||||
fn from(pipe: PipeReader) -> Self {
|
||||
pipe.0.diverge()
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<PipeWriter> for Stdio {
|
||||
fn from(pipe: PipeWriter) -> Self {
|
||||
pipe.0.diverge()
|
||||
}
|
||||
}
|
||||
109
library/std/src/sys/anonymous_pipe/windows.rs
Normal file
109
library/std/src/sys/anonymous_pipe/windows.rs
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
use crate::{
|
||||
io,
|
||||
os::windows::io::{
|
||||
AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle,
|
||||
},
|
||||
pipe::{PipeReader, PipeWriter},
|
||||
process::Stdio,
|
||||
sys::{handle::Handle, pipe::unnamed_anon_pipe},
|
||||
sys_common::{FromInner, IntoInner},
|
||||
};
|
||||
|
||||
pub(crate) type AnonPipe = Handle;
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn pipe() -> io::Result<(AnonPipe, AnonPipe)> {
|
||||
unnamed_anon_pipe().map(|(rx, wx)| (rx.into_inner(), wx.into_inner()))
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl AsHandle for PipeReader {
|
||||
fn as_handle(&self) -> BorrowedHandle<'_> {
|
||||
self.0.as_handle()
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl AsRawHandle for PipeReader {
|
||||
fn as_raw_handle(&self) -> RawHandle {
|
||||
self.0.as_raw_handle()
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl FromRawHandle for PipeReader {
|
||||
unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self {
|
||||
Self(Handle::from_raw_handle(raw_handle))
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl IntoRawHandle for PipeReader {
|
||||
fn into_raw_handle(self) -> RawHandle {
|
||||
self.0.into_raw_handle()
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<PipeReader> for OwnedHandle {
|
||||
fn from(pipe: PipeReader) -> Self {
|
||||
Handle::into_inner(pipe.0)
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<PipeReader> for Stdio {
|
||||
fn from(pipe: PipeReader) -> Self {
|
||||
Self::from(OwnedHandle::from(pipe))
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl AsHandle for PipeWriter {
|
||||
fn as_handle(&self) -> BorrowedHandle<'_> {
|
||||
self.0.as_handle()
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl AsRawHandle for PipeWriter {
|
||||
fn as_raw_handle(&self) -> RawHandle {
|
||||
self.0.as_raw_handle()
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl FromRawHandle for PipeWriter {
|
||||
unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self {
|
||||
Self(Handle::from_raw_handle(raw_handle))
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl IntoRawHandle for PipeWriter {
|
||||
fn into_raw_handle(self) -> RawHandle {
|
||||
self.0.into_raw_handle()
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<PipeWriter> for OwnedHandle {
|
||||
fn from(pipe: PipeWriter) -> Self {
|
||||
Handle::into_inner(pipe.0)
|
||||
}
|
||||
}
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<PipeWriter> for Stdio {
|
||||
fn from(pipe: PipeWriter) -> Self {
|
||||
Self::from(OwnedHandle::from(pipe))
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<OwnedHandle> for PipeReader {
|
||||
fn from(owned_handle: OwnedHandle) -> Self {
|
||||
Self(Handle::from_inner(owned_handle))
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable(feature = "anonymous_pipe", issue = "127154")]
|
||||
impl From<OwnedHandle> for PipeWriter {
|
||||
fn from(owned_handle: OwnedHandle) -> Self {
|
||||
Self(Handle::from_inner(owned_handle))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue