1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
//! A sink for values which are asynchronously accepted, until the target is closed.
//!
//! Postage channel senders implement Sink:
//! ```rust
//! 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:
//! ```rust
//! 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:
//! ```rust
//! 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`:
//! ```rust
//! use postage::mpsc::channel;
//! use postage::sink::Sink;
//!
//! #[tokio::main]
//! async fn main() {
//! let (mut tx, rx) = channel(16);
//! tx.send(true).await.ok();
//! }
//! ```
use std::marker::PhantomPinned;
use std::{future::Future, ops::DerefMut, pin::Pin, task::Poll};
use crate::Context;
use pin_project::pin_project;
mod chain;
mod errors;
mod filter;
#[cfg(feature = "logging")]
mod sink_log;
pub use errors::*;
/// A sink which can asynchronously accept messages, and at some point may refuse to accept any further messages.
///
/// Sinks implement `poll_send`, a poll-based method very similar to `std::future::Future`.
///
/// Sinks can be used in async code with `stream.send(value).await`, or with `stream.try_send(value)`. Note that
/// `send` returns an error if the sink has been closed. And `try_send` returns an error if the sink is full, or it is closed.
///
/// Send errors can be ignored using `Result::ok`.
///
/// ```rust
/// use postage::mpsc::channel;
/// use postage::sink::{Sink, TrySendError};
///
/// #[tokio::main]
/// async fn main() -> Result<(), TrySendError<bool>> {
/// let (mut tx, mut rx) = channel(16);
/// tx.send(true).await.ok();
/// tx.try_send(true)?;
/// drop(tx);
/// Ok(())
/// }
/// ```
///
/// Sinks also support combinators, such as map, filter, chain, and log.
/// ```rust
/// use postage::mpsc::channel;
/// use postage::sink::{Sink, SendError, TrySendError};
/// use postage::stream::Stream;
///
/// #[tokio::main]
/// async fn main() {
/// let (mut tx, mut rx) = channel(16);
/// let (tx2, mut rx2) = channel(16);
///
/// let mut combo = tx2
/// .after(tx)
/// .filter(|i| *i >= 2);
///
/// // The `logging` feature enables a combinator that logs values using the Debug trait.
/// #[cfg(feature = "logging")]
/// let mut combo = combo
/// .log(log::Level::Info);
///
/// combo.send(1usize).await.ok();
/// combo.send(2usize).await.ok();
/// assert_eq!(Some(2usize), rx.recv().await);
/// drop(rx);
///
/// combo.send(3usize).await.ok();
/// combo.send(4usize).await.ok();
/// assert_eq!(Some(3usize), rx2.recv().await);
/// assert_eq!(Some(4usize), rx2.recv().await);
///
/// drop(rx2);
/// assert_eq!(Err(SendError(5usize)), combo.send(5usize).await);
/// }
/// ```
pub trait Sink {
type Item;
/// Attempts to accept the message, without blocking.
///
/// Returns:
/// - `PollSend::Ready` if the value was sent
/// - `PollSend::Pending(value)` if the channel is full. The channel will call the waker in `cx` when the item may be accepted in the future.
/// - `PollSend::Rejected(value)` if the channel is closed, and will never accept the item.
fn poll_send(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
value: Self::Item,
) -> PollSend<Self::Item>;
/// Attempts to send a message into the sink.
///
/// Returns:
/// - `Ok(())` if the value was accepted.
/// - `Err(SendError(value))` if the sink rejected the message.
fn send(&mut self, value: Self::Item) -> SendFuture<Self> {
SendFuture::new(self, value)
}
/// Attempts to send a message over the sink, without blocking.
///
/// Returns:
/// - `Ok(())` if the value was accepted.
/// - `Err(TrySendError::Pending(value))` if the channel is full, and cannot accept the item at this time.
/// - `Err(TrySendError::Rejected(value))` if the channel is closed, and will never accept the item.
fn try_send(&mut self, value: Self::Item) -> Result<(), TrySendError<Self::Item>>
where
Self: Unpin,
{
let pin = Pin::new(self);
match pin.poll_send(&mut Context::empty(), value) {
PollSend::Ready => Ok(()),
PollSend::Pending(value) => Err(TrySendError::Pending(value)),
PollSend::Rejected(value) => Err(TrySendError::Rejected(value)),
}
}
/// Sends a message over the channel, blocking the current thread until the message is sent.
///
/// Requires the `blocking` feature (enabled by default).
#[cfg(feature = "blocking")]
fn blocking_send(&mut self, value: Self::Item) -> Result<(), SendError<Self::Item>>
where
Self: Unpin,
{
pollster::block_on(self.send(value))
}
/// Chains two sink implementations. Messages will be transmitted to the argument until it rejects a message.
/// Then messages will be transmitted to self.
fn after<Before>(self, before: Before) -> chain::ChainSink<Before, Self>
where
Before: Sink<Item = Self::Item>,
Self: Sized,
{
chain::ChainSink::new(before, self)
}
/// Filters messages, forwarding them to the sink if the filter returns true
fn filter<Filter>(self, filter: Filter) -> filter::FilterSink<Filter, Self>
where
Filter: FnMut(&Self::Item) -> bool,
Self: Sized,
{
filter::FilterSink::new(filter, self)
}
/// Logs messages that are accepted by the sink using the Debug trait, at the provided log level.
///
/// Requires the `logging` feature
#[cfg(feature = "logging")]
fn log(self, level: log::Level) -> sink_log::SinkLog<Self>
where
Self: Sized,
Self::Item: std::fmt::Debug,
{
sink_log::SinkLog::new(self, level)
}
}
impl<S> Sink for &mut S
where
S: Sink + Unpin + ?Sized,
{
type Item = S::Item;
fn poll_send(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
value: Self::Item,
) -> PollSend<Self::Item> {
S::poll_send(Pin::new(&mut **self), cx, value)
}
}
impl<P, S> Sink for Pin<P>
where
P: DerefMut<Target = S> + Unpin,
S: Sink + Unpin + ?Sized,
{
type Item = <S as Sink>::Item;
fn poll_send(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
value: Self::Item,
) -> PollSend<Self::Item> {
Pin::get_mut(self).as_mut().poll_send(cx, value)
}
}
/// An enum of poll responses that are produced by Sink implementations.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PollSend<T> {
/// The item was accepted and sent
Ready,
/// The sender is pending, and has registered with the waker context
Pending(T),
/// The sender has been closed, and will never accept the item
Rejected(T),
}
/// A future returned by `Sink::send`, which wraps an item.
/// The item is sent to the sink, or returned if the sink is closed.
#[pin_project]
#[must_use = "futures do nothing unless polled"]
pub struct SendFuture<'s, S>
where
S: Sink + ?Sized,
{
#[pin]
send: &'s mut S,
value: Option<S::Item>,
#[pin]
_pin: PhantomPinned,
}
impl<'s, S> SendFuture<'s, S>
where
S: Sink + ?Sized,
{
pub fn new(send: &'s mut S, value: S::Item) -> SendFuture<S> {
Self {
send,
value: Some(value),
_pin: PhantomPinned,
}
}
}
impl<'s, S> Future for SendFuture<'s, S>
where
S: Sink + Unpin + ?Sized,
{
type Output = Result<(), SendError<S::Item>>;
fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
if self.value.is_none() {
return Poll::Ready(Ok(()));
}
let this = self.project();
let mut cx: crate::Context<'_> = cx.into();
match this.send.poll_send(&mut cx, this.value.take().unwrap()) {
PollSend::Ready => Poll::Ready(Ok(())),
PollSend::Pending(value) => {
*this.value = Some(value);
Poll::Pending
}
PollSend::Rejected(value) => Poll::Ready(Err(SendError(value))),
}
}
}
#[cfg(test)]
mod tests {
#[cfg(feature = "blocking")]
#[test]
fn test_blocking() {
use super::Sink;
use crate::test::sink::ready;
let mut stream = ready();
assert_eq!(Ok(()), stream.blocking_send(1usize));
}
}