pub struct Sender { /* private fields */ }
Expand description
Writing end of a Unix pipe.
It can be constructed from a FIFO file with OpenOptions::open_sender
.
Opening a named pipe for writing involves a few steps.
Call to OpenOptions::open_sender
might fail with an error indicating
different things:
io::ErrorKind::NotFound
- There is no file at the specified path.io::ErrorKind::InvalidInput
- The file exists, but it is not a FIFO.ENXIO
- The file is a FIFO, but no process has it open for reading. Sleep for a while and try again.- Other OS errors not specific to opening FIFO files.
Opening a Sender
from a FIFO file should look like this:
use std::time::Duration;
use compio_fs::pipe;
use compio_runtime::time;
const FIFO_NAME: &str = "path/to/a/fifo";
// Wait for a reader to open the file.
let tx = loop {
match pipe::OpenOptions::new().open_sender(FIFO_NAME).await {
Ok(tx) => break tx,
Err(e) if e.raw_os_error() == Some(libc::ENXIO) => {}
Err(e) => return Err(e.into()),
}
time::sleep(Duration::from_millis(50)).await;
};
On Linux, it is possible to create a Sender
without waiting in a sleeping
loop. This is done by opening a named pipe in read-write access mode with
OpenOptions::read_write
. This way, a Sender
can at the same time hold
both a writing end and a reading end, and the latter allows to open a FIFO
without ENXIO
error since the pipe is open for reading as well.
Sender
cannot be used to read from a pipe, so in practice the read access
is only used when a FIFO is opened. However, using a Sender
in read-write
mode may lead to lost data, because written data will be dropped by the
system as soon as all pipe ends are closed. To avoid lost data you have to
make sure that a reading end has been opened before dropping a Sender
.
Note that using read-write access mode with FIFO files is not defined by the POSIX standard and it is only guaranteed to work on Linux.
use compio_fs::pipe;
use compio_io::AsyncWriteExt;
const FIFO_NAME: &str = "path/to/a/fifo";
let mut tx = pipe::OpenOptions::new()
.read_write(true)
.open_sender(FIFO_NAME)
.unwrap();
// Asynchronously write to the pipe before a reader.
tx.write_all("hello world").await.unwrap();
Implementations§
Trait Implementations§
source§impl AsyncWrite for &Sender
impl AsyncWrite for &Sender
source§async fn write_vectored<T: IoVectoredBuf>(
&mut self,
buffer: T,
) -> BufResult<usize, T>
async fn write_vectored<T: IoVectoredBuf>( &mut self, buffer: T, ) -> BufResult<usize, T>
write
, except that it write bytes from a buffer implements
IoVectoredBuf
into the source. Read moresource§impl AsyncWrite for Sender
impl AsyncWrite for Sender
source§async fn write_vectored<T: IoVectoredBuf>(
&mut self,
buf: T,
) -> BufResult<usize, T>
async fn write_vectored<T: IoVectoredBuf>( &mut self, buf: T, ) -> BufResult<usize, T>
write
, except that it write bytes from a buffer implements
IoVectoredBuf
into the source. Read moresource§impl FromRawFd for Sender
impl FromRawFd for Sender
source§unsafe fn from_raw_fd(fd: RawFd) -> Self
unsafe fn from_raw_fd(fd: RawFd) -> Self
Self
from the given raw file
descriptor. Read moreSharedFd
.Auto Trait Implementations§
impl Freeze for Sender
impl RefUnwindSafe for Sender
impl Send for Sender
impl Sync for Sender
impl Unpin for Sender
impl UnwindSafe for Sender
Blanket Implementations§
source§impl<A> AsyncWriteExt for Awhere
A: AsyncWrite + ?Sized,
impl<A> AsyncWriteExt for Awhere
A: AsyncWrite + ?Sized,
source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
AsyncWrite
. Read moresource§async fn write_all<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoBuf,
async fn write_all<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoBuf,
source§async fn write_vectored_all<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoVectoredBuf,
async fn write_vectored_all<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoVectoredBuf,
AsyncWrite::write_vectored
, except that it tries to write the entire
contents of the buffer into this writer.source§async fn write_u8(&mut self, num: u8) -> Result<(), Error>
async fn write_u8(&mut self, num: u8) -> Result<(), Error>
u8
into the underlying writer.source§async fn write_u8_le(&mut self, num: u8) -> Result<(), Error>
async fn write_u8_le(&mut self, num: u8) -> Result<(), Error>
u8
into the underlying writer.source§async fn write_u16(&mut self, num: u16) -> Result<(), Error>
async fn write_u16(&mut self, num: u16) -> Result<(), Error>
u16
into the underlying writer.source§async fn write_u16_le(&mut self, num: u16) -> Result<(), Error>
async fn write_u16_le(&mut self, num: u16) -> Result<(), Error>
u16
into the underlying writer.source§async fn write_u32(&mut self, num: u32) -> Result<(), Error>
async fn write_u32(&mut self, num: u32) -> Result<(), Error>
u32
into the underlying writer.source§async fn write_u32_le(&mut self, num: u32) -> Result<(), Error>
async fn write_u32_le(&mut self, num: u32) -> Result<(), Error>
u32
into the underlying writer.source§async fn write_u64(&mut self, num: u64) -> Result<(), Error>
async fn write_u64(&mut self, num: u64) -> Result<(), Error>
u64
into the underlying writer.source§async fn write_u64_le(&mut self, num: u64) -> Result<(), Error>
async fn write_u64_le(&mut self, num: u64) -> Result<(), Error>
u64
into the underlying writer.source§async fn write_u128(&mut self, num: u128) -> Result<(), Error>
async fn write_u128(&mut self, num: u128) -> Result<(), Error>
u128
into the underlying writer.source§async fn write_u128_le(&mut self, num: u128) -> Result<(), Error>
async fn write_u128_le(&mut self, num: u128) -> Result<(), Error>
u128
into the underlying writer.source§async fn write_i8(&mut self, num: i8) -> Result<(), Error>
async fn write_i8(&mut self, num: i8) -> Result<(), Error>
i8
into the underlying writer.source§async fn write_i8_le(&mut self, num: i8) -> Result<(), Error>
async fn write_i8_le(&mut self, num: i8) -> Result<(), Error>
i8
into the underlying writer.source§async fn write_i16(&mut self, num: i16) -> Result<(), Error>
async fn write_i16(&mut self, num: i16) -> Result<(), Error>
i16
into the underlying writer.source§async fn write_i16_le(&mut self, num: i16) -> Result<(), Error>
async fn write_i16_le(&mut self, num: i16) -> Result<(), Error>
i16
into the underlying writer.source§async fn write_i32(&mut self, num: i32) -> Result<(), Error>
async fn write_i32(&mut self, num: i32) -> Result<(), Error>
i32
into the underlying writer.source§async fn write_i32_le(&mut self, num: i32) -> Result<(), Error>
async fn write_i32_le(&mut self, num: i32) -> Result<(), Error>
i32
into the underlying writer.source§async fn write_i64(&mut self, num: i64) -> Result<(), Error>
async fn write_i64(&mut self, num: i64) -> Result<(), Error>
i64
into the underlying writer.source§async fn write_i64_le(&mut self, num: i64) -> Result<(), Error>
async fn write_i64_le(&mut self, num: i64) -> Result<(), Error>
i64
into the underlying writer.source§async fn write_i128(&mut self, num: i128) -> Result<(), Error>
async fn write_i128(&mut self, num: i128) -> Result<(), Error>
i128
into the underlying writer.source§async fn write_i128_le(&mut self, num: i128) -> Result<(), Error>
async fn write_i128_le(&mut self, num: i128) -> Result<(), Error>
i128
into the underlying writer.source§async fn write_f32(&mut self, num: f32) -> Result<(), Error>
async fn write_f32(&mut self, num: f32) -> Result<(), Error>
f32
into the underlying writer.source§async fn write_f32_le(&mut self, num: f32) -> Result<(), Error>
async fn write_f32_le(&mut self, num: f32) -> Result<(), Error>
f32
into the underlying writer.source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)