Skip to main content

remotia_core/processors/
async_functional.rs

1use std::pin::Pin;
2
3use async_trait::async_trait;
4use futures::Future;
5
6use crate::{traits::FrameProcessor};
7
8pub type PinnedFrameData<F> = Pin<Box<dyn Future<Output = Option<F>> + Send>>;
9type AsyncProcessorFn<F> = fn(F) -> PinnedFrameData<F>;
10
11pub struct AsyncFunction<F> {
12    function: AsyncProcessorFn<F>
13}
14
15impl<F> AsyncFunction<F> {
16    pub fn new(function: AsyncProcessorFn<F>) -> Self {
17        Self {
18            function,
19        }
20    }
21}
22
23#[async_trait]
24impl<F> FrameProcessor<F> for AsyncFunction<F> where
25    F: Send
26{
27    async fn process(&mut self, frame_data: F) -> Option<F> {
28        (self.function)(frame_data).await
29    }
30}
31
32#[macro_export]
33macro_rules! async_func {
34    (async move $body:block) => {
35        Box::pin(async move { $body })
36    };
37}