Skip to content

Commit 6863a99

Browse files
committed
Mv os-specific trait impl of Pipe* into std::os::*
Signed-off-by: Jiahao XU <[email protected]>
1 parent 340a452 commit 6863a99

File tree

12 files changed

+278
-208
lines changed

12 files changed

+278
-208
lines changed

library/std/src/io/pipe.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::io;
22
use crate::sys::anonymous_pipe::{AnonPipe, pipe as pipe_inner};
3+
use crate::sys_common::{FromInner, IntoInner};
34

45
/// Create an anonymous pipe.
56
///
@@ -82,6 +83,30 @@ pub struct PipeReader(pub(crate) AnonPipe);
8283
#[derive(Debug)]
8384
pub struct PipeWriter(pub(crate) AnonPipe);
8485

86+
impl FromInner<AnonPipe> for PipeReader {
87+
fn from_inner(inner: AnonPipe) -> Self {
88+
Self(inner)
89+
}
90+
}
91+
92+
impl IntoInner<AnonPipe> for PipeReader {
93+
fn into_inner(self) -> AnonPipe {
94+
self.0
95+
}
96+
}
97+
98+
impl FromInner<AnonPipe> for PipeWriter {
99+
fn from_inner(inner: AnonPipe) -> Self {
100+
Self(inner)
101+
}
102+
}
103+
104+
impl IntoInner<AnonPipe> for PipeWriter {
105+
fn into_inner(self) -> AnonPipe {
106+
self.0
107+
}
108+
}
109+
85110
impl PipeReader {
86111
/// Create a new [`PipeReader`] instance that shares the same underlying file description.
87112
///

library/std/src/os/fd/owned.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ use crate::mem::ManuallyDrop;
1515
target_os = "trusty"
1616
)))]
1717
use crate::sys::cvt;
18+
use crate::sys_common::FromInner;
1819
#[cfg(not(target_os = "trusty"))]
19-
use crate::sys_common::{AsInner, FromInner, IntoInner};
20+
use crate::sys_common::{AsInner, IntoInner};
2021
use crate::{fmt, io};
2122

2223
type ValidRawFd = core::num::niche_types::NotAllOnes<RawFd>;
@@ -504,3 +505,45 @@ impl<'a> AsFd for io::StderrLock<'a> {
504505
unsafe { BorrowedFd::borrow_raw(2) }
505506
}
506507
}
508+
509+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
510+
impl AsFd for io::PipeReader {
511+
fn as_fd(&self) -> BorrowedFd<'_> {
512+
self.0.as_fd()
513+
}
514+
}
515+
516+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
517+
impl From<io::PipeReader> for OwnedFd {
518+
fn from(pipe: io::PipeReader) -> Self {
519+
pipe.0.into_inner()
520+
}
521+
}
522+
523+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
524+
impl AsFd for io::PipeWriter {
525+
fn as_fd(&self) -> BorrowedFd<'_> {
526+
self.0.as_fd()
527+
}
528+
}
529+
530+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
531+
impl From<io::PipeWriter> for OwnedFd {
532+
fn from(pipe: io::PipeWriter) -> Self {
533+
pipe.0.into_inner()
534+
}
535+
}
536+
537+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
538+
impl From<OwnedFd> for io::PipeReader {
539+
fn from(owned_fd: OwnedFd) -> Self {
540+
Self(FromInner::from_inner(owned_fd))
541+
}
542+
}
543+
544+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
545+
impl From<OwnedFd> for io::PipeWriter {
546+
fn from(owned_fd: OwnedFd) -> Self {
547+
Self(FromInner::from_inner(owned_fd))
548+
}
549+
}

library/std/src/os/fd/raw.rs

+43
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::os::unix::io::AsFd;
1818
use crate::os::unix::io::OwnedFd;
1919
#[cfg(target_os = "wasi")]
2020
use crate::os::wasi::io::OwnedFd;
21+
use crate::sys_common::FromInner;
2122
#[cfg(not(target_os = "trusty"))]
2223
use crate::sys_common::{AsInner, IntoInner};
2324

@@ -284,3 +285,45 @@ impl<T: AsRawFd> AsRawFd for Box<T> {
284285
(**self).as_raw_fd()
285286
}
286287
}
288+
289+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
290+
impl AsRawFd for io::PipeReader {
291+
fn as_raw_fd(&self) -> RawFd {
292+
self.0.as_raw_fd()
293+
}
294+
}
295+
296+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
297+
impl FromRawFd for io::PipeReader {
298+
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
299+
Self::from_inner(unsafe { FromRawFd::from_raw_fd(raw_fd) })
300+
}
301+
}
302+
303+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
304+
impl IntoRawFd for io::PipeReader {
305+
fn into_raw_fd(self) -> RawFd {
306+
self.0.into_raw_fd()
307+
}
308+
}
309+
310+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
311+
impl AsRawFd for io::PipeWriter {
312+
fn as_raw_fd(&self) -> RawFd {
313+
self.0.as_raw_fd()
314+
}
315+
}
316+
317+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
318+
impl FromRawFd for io::PipeWriter {
319+
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
320+
Self::from_inner(unsafe { FromRawFd::from_raw_fd(raw_fd) })
321+
}
322+
}
323+
324+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
325+
impl IntoRawFd for io::PipeWriter {
326+
fn into_raw_fd(self) -> RawFd {
327+
self.0.into_raw_fd()
328+
}
329+
}

library/std/src/os/windows/io/handle.rs

+42
Original file line numberDiff line numberDiff line change
@@ -660,3 +660,45 @@ impl<T> From<crate::thread::JoinHandle<T>> for OwnedHandle {
660660
join_handle.into_inner().into_handle().into_inner()
661661
}
662662
}
663+
664+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
665+
impl AsHandle for io::PipeReader {
666+
fn as_handle(&self) -> BorrowedHandle<'_> {
667+
self.0.as_handle()
668+
}
669+
}
670+
671+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
672+
impl From<io::PipeReader> for OwnedHandle {
673+
fn from(pipe: io::PipeReader) -> Self {
674+
pipe.into_inner().into_inner()
675+
}
676+
}
677+
678+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
679+
impl AsHandle for io::PipeWriter {
680+
fn as_handle(&self) -> BorrowedHandle<'_> {
681+
self.0.as_handle()
682+
}
683+
}
684+
685+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
686+
impl From<io::PipeWriter> for OwnedHandle {
687+
fn from(pipe: io::PipeWriter) -> Self {
688+
pipe.into_inner().into_inner()
689+
}
690+
}
691+
692+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
693+
impl From<OwnedHandle> for io::PipeReader {
694+
fn from(owned_handle: OwnedHandle) -> Self {
695+
Self::from_inner(FromInner::from_inner(owned_handle))
696+
}
697+
}
698+
699+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
700+
impl From<OwnedHandle> for io::PipeWriter {
701+
fn from(owned_handle: OwnedHandle) -> Self {
702+
Self::from_inner(FromInner::from_inner(owned_handle))
703+
}
704+
}

library/std/src/os/windows/io/raw.rs

+42
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,45 @@ impl IntoRawSocket for net::UdpSocket {
310310
self.into_inner().into_socket().into_inner().into_raw_socket()
311311
}
312312
}
313+
314+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
315+
impl AsRawHandle for io::PipeReader {
316+
fn as_raw_handle(&self) -> RawHandle {
317+
self.0.as_raw_handle()
318+
}
319+
}
320+
321+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
322+
impl FromRawHandle for io::PipeReader {
323+
unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self {
324+
unsafe { Self::from_inner(FromRawHandle::from_raw_handle(raw_handle)) }
325+
}
326+
}
327+
328+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
329+
impl IntoRawHandle for io::PipeReader {
330+
fn into_raw_handle(self) -> RawHandle {
331+
self.0.into_raw_handle()
332+
}
333+
}
334+
335+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
336+
impl AsRawHandle for io::PipeWriter {
337+
fn as_raw_handle(&self) -> RawHandle {
338+
self.0.as_raw_handle()
339+
}
340+
}
341+
342+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
343+
impl FromRawHandle for io::PipeWriter {
344+
unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self {
345+
unsafe { Self::from_inner(FromRawHandle::from_raw_handle(raw_handle)) }
346+
}
347+
}
348+
349+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
350+
impl IntoRawHandle for io::PipeWriter {
351+
fn into_raw_handle(self) -> RawHandle {
352+
self.0.into_raw_handle()
353+
}
354+
}

library/std/src/process.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,20 @@ impl From<io::Stderr> for Stdio {
16591659
}
16601660
}
16611661

1662+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
1663+
impl From<io::PipeWriter> for Stdio {
1664+
fn from(pipe: io::PipeWriter) -> Self {
1665+
Stdio::from_inner(pipe.into_inner().into())
1666+
}
1667+
}
1668+
1669+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
1670+
impl From<io::PipeReader> for Stdio {
1671+
fn from(pipe: io::PipeReader) -> Self {
1672+
Stdio::from_inner(pipe.into_inner().into())
1673+
}
1674+
}
1675+
16621676
/// Describes the result of a process after it has terminated.
16631677
///
16641678
/// This `struct` is used to represent the exit status or other termination of a child process.
+2-92
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,11 @@
1-
use crate::io::{self, PipeReader, PipeWriter};
2-
use crate::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
3-
use crate::process::Stdio;
1+
use crate::io;
42
use crate::sys::fd::FileDesc;
53
use crate::sys::pipe::anon_pipe;
6-
use crate::sys_common::{FromInner, IntoInner};
4+
use crate::sys_common::IntoInner;
75

86
pub type AnonPipe = FileDesc;
97

108
#[inline]
119
pub fn pipe() -> io::Result<(AnonPipe, AnonPipe)> {
1210
anon_pipe().map(|(rx, wx)| (rx.into_inner(), wx.into_inner()))
1311
}
14-
15-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
16-
impl AsFd for PipeReader {
17-
fn as_fd(&self) -> BorrowedFd<'_> {
18-
self.0.as_fd()
19-
}
20-
}
21-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
22-
impl AsRawFd for PipeReader {
23-
fn as_raw_fd(&self) -> RawFd {
24-
self.0.as_raw_fd()
25-
}
26-
}
27-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
28-
impl From<PipeReader> for OwnedFd {
29-
fn from(pipe: PipeReader) -> Self {
30-
FileDesc::into_inner(pipe.0)
31-
}
32-
}
33-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
34-
impl FromRawFd for PipeReader {
35-
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
36-
unsafe { Self(FileDesc::from_raw_fd(raw_fd)) }
37-
}
38-
}
39-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
40-
impl IntoRawFd for PipeReader {
41-
fn into_raw_fd(self) -> RawFd {
42-
self.0.into_raw_fd()
43-
}
44-
}
45-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
46-
impl From<PipeReader> for Stdio {
47-
fn from(pipe: PipeReader) -> Self {
48-
Self::from(OwnedFd::from(pipe))
49-
}
50-
}
51-
52-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
53-
impl AsFd for PipeWriter {
54-
fn as_fd(&self) -> BorrowedFd<'_> {
55-
self.0.as_fd()
56-
}
57-
}
58-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
59-
impl AsRawFd for PipeWriter {
60-
fn as_raw_fd(&self) -> RawFd {
61-
self.0.as_raw_fd()
62-
}
63-
}
64-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
65-
impl From<PipeWriter> for OwnedFd {
66-
fn from(pipe: PipeWriter) -> Self {
67-
FileDesc::into_inner(pipe.0)
68-
}
69-
}
70-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
71-
impl FromRawFd for PipeWriter {
72-
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
73-
unsafe { Self(FileDesc::from_raw_fd(raw_fd)) }
74-
}
75-
}
76-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
77-
impl IntoRawFd for PipeWriter {
78-
fn into_raw_fd(self) -> RawFd {
79-
self.0.into_raw_fd()
80-
}
81-
}
82-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
83-
impl From<PipeWriter> for Stdio {
84-
fn from(pipe: PipeWriter) -> Self {
85-
Self::from(OwnedFd::from(pipe))
86-
}
87-
}
88-
89-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
90-
impl From<OwnedFd> for PipeReader {
91-
fn from(owned_fd: OwnedFd) -> Self {
92-
Self(FileDesc::from_inner(owned_fd))
93-
}
94-
}
95-
96-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
97-
impl From<OwnedFd> for PipeWriter {
98-
fn from(owned_fd: OwnedFd) -> Self {
99-
Self(FileDesc::from_inner(owned_fd))
100-
}
101-
}
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,7 @@
1-
use crate::io::{self, PipeReader, PipeWriter};
2-
use crate::process::Stdio;
1+
use crate::io;
32
pub use crate::sys::pipe::AnonPipe;
43

54
#[inline]
65
pub fn pipe() -> io::Result<(AnonPipe, AnonPipe)> {
76
Err(io::Error::UNSUPPORTED_PLATFORM)
87
}
9-
10-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
11-
impl From<PipeReader> for Stdio {
12-
fn from(pipe: PipeReader) -> Self {
13-
pipe.0.diverge()
14-
}
15-
}
16-
17-
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
18-
impl From<PipeWriter> for Stdio {
19-
fn from(pipe: PipeWriter) -> Self {
20-
pipe.0.diverge()
21-
}
22-
}

0 commit comments

Comments
 (0)