Skip to content

Commit 5815404

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 5815404

File tree

15 files changed

+351
-292
lines changed

15 files changed

+351
-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]

0 commit comments

Comments
 (0)