pub struct Receiver { /* private fields */ }
Expand description
Reading end of a Unix pipe.
It can be constructed from a FIFO file with OpenOptions::open_receiver
.
§Examples
Receiving messages from a named pipe in a loop:
use std::io;
use compio_buf::BufResult;
use compio_fs::pipe;
use compio_io::AsyncReadExt;
const FIFO_NAME: &str = "path/to/a/fifo";
let mut rx = pipe::OpenOptions::new().open_receiver(FIFO_NAME).await?;
loop {
let mut msg = Vec::with_capacity(256);
let BufResult(res, msg) = rx.read_exact(msg).await;
match res {
Ok(_) => { /* handle the message */ }
Err(e) if e.kind() == io::ErrorKind::UnexpectedEof => {
// Writing end has been closed, we should reopen the pipe.
rx = pipe::OpenOptions::new().open_receiver(FIFO_NAME).await?;
}
Err(e) => return Err(e.into()),
}
}
On Linux, you can use a Receiver
in read-write access mode to implement
resilient reading from a named pipe. Unlike Receiver
opened in read-only
mode, read from a pipe in read-write mode will not fail with UnexpectedEof
when the writing end is closed. This way, a Receiver
can asynchronously
wait for the next writer to open the pipe.
You should not use functions waiting for EOF such as read_to_end
with
a Receiver
in read-write access mode, since it may wait forever.
Receiver
in this mode also holds an open writing end, which prevents
receiving EOF.
To set the read-write access mode you can use OpenOptions::read_write
.
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::AsyncReadExt;
const FIFO_NAME: &str = "path/to/a/fifo";
let mut rx = pipe::OpenOptions::new()
.read_write(true)
.open_receiver(FIFO_NAME)
.unwrap();
loop {
let mut msg = Vec::with_capacity(256);
rx.read_exact(msg).await.unwrap();
// handle the message
}
Implementations§
Trait Implementations§
source§impl AsyncRead for &Receiver
impl AsyncRead for &Receiver
source§impl AsyncRead for Receiver
impl AsyncRead for Receiver
source§impl FromRawFd for Receiver
impl FromRawFd for Receiver
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 Receiver
impl RefUnwindSafe for Receiver
impl Send for Receiver
impl Sync for Receiver
impl Unpin for Receiver
impl UnwindSafe for Receiver
Blanket Implementations§
source§impl<A> AsyncReadExt for A
impl<A> AsyncReadExt for A
source§async fn read_exact<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoBufMut,
async fn read_exact<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoBufMut,
source§async fn read_to_end(&mut self, buf: Vec<u8>) -> BufResult<usize, Vec<u8>>
async fn read_to_end(&mut self, buf: Vec<u8>) -> BufResult<usize, Vec<u8>>
EOF
.source§async fn read_vectored_exact<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoVectoredBufMut,
async fn read_vectored_exact<T>(&mut self, buf: T) -> BufResult<(), T>where
T: IoVectoredBufMut,
source§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit
bytes from it. Read moresource§async fn read_u8(&mut self) -> Result<u8, Error>
async fn read_u8(&mut self) -> Result<u8, Error>
u8
from the underlying reader.source§async fn read_u8_le(&mut self) -> Result<u8, Error>
async fn read_u8_le(&mut self) -> Result<u8, Error>
u8
from the underlying reader.source§async fn read_u16(&mut self) -> Result<u16, Error>
async fn read_u16(&mut self) -> Result<u16, Error>
u16
from the underlying reader.source§async fn read_u16_le(&mut self) -> Result<u16, Error>
async fn read_u16_le(&mut self) -> Result<u16, Error>
u16
from the underlying reader.source§async fn read_u32(&mut self) -> Result<u32, Error>
async fn read_u32(&mut self) -> Result<u32, Error>
u32
from the underlying reader.source§async fn read_u32_le(&mut self) -> Result<u32, Error>
async fn read_u32_le(&mut self) -> Result<u32, Error>
u32
from the underlying reader.source§async fn read_u64(&mut self) -> Result<u64, Error>
async fn read_u64(&mut self) -> Result<u64, Error>
u64
from the underlying reader.source§async fn read_u64_le(&mut self) -> Result<u64, Error>
async fn read_u64_le(&mut self) -> Result<u64, Error>
u64
from the underlying reader.source§async fn read_u128(&mut self) -> Result<u128, Error>
async fn read_u128(&mut self) -> Result<u128, Error>
u128
from the underlying reader.source§async fn read_u128_le(&mut self) -> Result<u128, Error>
async fn read_u128_le(&mut self) -> Result<u128, Error>
u128
from the underlying reader.source§async fn read_i8(&mut self) -> Result<i8, Error>
async fn read_i8(&mut self) -> Result<i8, Error>
i8
from the underlying reader.source§async fn read_i8_le(&mut self) -> Result<i8, Error>
async fn read_i8_le(&mut self) -> Result<i8, Error>
i8
from the underlying reader.source§async fn read_i16(&mut self) -> Result<i16, Error>
async fn read_i16(&mut self) -> Result<i16, Error>
i16
from the underlying reader.source§async fn read_i16_le(&mut self) -> Result<i16, Error>
async fn read_i16_le(&mut self) -> Result<i16, Error>
i16
from the underlying reader.source§async fn read_i32(&mut self) -> Result<i32, Error>
async fn read_i32(&mut self) -> Result<i32, Error>
i32
from the underlying reader.source§async fn read_i32_le(&mut self) -> Result<i32, Error>
async fn read_i32_le(&mut self) -> Result<i32, Error>
i32
from the underlying reader.source§async fn read_i64(&mut self) -> Result<i64, Error>
async fn read_i64(&mut self) -> Result<i64, Error>
i64
from the underlying reader.source§async fn read_i64_le(&mut self) -> Result<i64, Error>
async fn read_i64_le(&mut self) -> Result<i64, Error>
i64
from the underlying reader.source§async fn read_i128(&mut self) -> Result<i128, Error>
async fn read_i128(&mut self) -> Result<i128, Error>
i128
from the underlying reader.source§async fn read_i128_le(&mut self) -> Result<i128, Error>
async fn read_i128_le(&mut self) -> Result<i128, Error>
i128
from the underlying reader.source§async fn read_f32(&mut self) -> Result<f32, Error>
async fn read_f32(&mut self) -> Result<f32, Error>
f32
from the underlying reader.source§async fn read_f32_le(&mut self) -> Result<f32, Error>
async fn read_f32_le(&mut self) -> Result<f32, Error>
f32
from the underlying reader.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
)