use core::pin::Pin;
use pin_project_lite::pin_project;
use crate::{Sink, SinkBuild, forward_sink};
pin_project! {
#[must_use = "sinks do nothing unless polled"]
pub struct Map<Si, Func> {
#[pin]
sink: Si,
func: Func,
}
}
impl<Si, Func> Map<Si, Func> {
pub fn new<Item>(func: Func, sink: Si) -> Self
where
Self: Sink<Item>,
{
Self { sink, func }
}
}
impl<Si, Func, Item, ItemOut> Sink<Item> for Map<Si, Func>
where
Si: Sink<ItemOut>,
Func: FnMut(Item) -> ItemOut,
{
type Error = Si::Error;
fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error> {
let this = self.project();
let item = (this.func)(item);
this.sink.start_send(item)
}
forward_sink!(poll_ready, poll_flush, poll_close);
}
pub struct MapBuilder<Prev, Func> {
pub(crate) prev: Prev,
pub(crate) func: Func,
}
impl<Prev, ItemOut, Func> SinkBuild for MapBuilder<Prev, Func>
where
Prev: SinkBuild,
Func: FnMut(Prev::Item) -> ItemOut,
{
type Item = ItemOut;
type Output<Next: Sink<ItemOut>> = Prev::Output<Map<Next, Func>>;
fn send_to<Next>(self, next: Next) -> Self::Output<Next>
where
Next: Sink<ItemOut>,
{
self.prev.send_to(Map::new(self.func, next))
}
}