use crate::{collector::*, mapper::Mapper};
pub struct FnCollector<TFn>(pub TFn);
impl<'a,T,TFn> Mapper<T> for &'a FnCollector<TFn> {
type Output=T;
fn map(self,value:T)->Self::Output {
value
}
}
impl<'a,Input,Next,Output,TFn> Collector<Input,Next> for &'a FnCollector<TFn>
where TFn:Fn(Input,Next)->Output
{
type Output=Output;
fn collect(self,value:Input,next:Next)-><Self as Collector<Input,Next>>::Output {
(self.0)(value,next)
}
}
pub struct FnMutCollector<TFn>(pub TFn);
impl<'a,T,TFn> Mapper<T> for &'a mut FnMutCollector<TFn> {
type Output=T;
fn map(self,value:T)->Self::Output {
value
}
}
impl<'a,Input,Next,Output,TFn> Collector<Input,Next> for &'a mut FnMutCollector<TFn>
where TFn:FnMut(Input,Next)->Output
{
type Output=Output;
fn collect(self,value:Input,next:Next)-><Self as Collector<Input,Next>>::Output {
(self.0)(value,next)
}
}
pub struct MapCollector<TMapper:Clone,TCollector:Clone>(pub TMapper,pub TCollector);
impl<'a,TMapper,TCollector,Input> Mapper<Input> for &'a MapCollector<TMapper,TCollector>
where TMapper:Mapper<Input>+Clone,
TCollector:Mapper< <TMapper as Mapper<Input>>::Output >+Clone
{
type Output=< TCollector as Mapper< <TMapper as Mapper<Input>>::Output >>::Output;
fn map(self,value:Input)->Self::Output {
self.1.clone().map(self.0.clone().map(value))
}
}
impl <'a,TMapper,TCollector,Input,TNext> Collector<Input,TNext> for &'a MapCollector<TMapper,TCollector>
where TMapper:Mapper<Input>+Clone,
TCollector:Collector<<TMapper as Mapper<Input>>::Output,TNext>+Clone
{
type Output= <TCollector as Collector<<TMapper as Mapper<Input>>::Output,TNext>>::Output ;
fn collect(self,value:Input,next:TNext)-><Self as Collector<Input,TNext>>::Output {
self.1.clone().collect(self.0.clone().map(value), next)
}
}
impl Mapper<()> for () {
type Output=();
fn map(self,_value:())->Self::Output {()}
}
impl Collector<(),()> for () {
type Output=();
fn collect(self,_value:(),_next:())->() {()}
}