Skip to main content

remotia_core/processors/
functional.rs

1use std::marker::PhantomData;
2
3use async_trait::async_trait;
4
5use crate::{pipeline::component::Component, traits::FrameProcessor};
6
7pub struct Function<F> {
8    function: fn(F) -> Option<F>,
9}
10
11impl<F> Function<F> {
12    pub fn new(function: fn(F) -> Option<F>) -> Self {
13        Self { function }
14    }
15}
16
17#[async_trait]
18impl<F: Send> FrameProcessor<F> for Function<F> {
19    async fn process(&mut self, frame_data: F) -> Option<F> {
20        (self.function)(frame_data)
21    }
22}
23
24pub struct Closure<FD, FN>
25where
26    FN: Fn(FD) -> Option<FD>,
27{
28    function: FN,
29    data_type: PhantomData<FD>,
30}
31
32impl<FD, FN> Closure<FD, FN>
33where
34    FN: Fn(FD) -> Option<FD>,
35{
36    pub fn new(function: FN) -> Self {
37        Self {
38            function,
39            data_type: PhantomData,
40        }
41    }
42}
43
44#[async_trait]
45impl<FD, FN> FrameProcessor<FD> for Closure<FD, FN>
46where
47    FD: Send,
48    FN: Fn(FD) -> Option<FD> + Send,
49{
50    async fn process(&mut self, frame_data: FD) -> Option<FD> {
51        (self.function)(frame_data)
52    }
53}
54
55pub trait ClosureAppends<FN> {
56    fn closure(self, closure: FN) -> Self;
57}
58
59impl<FD, FN> ClosureAppends<FN> for Component<FD>
60where
61    FD: Default + Send + 'static,
62    FN: Fn(FD) -> Option<FD> + Send + 'static,
63{
64    fn closure(self, closure: FN) -> Self {
65        self.append(Closure::new(closure))
66    }
67}