pub struct PipeReader(/* private fields */);
Read end of an anonymous pipe.
impl PipeReader
pub fn try_clone(&self) -> Result<Self>
Creates a new PipeReader instance that shares the same underlying file description.
use std::fs;
use std::io::{pipe, Write};
use std::process::Command;
const NUM_SLOT: u8 = 2;
const NUM_PROC: u8 = 5;
const OUTPUT: &str = "work.txt";
let mut jobs = vec![];
let (reader, mut writer) = pipe()?;
// Write NUM_SLOT characters the pipe.
writer.write_all(&[b'|'; NUM_SLOT as usize])?;
// Spawn several processes that read a character from the pipe, do some work, then
// write back to the pipe. When the pipe is empty, the processes block, so only
// NUM_SLOT processes can be working at any given time.
for _ in 0..NUM_PROC {
jobs.push(
Command::new("bash")
.args(["-c",
&format!(
"read -n 1\n\
echo -n 'x' >> '{OUTPUT}'\n\
echo -n '|'",
),
])
.stdin(reader.try_clone()?)
.stdout(writer.try_clone()?)
.spawn()?,
);
}
// Wait for all jobs to finish.
for mut job in jobs {
job.wait()?;
}
// Check our work and clean up.
let xs = fs::read_to_string(OUTPUT)?;
fs::remove_file(OUTPUT)?;
assert_eq!(xs, "x".repeat(NUM_PROC.into()));impl AsFd for PipeReaderAvailable on (Unix or HermitCore or target_os=trusty or WASI or target_os=motor) and non-target_os=trusty only.
impl AsHandle for PipeReaderAvailable on Windows only.
impl AsRawFd for PipeReaderAvailable on (Unix or HermitCore or target_os=trusty or WASI or target_os=motor) and non-target_os=trusty only.
impl AsRawHandle for PipeReaderAvailable on Windows only.
impl Debug for PipeReader
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl From<OwnedFd> for PipeReaderAvailable on (Unix or HermitCore or target_os=trusty or WASI or target_os=motor) and non-target_os=trusty only.
fn from(owned_fd: OwnedFd) -> Self
impl From<OwnedHandle> for PipeReaderAvailable on Windows only.
fn from(owned_handle: OwnedHandle) -> Self
impl From<PipeReader> for OwnedFdAvailable on (Unix or HermitCore or target_os=trusty or WASI or target_os=motor) and non-target_os=trusty only.
fn from(pipe: PipeReader) -> Self
impl From<PipeReader> for OwnedHandleAvailable on Windows only.
fn from(pipe: PipeReader) -> Self
impl From<PipeReader> for Stdio
fn from(pipe: PipeReader) -> Self
impl FromRawFd for PipeReaderAvailable on (Unix or HermitCore or target_os=trusty or WASI or target_os=motor) and non-target_os=trusty only.
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self
Self from the given raw file descriptor. Read more
impl FromRawHandle for PipeReaderAvailable on Windows only.
unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self
impl IntoRawFd for PipeReaderAvailable on (Unix or HermitCore or target_os=trusty or WASI or target_os=motor) and non-target_os=trusty only.
fn into_raw_fd(self) -> RawFd
impl IntoRawHandle for PipeReaderAvailable on Windows only.
fn into_raw_handle(self) -> RawHandle
impl Read for &PipeReader
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
read, except that it reads into a slice of buffers. Read more
fn is_read_vectored(&self) -> bool
can_vector #69941)
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
buf. Read more
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<()>
read_buf #78485)
fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
buf. Read more
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
buf. Read more
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()>
read_buf #78485)
cursor. Read more
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,Read. Read more
fn bytes(self) -> Bytes<Self> ⓘwhere
Self: Sized,fn chain<R: Read>(self, next: R) -> Chain<Self, R> ⓘwhere
Self: Sized,fn take(self, limit: u64) -> Take<Self> ⓘwhere
Self: Sized,limit bytes from it. Read more
fn read_array<const N: usize>(&mut self) -> Result<[u8; N]>where
Self: Sized,read_array #148848)
impl Read for PipeReader
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
read, except that it reads into a slice of buffers. Read more
fn is_read_vectored(&self) -> bool
can_vector #69941)
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
buf. Read more
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<()>
read_buf #78485)
fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
buf. Read more
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
buf. Read more
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<()>
read_buf #78485)
cursor. Read more
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,Read. Read more
fn bytes(self) -> Bytes<Self> ⓘwhere
Self: Sized,fn chain<R: Read>(self, next: R) -> Chain<Self, R> ⓘwhere
Self: Sized,fn take(self, limit: u64) -> Take<Self> ⓘwhere
Self: Sized,limit bytes from it. Read more
fn read_array<const N: usize>(&mut self) -> Result<[u8; N]>where
Self: Sized,read_array #148848)
impl Freeze for PipeReader
impl RefUnwindSafe for PipeReader
impl Send for PipeReader
impl Sync for PipeReader
impl Unpin for PipeReader
impl UnwindSafe for PipeReader
impl<T> Any for Twhere
T: 'static + ?Sized,impl<T> Borrow<T> for Twhere
T: ?Sized,impl<T> BorrowMut<T> for Twhere
T: ?Sized,impl<T> From<T> for T
fn from(t: T) -> T
Returns the argument unchanged.
impl<T, U> Into<U> for Twhere
U: From<T>,fn into(self) -> U
Calls U::from(self).
That is, this conversion is whatever the implementation of From<T> for U chooses to do.
impl<T, U> TryFrom<U> for Twhere
U: Into<T>,type Error = Infallible
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/io/struct.PipeReader.html