use core::pin::Pin;
use core::task::{Context, Poll};
use pin_project_lite::pin_project;
use crate::Sink;
pin_project! {
#[must_use = "sinks do nothing unless polled"]
pub struct ForEach<Func> {
func: Func,
}
}
impl<Func> ForEach<Func> {
pub fn new<Item>(func: Func) -> Self
where
Self: Sink<Item>,
{
Self { func }
}
}
impl<Func, Item> Sink<Item> for ForEach<Func>
where
Func: FnMut(Item),
{
type Error = core::convert::Infallible;
fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error> {
(self.project().func)(item);
Ok(())
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
}