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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::{future::Future, pin::Pin, sync::Arc};
use async_trait::async_trait;
use crate::{Framework, Pipeline, RawFramework, RawPipeline};

#[async_trait]
pub trait RawAsyncPipeline<VT, RT, ET> {
  async fn run(&self,value: VT) -> Result<RT, ET>;
}
pub struct AsyncPipeline<VT, RT, ET> {
  raw: Arc<dyn RawAsyncPipeline<VT, RT, ET> + Sync + Send + 'static>,
}

impl<VT: 'static,RT: 'static,ET: 'static> AsyncPipeline<VT,RT,ET> {
  pub fn new<EfffectorT: RawAsyncPipeline<VT,RT,ET> + Sync + Send + 'static>(raw: EfffectorT) -> Pipeline<VT,RT,ET> {
    return Pipeline::new(AsyncPipeline{
      raw: Arc::new(raw)
    });
  }
}

impl<VT,RT,ET> RawPipeline<VT,RT,ET> for AsyncPipeline<VT,RT,ET> {
  fn run(&self,value: VT) -> Result<RT, ET> {
    let rt  = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async {
      self.raw.run(value).await
    })
  }
}

#[async_trait]
pub trait RawAsyncFramework<VT,RT,ET> {
  async fn run(&self, pipeline: Pipeline<VT,RT,ET>);
}

pub struct AsyncFramework<VT,RT,ET> {
  raw: Arc<dyn RawAsyncFramework<VT,RT,ET>>
}

impl<VT: 'static,RT: 'static,ET: 'static> AsyncFramework<VT,RT,ET> {
  pub fn new<FT: RawAsyncFramework<VT,RT,ET> + 'static>(f: FT) -> Framework<VT, RT, ET> {
    Framework::new(
      AsyncFramework {
        raw: Arc::new(f)
      }
    ) 
  }
}

impl<VT,RT,ET> RawFramework<VT,RT,ET> for AsyncFramework<VT,RT,ET> {
  fn run(&self, pipeline: Pipeline<VT,RT,ET>) {
    let rt  = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(async {
      self.raw.run(pipeline).await
    });
  }
}