pub struct Receiver { /* private fields */ }fs and Unix only.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 AsFd for Receiver
impl AsFd for Receiver
Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Source§impl AsyncRead for &Receiver
impl AsyncRead for &Receiver
Source§impl AsyncRead for Receiver
impl AsyncRead for Receiver
Source§impl AsyncReadManaged for &Receiver
impl AsyncReadManaged for &Receiver
Source§type Buffer<'a> = BorrowedBuffer<'a>
type Buffer<'a> = BorrowedBuffer<'a>
Source§type BufferPool = BufferPool
type BufferPool = BufferPool
Source§async fn read_managed<'a>(
&mut self,
buffer_pool: &'a <&Receiver as AsyncReadManaged>::BufferPool,
len: usize,
) -> Result<<&Receiver as AsyncReadManaged>::Buffer<'a>, Error>
async fn read_managed<'a>( &mut self, buffer_pool: &'a <&Receiver as AsyncReadManaged>::BufferPool, len: usize, ) -> Result<<&Receiver as AsyncReadManaged>::Buffer<'a>, Error>
Source§impl AsyncReadManaged for Receiver
impl AsyncReadManaged for Receiver
Source§type Buffer<'a> = BorrowedBuffer<'a>
type Buffer<'a> = BorrowedBuffer<'a>
Source§type BufferPool = BufferPool
type BufferPool = BufferPool
Source§async fn read_managed<'a>(
&mut self,
buffer_pool: &'a <Receiver as AsyncReadManaged>::BufferPool,
len: usize,
) -> Result<<Receiver as AsyncReadManaged>::Buffer<'a>, Error>
async fn read_managed<'a>( &mut self, buffer_pool: &'a <Receiver as AsyncReadManaged>::BufferPool, len: usize, ) -> Result<<Receiver as AsyncReadManaged>::Buffer<'a>, Error>
SharedFd.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§
§impl<T> AsSource for Twhere
T: AsFd,
impl<T> AsSource for Twhere
T: AsFd,
§fn source(&self) -> BorrowedFd<'_>
fn source(&self) -> BorrowedFd<'_>
§impl<A> AsyncReadExt for A
impl<A> AsyncReadExt for A
§async fn append<T>(&mut self, buf: T) -> BufResult<usize, T>where
T: IoBufMut,
async fn append<T>(&mut self, buf: T) -> BufResult<usize, T>where
T: IoBufMut,
AsyncRead::read, but it appends data to the end of the
buffer; in other words, it read to the beginning of the uninitialized
area.§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,
§async fn read_to_string(&mut self, buf: String) -> BufResult<usize, String>
async fn read_to_string(&mut self, buf: String) -> BufResult<usize, String>
String until underlying reader reaches EOF.§async fn read_to_end<A>(
&mut self,
buf: Vec<u8, A>,
) -> BufResult<usize, Vec<u8, A>>where
A: Allocator + 'static,
async fn read_to_end<A>(
&mut self,
buf: Vec<u8, A>,
) -> BufResult<usize, Vec<u8, A>>where
A: Allocator + 'static,
EOF.§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,
§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 more§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.§async fn read_u16(&mut self) -> Result<u16, Error>
async fn read_u16(&mut self) -> Result<u16, Error>
u16 from the underlying reader.§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.§async fn read_u32(&mut self) -> Result<u32, Error>
async fn read_u32(&mut self) -> Result<u32, Error>
u32 from the underlying reader.§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.§async fn read_u64(&mut self) -> Result<u64, Error>
async fn read_u64(&mut self) -> Result<u64, Error>
u64 from the underlying reader.§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.§async fn read_u128(&mut self) -> Result<u128, Error>
async fn read_u128(&mut self) -> Result<u128, Error>
u128 from the underlying reader.§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.§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.§async fn read_i16(&mut self) -> Result<i16, Error>
async fn read_i16(&mut self) -> Result<i16, Error>
i16 from the underlying reader.§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.§async fn read_i32(&mut self) -> Result<i32, Error>
async fn read_i32(&mut self) -> Result<i32, Error>
i32 from the underlying reader.§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.§async fn read_i64(&mut self) -> Result<i64, Error>
async fn read_i64(&mut self) -> Result<i64, Error>
i64 from the underlying reader.§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.§async fn read_i128(&mut self) -> Result<i128, Error>
async fn read_i128(&mut self) -> Result<i128, Error>
i128 from the underlying reader.§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.§async fn read_f32(&mut self) -> Result<f32, Error>
async fn read_f32(&mut self) -> Result<f32, Error>
f32 from the underlying reader.§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.§async fn read_f64(&mut self) -> Result<f64, Error>
async fn read_f64(&mut self) -> Result<f64, Error>
f64 from the underlying reader.§async fn read_f64_le(&mut self) -> Result<f64, Error>
async fn read_f64_le(&mut self) -> Result<f64, Error>
f64 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,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more