use std::collections::HashMap;
use std::hash::Hash;
use std::ops::ControlFlow;
use std::sync::Arc;
use super::stream::Stream;
use crate::capability::HNil;
use crate::coordination::Queue;
use crate::failure::Never;
use crate::kernel::Effect;
type Driver<A, In, E, R> = Arc<dyn Fn(Stream<In, E, R>) -> Effect<A, E, R> + Send + Sync>;
pub(crate) struct FoldState<A, In> {
pub(crate) init: A,
pub(crate) step: Arc<dyn Fn(A, In) -> A + Send + Sync>,
}
pub struct Sink<A, In, E = Never, R = HNil> {
driver: Driver<A, In, E, R>,
pub(crate) fold: Option<Arc<FoldState<A, In>>>,
}
impl<A, In, E, R> Clone for Sink<A, In, E, R> {
fn clone(&self) -> Self {
Self {
driver: Arc::clone(&self.driver),
fold: self.fold.clone(),
}
}
}
impl<A, In, E, R> std::fmt::Debug for Sink<A, In, E, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Sink")
.field("fold", &self.fold.is_some())
.finish_non_exhaustive()
}
}
impl<A, In, E, R> Sink<A, In, E, R>
where
A: Send + 'static,
In: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
pub fn new(driver: impl Fn(Stream<In, E, R>) -> Effect<A, E, R> + Send + Sync + 'static) -> Self {
Self {
driver: Arc::new(driver),
fold: None,
}
}
pub fn run(&self, stream: Stream<In, E, R>) -> Effect<A, E, R> {
(self.driver)(stream)
}
}
impl<A, In, E, R> Sink<A, In, E, R>
where
A: Clone + Send + Sync + 'static,
In: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
pub fn fold_left(init: A, f: impl Fn(A, In) -> A + Send + Sync + 'static) -> Self {
Self::from_fold_state(FoldState {
init,
step: Arc::new(f),
})
}
pub fn from_fold(init: A, f: impl Fn(A, In) -> A + Send + Sync + 'static) -> Self {
Self::fold_left(init, f)
}
fn from_fold_state(state: FoldState<A, In>) -> Self {
let state = Arc::new(state);
let st = Arc::clone(&state);
Self {
driver: Arc::new(move |stream: Stream<In, E, R>| {
let step = Arc::clone(&st.step);
stream.run_fold(st.init.clone(), move |acc, x| step(acc, x))
}),
fold: Some(state),
}
}
pub fn zip<B>(self, other: Sink<B, In, E, R>) -> Sink<(A, B), In, E, R>
where
B: Clone + Send + Sync + 'static,
In: Clone,
{
let sa = self
.fold
.expect("Sink::zip requires fold_left/from_fold sinks");
let sb = other
.fold
.expect("Sink::zip requires fold_left/from_fold sinks");
let (fa, fb) = (Arc::clone(&sa.step), Arc::clone(&sb.step));
Sink::from_fold_state(FoldState {
init: (sa.init.clone(), sb.init.clone()),
step: Arc::new(move |(a, b), x: In| (fa(a, x.clone()), fb(b, x))),
})
}
}
impl<In, E, R> Sink<Vec<In>, In, E, R>
where
In: Clone + Send + Sync + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
pub fn collect() -> Self {
Sink::fold_left(Vec::new(), |mut v, x| {
v.push(x);
v
})
}
pub fn collect_all_while(pred: impl Fn(&In) -> bool + Send + Sync + 'static) -> Self {
let pred = Arc::new(pred);
Sink::new(move |stream: Stream<In, E, R>| {
let pred = Arc::clone(&pred);
stream.fold_chunks(Vec::new(), move |mut out, items| {
for x in items {
if !pred(&x) {
return ControlFlow::Break(out);
}
out.push(x);
}
ControlFlow::Continue(out)
})
})
}
pub fn collect_all_until(pred: impl Fn(&In) -> bool + Send + Sync + 'static) -> Self {
Self::collect_all_while(move |x| !pred(x))
}
}
impl<In, E, R> Sink<(), In, E, R>
where
In: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
pub fn drain() -> Self {
Sink::fold_left((), |(), _| ())
}
pub fn for_each(f: impl Fn(In) -> Effect<(), E, R> + Send + Sync + 'static) -> Self {
let f = Arc::new(f);
Sink::new(move |stream: Stream<In, E, R>| {
let f = Arc::clone(&f);
stream.run_for_each_effect(move |x| f(x))
})
}
pub fn to_queue(queue: Queue<In>) -> Self {
Sink::new(move |stream: Stream<In, E, R>| {
let queue = queue.clone();
stream.run_for_each_effect(move |x| queue.offer(x).map(|_| ()))
})
}
}
impl<K, V, E, R> Sink<HashMap<K, V>, (K, V), E, R>
where
K: Hash + Eq + Clone + Send + Sync + 'static,
V: Clone + Send + Sync + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
pub fn collect_to_map() -> Self {
Sink::fold_left(HashMap::new(), |mut m, (k, v)| {
m.insert(k, v);
m
})
}
}