teo_runtime/middleware/
next.rs

1use std::future::Future;
2use std::sync::Arc;
3use futures_util::future::BoxFuture;
4use crate::request::Request;
5use crate::response::Response;
6
7pub trait NextImp: Send + Sync {
8    fn call(&self, request: Request) -> BoxFuture<'static, teo_result::Result<Response>>;
9}
10
11impl<F, Fut> NextImp for F where
12    F: Fn(Request) -> Fut + Sync + Send,
13    Fut: Future<Output =teo_result::Result<Response>> + Send + 'static {
14    fn call(&self, ctx: Request) -> BoxFuture<'static, teo_result::Result<Response>> {
15        Box::pin(self(ctx))
16    }
17}
18
19#[derive(Clone)]
20#[repr(transparent)]
21pub struct Next {
22    pub imp: Arc<dyn NextImp>,
23}
24
25impl Next {
26
27    #[inline]
28    pub fn new<F>(f: F) -> Self where F: NextImp + 'static {
29        Self {
30            imp: Arc::new(f),
31        }
32    }
33}
34
35impl NextImp for Next {
36    fn call(&self, request: Request) -> BoxFuture<'static, teo_result::Result<Response>> {
37        self.imp.call(request)
38    }
39}