Skip to content

Commit 0778f24

Browse files
committed
Move trait impls for Pipe{Reader,Writer} to std::io::pipe
Also get rid of the now mostly-empty `std::sys::anonymous_pipe` module. It basically just unwrapped the `AnonPipe` abstraction from `std::sys::pipe`.
1 parent 604faa6 commit 0778f24

File tree

16 files changed

+315
-292
lines changed

16 files changed

+315
-292
lines changed

library/std/src/io/pipe.rs

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

45
/// Create an anonymous pipe.
56
///
@@ -66,18 +67,23 @@ use crate::sys::anonymous_pipe::{AnonPipe, pipe as pipe_inner};
6667
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
6768
#[inline]
6869
pub fn pipe() -> io::Result<(PipeReader, PipeWriter)> {
69-
pipe_inner().map(|(reader, writer)| (PipeReader(reader), PipeWriter(writer)))
70+
anon_pipe()
71+
.map(|(reader, writer)| (PipeReader::from_inner(reader), PipeWriter::from_inner(writer)))
7072
}
7173

7274
/// Read end of an anonymous pipe.
7375
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
7476
#[derive(Debug)]
75-
pub struct PipeReader(pub(crate) AnonPipe);
77+
pub struct PipeReader {
78+
inner: AnonPipe,
79+
}
7680

7781
/// Write end of an anonymous pipe.
7882
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
7983
#[derive(Debug)]
80-
pub struct PipeWriter(pub(crate) AnonPipe);
84+
pub struct PipeWriter {
85+
inner: AnonPipe,
86+
}
8187

8288
impl PipeReader {
8389
/// Create a new [`PipeReader`] instance that shares the same underlying file description.
@@ -131,7 +137,7 @@ impl PipeReader {
131137
/// ```
132138
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
133139
pub fn try_clone(&self) -> io::Result<Self> {
134-
self.0.try_clone().map(Self)
140+
self.as_inner().try_clone().map(Self::from_inner)
135141
}
136142
}
137143

@@ -167,82 +173,116 @@ impl PipeWriter {
167173
/// ```
168174
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
169175
pub fn try_clone(&self) -> io::Result<Self> {
170-
self.0.try_clone().map(Self)
176+
self.as_inner().try_clone().map(Self::from_inner)
177+
}
178+
}
179+
180+
impl AsInner<AnonPipe> for PipeReader {
181+
#[inline]
182+
fn as_inner(&self) -> &AnonPipe {
183+
&self.inner
184+
}
185+
}
186+
impl FromInner<AnonPipe> for PipeReader {
187+
fn from_inner(pipe: AnonPipe) -> PipeReader {
188+
PipeReader { inner: pipe }
189+
}
190+
}
191+
impl IntoInner<AnonPipe> for PipeReader {
192+
fn into_inner(self) -> AnonPipe {
193+
self.inner
194+
}
195+
}
196+
197+
impl AsInner<AnonPipe> for PipeWriter {
198+
#[inline]
199+
fn as_inner(&self) -> &AnonPipe {
200+
&self.inner
201+
}
202+
}
203+
impl FromInner<AnonPipe> for PipeWriter {
204+
fn from_inner(pipe: AnonPipe) -> PipeWriter {
205+
PipeWriter { inner: pipe }
206+
}
207+
}
208+
impl IntoInner<AnonPipe> for PipeWriter {
209+
fn into_inner(self) -> AnonPipe {
210+
self.inner
171211
}
172212
}
173213

174214
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
175215
impl io::Read for &PipeReader {
176216
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
177-
self.0.read(buf)
217+
self.as_inner().read(buf)
178218
}
179219
fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
180-
self.0.read_vectored(bufs)
220+
self.as_inner().read_vectored(bufs)
181221
}
182222
#[inline]
183223
fn is_read_vectored(&self) -> bool {
184-
self.0.is_read_vectored()
224+
self.as_inner().is_read_vectored()
185225
}
186226
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
187-
self.0.read_to_end(buf)
227+
self.as_inner().read_to_end(buf)
188228
}
189229
fn read_buf(&mut self, buf: io::BorrowedCursor<'_>) -> io::Result<()> {
190-
self.0.read_buf(buf)
230+
self.as_inner().read_buf(buf)
191231
}
192232
}
193233

194234
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
195235
impl io::Read for PipeReader {
196236
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
197-
self.0.read(buf)
237+
self.as_inner().read(buf)
198238
}
199239
fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
200-
self.0.read_vectored(bufs)
240+
self.as_inner().read_vectored(bufs)
201241
}
202242
#[inline]
203243
fn is_read_vectored(&self) -> bool {
204-
self.0.is_read_vectored()
244+
self.as_inner().is_read_vectored()
205245
}
206246
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
207-
self.0.read_to_end(buf)
247+
self.as_inner().read_to_end(buf)
208248
}
209249
fn read_buf(&mut self, buf: io::BorrowedCursor<'_>) -> io::Result<()> {
210-
self.0.read_buf(buf)
250+
self.as_inner().read_buf(buf)
211251
}
212252
}
213253

214254
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
215255
impl io::Write for &PipeWriter {
216256
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
217-
self.0.write(buf)
257+
self.as_inner().write(buf)
218258
}
219259
#[inline]
220260
fn flush(&mut self) -> io::Result<()> {
221261
Ok(())
222262
}
223263
fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
224-
self.0.write_vectored(bufs)
264+
self.as_inner().write_vectored(bufs)
225265
}
226266
#[inline]
227267
fn is_write_vectored(&self) -> bool {
228-
self.0.is_write_vectored()
268+
self.as_inner().is_write_vectored()
229269
}
230270
}
231271

232272
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
233273
impl io::Write for PipeWriter {
234274
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
235-
self.0.write(buf)
275+
self.as_inner().write(buf)
236276
}
237277
#[inline]
238278
fn flush(&mut self) -> io::Result<()> {
239279
Ok(())
240280
}
241281
fn write_vectored(&mut self, bufs: &[io::IoSlice<'_>]) -> io::Result<usize> {
242-
self.0.write_vectored(bufs)
282+
self.as_inner().write_vectored(bufs)
243283
}
244284
#[inline]
245285
fn is_write_vectored(&self) -> bool {
246-
self.0.is_write_vectored()
286+
self.as_inner().is_write_vectored()
247287
}
248288
}

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

+38
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,44 @@ impl From<OwnedFd> for fs::File {
310310
}
311311
}
312312

313+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
314+
impl AsFd for io::PipeReader {
315+
fn as_fd(&self) -> BorrowedFd<'_> {
316+
self.as_inner().as_fd()
317+
}
318+
}
319+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
320+
impl From<io::PipeReader> for OwnedFd {
321+
fn from(pipe: io::PipeReader) -> OwnedFd {
322+
pipe.into_inner().into_inner().into_inner()
323+
}
324+
}
325+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
326+
impl From<OwnedFd> for io::PipeReader {
327+
fn from(owned_fd: OwnedFd) -> io::PipeReader {
328+
Self::from_inner(FromInner::from_inner(FromInner::from_inner(owned_fd)))
329+
}
330+
}
331+
332+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
333+
impl AsFd for io::PipeWriter {
334+
fn as_fd(&self) -> BorrowedFd<'_> {
335+
self.as_inner().as_fd()
336+
}
337+
}
338+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
339+
impl From<io::PipeWriter> for OwnedFd {
340+
fn from(pipe: io::PipeWriter) -> OwnedFd {
341+
pipe.into_inner().into_inner().into_inner()
342+
}
343+
}
344+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
345+
impl From<OwnedFd> for io::PipeWriter {
346+
fn from(owned_fd: OwnedFd) -> io::PipeWriter {
347+
Self::from_inner(FromInner::from_inner(FromInner::from_inner(owned_fd)))
348+
}
349+
}
350+
313351
#[stable(feature = "io_safety", since = "1.63.0")]
314352
impl AsFd for crate::net::TcpStream {
315353
#[inline]

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

+38-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +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_common::{AsInner, IntoInner};
18+
use crate::sys_common::{AsInner, FromInner, IntoInner};
1919
use crate::{fs, io};
2020

2121
/// Raw file descriptors.
@@ -182,6 +182,43 @@ impl IntoRawFd for fs::File {
182182
}
183183
}
184184

185+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
186+
impl AsRawFd for io::PipeReader {
187+
fn as_raw_fd(&self) -> RawFd {
188+
self.as_inner().as_raw_fd()
189+
}
190+
}
191+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
192+
impl FromRawFd for io::PipeReader {
193+
unsafe fn from_raw_fd(raw_fd: RawFd) -> io::PipeReader {
194+
unsafe { io::PipeReader::from_inner(FromRawFd::from_raw_fd(raw_fd)) }
195+
}
196+
}
197+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
198+
impl IntoRawFd for io::PipeReader {
199+
fn into_raw_fd(self) -> RawFd {
200+
self.into_inner().into_raw_fd()
201+
}
202+
}
203+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
204+
impl AsRawFd for io::PipeWriter {
205+
fn as_raw_fd(&self) -> RawFd {
206+
self.as_inner().as_raw_fd()
207+
}
208+
}
209+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
210+
impl FromRawFd for io::PipeWriter {
211+
unsafe fn from_raw_fd(raw_fd: RawFd) -> io::PipeWriter {
212+
unsafe { io::PipeWriter::from_inner(FromRawFd::from_raw_fd(raw_fd)) }
213+
}
214+
}
215+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
216+
impl IntoRawFd for io::PipeWriter {
217+
fn into_raw_fd(self) -> RawFd {
218+
self.into_inner().into_raw_fd()
219+
}
220+
}
221+
185222
#[stable(feature = "asraw_stdio", since = "1.21.0")]
186223
impl AsRawFd for io::Stdin {
187224
#[inline]

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

+47-9
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl OwnedHandle {
185185
/// Creates a new `OwnedHandle` instance that shares the same underlying
186186
/// object as the existing `OwnedHandle` instance.
187187
#[stable(feature = "io_safety", since = "1.63.0")]
188-
pub fn try_clone(&self) -> crate::io::Result<Self> {
188+
pub fn try_clone(&self) -> io::Result<Self> {
189189
self.as_handle().try_clone_to_owned()
190190
}
191191
}
@@ -194,7 +194,7 @@ impl BorrowedHandle<'_> {
194194
/// Creates a new `OwnedHandle` instance that shares the same underlying
195195
/// object as the existing `BorrowedHandle` instance.
196196
#[stable(feature = "io_safety", since = "1.63.0")]
197-
pub fn try_clone_to_owned(&self) -> crate::io::Result<OwnedHandle> {
197+
pub fn try_clone_to_owned(&self) -> io::Result<OwnedHandle> {
198198
self.duplicate(0, false, sys::c::DUPLICATE_SAME_ACCESS)
199199
}
200200

@@ -410,7 +410,7 @@ macro_rules! impl_is_terminal {
410410
impl crate::sealed::Sealed for $t {}
411411

412412
#[stable(feature = "is_terminal", since = "1.70.0")]
413-
impl crate::io::IsTerminal for $t {
413+
impl io::IsTerminal for $t {
414414
#[inline]
415415
fn is_terminal(&self) -> bool {
416416
crate::sys::io::is_terminal(self)
@@ -546,48 +546,86 @@ impl From<OwnedHandle> for fs::File {
546546
}
547547
}
548548

549+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
550+
impl AsHandle for io::PipeReader {
551+
fn as_handle(&self) -> BorrowedHandle<'_> {
552+
self.as_inner().as_handle()
553+
}
554+
}
555+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
556+
impl From<io::PipeReader> for OwnedHandle {
557+
fn from(pipe: io::PipeReader) -> OwnedHandle {
558+
pipe.into_inner().into_inner().into_inner()
559+
}
560+
}
561+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
562+
impl From<OwnedHandle> for io::PipeReader {
563+
fn from(owned: OwnedHandle) -> io::PipeReader {
564+
io::PipeReader::from_inner(FromInner::from_inner(FromInner::from_inner(owned)))
565+
}
566+
}
567+
568+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
569+
impl AsHandle for io::PipeWriter {
570+
fn as_handle(&self) -> BorrowedHandle<'_> {
571+
self.as_inner().as_handle()
572+
}
573+
}
574+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
575+
impl From<io::PipeWriter> for OwnedHandle {
576+
fn from(pipe: io::PipeWriter) -> OwnedHandle {
577+
pipe.into_inner().into_inner().into_inner()
578+
}
579+
}
580+
#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
581+
impl From<OwnedHandle> for io::PipeWriter {
582+
fn from(owned: OwnedHandle) -> io::PipeWriter {
583+
io::PipeWriter::from_inner(FromInner::from_inner(FromInner::from_inner(owned)))
584+
}
585+
}
586+
549587
#[stable(feature = "io_safety", since = "1.63.0")]
550-
impl AsHandle for crate::io::Stdin {
588+
impl AsHandle for io::Stdin {
551589
#[inline]
552590
fn as_handle(&self) -> BorrowedHandle<'_> {
553591
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
554592
}
555593
}
556594

557595
#[stable(feature = "io_safety", since = "1.63.0")]
558-
impl<'a> AsHandle for crate::io::StdinLock<'a> {
596+
impl<'a> AsHandle for io::StdinLock<'a> {
559597
#[inline]
560598
fn as_handle(&self) -> BorrowedHandle<'_> {
561599
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
562600
}
563601
}
564602

565603
#[stable(feature = "io_safety", since = "1.63.0")]
566-
impl AsHandle for crate::io::Stdout {
604+
impl AsHandle for io::Stdout {
567605
#[inline]
568606
fn as_handle(&self) -> BorrowedHandle<'_> {
569607
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
570608
}
571609
}
572610

573611
#[stable(feature = "io_safety", since = "1.63.0")]
574-
impl<'a> AsHandle for crate::io::StdoutLock<'a> {
612+
impl<'a> AsHandle for io::StdoutLock<'a> {
575613
#[inline]
576614
fn as_handle(&self) -> BorrowedHandle<'_> {
577615
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
578616
}
579617
}
580618

581619
#[stable(feature = "io_safety", since = "1.63.0")]
582-
impl AsHandle for crate::io::Stderr {
620+
impl AsHandle for io::Stderr {
583621
#[inline]
584622
fn as_handle(&self) -> BorrowedHandle<'_> {
585623
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
586624
}
587625
}
588626

589627
#[stable(feature = "io_safety", since = "1.63.0")]
590-
impl<'a> AsHandle for crate::io::StderrLock<'a> {
628+
impl<'a> AsHandle for io::StderrLock<'a> {
591629
#[inline]
592630
fn as_handle(&self) -> BorrowedHandle<'_> {
593631
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }

0 commit comments

Comments
 (0)