Skip to content

Instantly share code, notes, and snippets.

@Agnishom
Created May 26, 2021 15:17
Show Gist options
  • Save Agnishom/41ab42217a30e5d72bc6105b27bceaaf to your computer and use it in GitHub Desktop.
Save Agnishom/41ab42217a30e5d72bc6105b27bceaaf to your computer and use it in GitHub Desktop.
Composable Transductions
use std::marker::PhantomData;
pub trait Sink<A> {
fn init(&mut self);
fn next(&mut self, item: A);
fn end(&mut self);
}
pub trait Query<A,B>: {
fn init(&mut self, sink: &mut dyn Sink<B>);
fn next(&mut self, item: A, sink: &mut dyn Sink<B>);
fn end(&mut self, sink: &mut dyn Sink<B>);
}
pub struct Pipeline<'a, A, B, C, QAB: Query<A, B>, QBC: Query<B, C> > {
q_ab: &'a mut QAB,
q_bc: &'a mut QBC,
_abc : PhantomData<(A, B, C)>
}
impl<'a, A, B, C, QAB: Query<A, B>, QBC: Query<B, C>> Pipeline<'a, A, B, C, QAB, QBC>{
pub fn new(q1: &'a mut QAB, q2: &'a mut QBC) -> Self{
Self {q_ab : q1, q_bc: q2, _abc:PhantomData}
}
}
struct PushSink<'a, B, C, QBC : Query<B, C>, S : Sink<C> + ?Sized> {
q_bc : &'a mut QBC,
s_c : &'a mut S,
_bc : PhantomData<(B, C)>
}
impl <'a, B, C, QBC: Query<B, C>, S: Sink<C> + ?Sized> PushSink<'a, B, C, QBC, S>{
pub fn new(q_bc: &'a mut QBC, s_c : &'a mut S) -> Self{
PushSink {q_bc : q_bc, s_c : s_c, _bc : PhantomData}
}
}
impl<'a, B, C, QBC: Query<B, C>, S: Sink<C> + ?Sized> Sink<B> for PushSink<'a, B, C, QBC, S>{
fn init(&mut self) {
self.q_bc.init(self.s_c);
}
fn next(&mut self, item: B) {
self.q_bc.next(item, self.s_c);
}
fn end(&mut self) {
self.q_bc.end(self.s_c);
}
}
impl<'a, A, B, C, QAB: Query<A, B>, QBC: Query<B, C>> Query<A, C> for Pipeline<'a, A, B, C, QAB, QBC>{
fn init(&mut self, sink: &mut dyn Sink<C>){
self.q_ab.init(&mut PushSink::new(self.q_bc, sink))
}
fn next(&mut self, item: A, sink: &mut dyn Sink<C>){
self.q_ab.next(item, &mut PushSink::new(self.q_bc, sink))
}
fn end(&mut self, sink: &mut dyn Sink<C>){
self.q_ab.end(&mut PushSink::new(self.q_bc, sink))
}
}
fn main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment