Module postage::sink [−][src]
A sink for values which are asynchronously accepted, until the target is closed.
Postage channel senders implement Sink:
use postage::mpsc::channel; use postage::sink::Sink; #[tokio::main] async fn main() { let (mut tx, rx) = channel(16); assert_eq!(Ok(()), tx.send(true).await); }
Sinks return an error if the channel is closed, and the message cannot be accepted by the receiver:
use postage::mpsc::channel; use postage::sink::{SendError, Sink}; #[tokio::main] async fn main() { let (mut tx, rx) = channel(16); drop(rx); assert_eq!(Err(SendError(true)), tx.send(true).await); }
Note that Sink::send
returns an Err
type, unlike Stream::recv
which returns an option.
This is because the failure to send a message sometimes needs to be interpreted as an application error:
use postage::mpsc::channel; use postage::sink::{SendError, Sink}; #[tokio::main] async fn main() -> Result<(), SendError<bool>> { let (mut tx, rx) = channel(16); tx.send(true).await?; Ok(()) }
Tasks can ignore send errors by using Result::ok
:
use postage::mpsc::channel; use postage::sink::Sink; #[tokio::main] async fn main() { let (mut tx, rx) = channel(16); tx.send(true).await.ok(); }
Structs
SendError | An error type returned by |
SendFuture | A future returned by |
Enums
PollSend | An enum of poll responses that are produced by Sink implementations. |
TrySendError | An error type returned by |
Traits
Sink | A sink which can asynchronously accept messages, and at some point may refuse to accept any further messages. |