monoio/io/sink/
mod.rs

1//! Sink trait in GAT style.
2mod sink_ext;
3
4use std::future::Future;
5
6pub use sink_ext::SinkExt;
7
8/// A `Sink` is a value into which other values can be sent in pure async/await.
9#[must_use = "sinks do nothing unless polled"]
10pub trait Sink<Item> {
11    /// The type of value produced by the sink when an error occurs.
12    type Error;
13
14    /// Send item.
15    fn send(&mut self, item: Item) -> impl Future<Output = Result<(), Self::Error>>;
16
17    /// Flush any remaining output from this sink.
18    fn flush(&mut self) -> impl Future<Output = Result<(), Self::Error>>;
19
20    /// Flush any remaining output and close this sink, if necessary.
21    fn close(&mut self) -> impl Future<Output = Result<(), Self::Error>>;
22}
23
24impl<T, S: ?Sized + Sink<T>> Sink<T> for &mut S {
25    type Error = S::Error;
26
27    fn send(&mut self, item: T) -> impl Future<Output = Result<(), Self::Error>> {
28        (**self).send(item)
29    }
30
31    fn flush(&mut self) -> impl Future<Output = Result<(), Self::Error>> {
32        (**self).flush()
33    }
34
35    fn close(&mut self) -> impl Future<Output = Result<(), Self::Error>> {
36        (**self).close()
37    }
38}