Skip to content

Commit 4c842c0

Browse files
committed
Mv os-specific trait impl of Pipe* into std::os::*
Signed-off-by: Jiahao XU <[email protected]>
1 parent 7acb0e3 commit 4c842c0

File tree

12 files changed

+239
-208
lines changed

12 files changed

+239
-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

+43-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
77
use crate::marker::PhantomData;
88
use crate::mem::ManuallyDrop;
99
#[cfg(not(any(target_arch = "wasm32", target_env = "sgx", target_os = "hermit")))]
10-
use crate::sys::cvt;
10+
use crate::sys::{cvt, fd::FileDesc};
1111
use crate::sys_common::{AsInner, FromInner, IntoInner};
1212
use crate::{fmt, fs, io};
1313

@@ -484,3 +484,45 @@ impl<'a> AsFd for io::StderrLock<'a> {
484484
unsafe { BorrowedFd::borrow_raw(2) }
485485
}
486486
}
487+
488+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
489+
impl AsFd for io::PipeReader {
490+
fn as_fd(&self) -> BorrowedFd<'_> {
491+
self.0.as_fd()
492+
}
493+
}
494+
495+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
496+
impl From<io::PipeReader> for OwnedFd {
497+
fn from(pipe: io::PipeReader) -> Self {
498+
FileDesc::into_inner(pipe.0)
499+
}
500+
}
501+
502+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
503+
impl AsFd for io::PipeWriter {
504+
fn as_fd(&self) -> BorrowedFd<'_> {
505+
self.0.as_fd()
506+
}
507+
}
508+
509+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
510+
impl From<io::PipeWriter> for OwnedFd {
511+
fn from(pipe: io::PipeWriter) -> Self {
512+
FileDesc::into_inner(pipe.0)
513+
}
514+
}
515+
516+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
517+
impl From<OwnedFd> for io::PipeReader {
518+
fn from(owned_fd: OwnedFd) -> Self {
519+
Self(FileDesc::from_inner(owned_fd))
520+
}
521+
}
522+
523+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
524+
impl From<OwnedFd> for io::PipeWriter {
525+
fn from(owned_fd: OwnedFd) -> Self {
526+
Self(FileDesc::from_inner(owned_fd))
527+
}
528+
}

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

+43
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::os::unix::io::AsFd;
1515
use crate::os::unix::io::OwnedFd;
1616
#[cfg(target_os = "wasi")]
1717
use crate::os::wasi::io::OwnedFd;
18+
use crate::sys::fd::FileDesc;
1819
use crate::sys_common::{AsInner, IntoInner};
1920
use crate::{fs, io};
2021

@@ -276,3 +277,45 @@ impl<T: AsRawFd> AsRawFd for Box<T> {
276277
(**self).as_raw_fd()
277278
}
278279
}
280+
281+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
282+
impl AsRawFd for io::PipeReader {
283+
fn as_raw_fd(&self) -> RawFd {
284+
self.0.as_raw_fd()
285+
}
286+
}
287+
288+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
289+
impl FromRawFd for io::PipeReader {
290+
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
291+
unsafe { Self(FileDesc::from_raw_fd(raw_fd)) }
292+
}
293+
}
294+
295+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
296+
impl IntoRawFd for io::PipeReader {
297+
fn into_raw_fd(self) -> RawFd {
298+
self.0.into_raw_fd()
299+
}
300+
}
301+
302+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
303+
impl AsRawFd for io::PipeWriter {
304+
fn as_raw_fd(&self) -> RawFd {
305+
self.0.as_raw_fd()
306+
}
307+
}
308+
309+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
310+
impl FromRawFd for io::PipeWriter {
311+
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
312+
unsafe { Self(FileDesc::from_raw_fd(raw_fd)) }
313+
}
314+
}
315+
316+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
317+
impl IntoRawFd for io::PipeWriter {
318+
fn into_raw_fd(self) -> RawFd {
319+
self.0.into_raw_fd()
320+
}
321+
}

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
@@ -1658,6 +1658,20 @@ impl From<io::Stderr> for Stdio {
16581658
}
16591659
}
16601660

1661+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
1662+
impl From<io::PipeWriter> for Stdio {
1663+
fn from(pipe: io::PipeWriter) -> Self {
1664+
Stdio::from_inner(pipe.into_inner().into())
1665+
}
1666+
}
1667+
1668+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
1669+
impl From<io::PipeReader> for Stdio {
1670+
fn from(pipe: io::PipeReader) -> Self {
1671+
Stdio::from_inner(pipe.into_inner().into())
1672+
}
1673+
}
1674+
16611675
/// Describes the result of a process after it has terminated.
16621676
///
16631677
/// 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)