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>,
}
impl<A, In, E, R> Clone for Sink<A, In, E, R> {
fn clone(&self) -> Self {
Self {
driver: Arc::clone(&self.driver),
}
}
}
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").finish_non_exhaustive()
}
}
pub struct FoldSink<A, In, E = Never, R = HNil> {
sink: Sink<A, In, E, R>,
pub(crate) fold: Arc<FoldState<A, In>>,
}
impl<A, In, E, R> Clone for FoldSink<A, In, E, R> {
fn clone(&self) -> Self {
Self {
sink: self.sink.clone(),
fold: Arc::clone(&self.fold),
}
}
}
impl<A, In, E, R> std::fmt::Debug for FoldSink<A, In, E, R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FoldSink").finish_non_exhaustive()
}
}
impl<A, In, E, R> From<FoldSink<A, In, E, R>> for Sink<A, In, E, R> {
fn from(fold: FoldSink<A, In, E, R>) -> Self {
fold.sink
}
}
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),
}
}
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,
) -> FoldSink<A, In, E, R> {
FoldSink::from_state(FoldState {
init,
step: Arc::new(f),
})
}
pub fn from_fold(
init: A,
f: impl Fn(A, In) -> A + Send + Sync + 'static,
) -> FoldSink<A, In, E, R> {
Self::fold_left(init, f)
}
}
impl<A, In, E, R> FoldSink<A, In, E, R>
where
A: Clone + Send + Sync + 'static,
In: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
fn from_state(state: FoldState<A, In>) -> Self {
let fold = Arc::new(state);
let st = Arc::clone(&fold);
let sink = Sink::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))
});
Self { sink, fold }
}
pub fn run(&self, stream: Stream<In, E, R>) -> Effect<A, E, R> {
self.sink.run(stream)
}
pub fn zip<B>(self, other: FoldSink<B, In, E, R>) -> FoldSink<(A, B), In, E, R>
where
B: Clone + Send + Sync + 'static,
In: Clone,
{
let (sa, sb) = (self.fold, other.fold);
let (fa, fb) = (Arc::clone(&sa.step), Arc::clone(&sb.step));
FoldSink::from_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() -> FoldSink<Vec<In>, In, E, R> {
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() -> FoldSink<(), In, E, R> {
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() -> FoldSink<HashMap<K, V>, (K, V), E, R> {
Sink::fold_left(HashMap::new(), |mut m, (k, v)| {
m.insert(k, v);
m
})
}
}