use crate::io::Stream;
#[allow(async_fn_in_trait)]
pub trait Sink<Item> {
type Error;
async fn send(&mut self, item: Item) -> Result<(), Self::Error>;
async fn flush(&mut self) -> Result<(), Self::Error>;
async fn close(&mut self) -> Result<(), Self::Error>;
}
impl<T, S: ?Sized + Sink<T>> Sink<T> for &mut S {
type Error = S::Error;
#[inline]
async fn send(&mut self, item: T) -> Result<(), Self::Error> {
(**self).send(item).await
}
#[inline]
async fn flush(&mut self) -> Result<(), Self::Error> {
(**self).flush().await
}
#[inline]
async fn close(&mut self) -> Result<(), Self::Error> {
(**self).close().await
}
}
#[allow(async_fn_in_trait)]
pub trait SinkExt<Item>: Sink<Item> {
async fn send_all<S>(&mut self, stream: &mut S) -> Result<(), Self::Error>
where
S: Stream<Item = Item> + ?Sized,
{
while let Some(item) = stream.next().await {
self.send(item).await?;
}
self.flush().await
}
}
impl<Item, S: Sink<Item> + ?Sized> SinkExt<Item> for S {}