pub struct Sender { /* private fields */ }
Expand description
Sending end of an Unix pipe.
See new
for documentation, including examples.
Implementations§
source§impl Sender
impl Sender
sourcepub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
pub fn set_nonblocking(&self, nonblocking: bool) -> Result<()>
Set the Sender
into or out of non-blocking mode.
sourcepub fn try_io<F, T>(&self, f: F) -> Result<T>where
F: FnOnce() -> Result<T>,
pub fn try_io<F, T>(&self, f: F) -> Result<T>where F: FnOnce() -> Result<T>,
Execute an I/O operation ensuring that the socket receives more events
if it hits a WouldBlock
error.
Notes
This method is required to be called for all I/O operations to
ensure the user will receive events once the socket is ready again after
returning a WouldBlock
error.
Examples
use std::io;
use std::os::unix::io::AsRawFd;
use mio::unix::pipe;
let (sender, receiver) = pipe::new()?;
// Wait until the sender is writable...
// Write to the sender using a direct libc call, of course the
// `io::Write` implementation would be easier to use.
let buf = b"hello";
let n = sender.try_io(|| {
let buf_ptr = &buf as *const _ as *const _;
let res = unsafe { libc::write(sender.as_raw_fd(), buf_ptr, buf.len()) };
if res != -1 {
Ok(res as usize)
} else {
// If EAGAIN or EWOULDBLOCK is set by libc::write, the closure
// should return `WouldBlock` error.
Err(io::Error::last_os_error())
}
})?;
eprintln!("write {} bytes", n);
// Wait until the receiver is readable...
// Read from the receiver using a direct libc call, of course the
// `io::Read` implementation would be easier to use.
let mut buf = [0; 512];
let n = receiver.try_io(|| {
let buf_ptr = &mut buf as *mut _ as *mut _;
let res = unsafe { libc::read(receiver.as_raw_fd(), buf_ptr, buf.len()) };
if res != -1 {
Ok(res as usize)
} else {
// If EAGAIN or EWOULDBLOCK is set by libc::read, the closure
// should return `WouldBlock` error.
Err(io::Error::last_os_error())
}
})?;
eprintln!("read {} bytes", n);
Trait Implementations§
source§impl IntoRawFd for Sender
impl IntoRawFd for Sender
source§fn into_raw_fd(self) -> RawFd
fn into_raw_fd(self) -> RawFd
Consumes this object, returning the raw underlying file descriptor. Read more
source§impl Source for Sender
impl Source for Sender
source§impl Write for &Sender
impl Write for &Sender
source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write a buffer into this writer, returning how many bytes were written. Read more
source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flush this output stream, ensuring that all intermediately buffered
contents reach their destination. Read more
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
)1.0.0 · source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer. Read more
source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
source§impl Write for Sender
impl Write for Sender
source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Write a buffer into this writer, returning how many bytes were written. Read more
source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flush this output stream, ensuring that all intermediately buffered
contents reach their destination. Read more
source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector
)1.0.0 · source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer. Read more
source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more