1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//! Трейт для функции компонента
//!
//! https://stackoverflow.com/questions/58173711/how-can-i-store-an-async-function-in-a-struct-and-call-it-from-a-struct-instance
//!

use std::future::Future;

use futures::future::BoxFuture;

use crate::types::{Input, Output};

/// Трейт для функции компонента
pub trait IComponentFunction<TMessage, TConfig>: Send {
    fn call(
        &self,
        stream_input: Input<TMessage>,
        stream_output: Output<TMessage>,
        config: TConfig,
    ) -> BoxFuture<'static, ()>;
}

impl<T, F, TMessage, TConfig> IComponentFunction<TMessage, TConfig> for T
where
    T: Fn(Input<TMessage>, Output<TMessage>, TConfig) -> F + Send,
    F: Future<Output = ()> + 'static + Send,
{
    fn call(
        &self,
        stream_input: Input<TMessage>,
        stream_output: Output<TMessage>,
        config: TConfig,
    ) -> BoxFuture<'static, ()> {
        Box::pin(self(stream_input, stream_output, config))
    }
}