flowly-service 0.4.13

Flowly is a library of modular and reusable components for building robust pipelines processing audio, video and other.
Documentation
use std::marker::PhantomData;

use futures::{Stream, StreamExt};

use crate::Service;

#[allow(dead_code)]
#[derive(Clone)]
pub struct Filter<I, F, S> {
    f: F,
    s: S,
    _m: PhantomData<I>,
}

impl<I, F, S> Service<I> for Filter<I, F, S>
where
    S: Service<I>,
    S::Out: Send,
    F: Fn(&I) -> bool,
{
    type Out = S::Out;

    fn handle(&mut self, input: I, cx: &crate::Context) -> impl Stream<Item = Self::Out> {
        if (self.f)(&input) {
            self.s.handle(input, cx).left_stream()
        } else {
            futures::stream::empty().right_stream()
        }
    }
}