Struct PipeReader

pub struct PipeReader(pub(crate) FileDesc);

Read end of an anonymous pipe.

Fields

0: FileDesc

Implementations

impl PipeReader

fn try_clone(&self) -> Result<Self>

Creates a new PipeReader instance that shares the same underlying file description.

Examples

# fn main() -> std::io::Result<()> {
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()));
# Ok(())
# }

Trait Implementations

impl AsFd for PipeReader

fn as_fd(&self) -> BorrowedFd<'_>

impl AsHandle for PipeReader

fn as_handle(&self) -> BorrowedHandle<'_>

impl AsRawFd for PipeReader

fn as_raw_fd(&self) -> RawFd

impl AsRawHandle for PipeReader

fn as_raw_handle(&self) -> RawHandle

impl CopyRead for PipeReader

fn properties(&self) -> CopyParams

impl Debug for PipeReader

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl From<OwnedFd> for PipeReader

fn from(owned_fd: OwnedFd) -> Self

impl From<OwnedHandle> for PipeReader

fn from(owned_handle: OwnedHandle) -> Self

impl FromInner<FileDesc> for PipeReader

fn from_inner(inner: FileDesc) -> Self

impl FromRawFd for PipeReader

unsafe fn from_raw_fd(raw_fd: RawFd) -> Self

impl FromRawHandle for PipeReader

unsafe fn from_raw_handle(raw_handle: RawHandle) -> Self

impl IntoInner<FileDesc> for PipeReader

fn into_inner(self) -> FileDesc

impl IntoRawFd for PipeReader

fn into_raw_fd(self) -> RawFd

impl IntoRawHandle for PipeReader

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>
fn is_read_vectored(&self) -> bool
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<()>

impl SpecCopy for PipeReader

fn copy<R: Read + ?Sized, W: Write + ?Sized>(read: &mut R, write: &mut W) -> Result<CopyState>

Auto Trait Implementations

impl Freeze for PipeReader

impl RefUnwindSafe for PipeReader

impl Send for PipeReader

impl Sync for PipeReader

impl Unpin for PipeReader

impl UnsafeUnpin for PipeReader

impl UnwindSafe for PipeReader

Blanket Implementations

impl<R> SpecReadByte for PipeReader where R: Read,

fn spec_read_byte(&mut self) -> Option<Result<u8, Error>>

impl<T> Any for PipeReader where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for PipeReader where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for PipeReader where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for PipeReader

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for PipeReader where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for PipeReader

impl<T, U> Into<U> for PipeReader where 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 PipeReader where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for PipeReader where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>