sheave_core/handlers/
middlewares.rs

1use std::{
2    io::Result as IOResult,
3    pin::Pin,
4    task::{
5        Context as FutureContext,
6        Poll
7    }
8};
9use pin_project_lite::pin_project;
10use super::{
11    AsyncHandler,
12    RtmpContext
13};
14
15pub trait Middleware {
16    fn poll_handle_wrapped<H: AsyncHandler + Unpin>(self: Pin<&mut Self>, cx: &mut FutureContext<'_>, rtmp_context: &mut RtmpContext, handler: Pin<&mut H>) -> Poll<IOResult<()>>;
17}
18
19pin_project! {
20    #[derive(Debug)]
21    pub struct Wrap<M, H> {
22        #[pin] middleware: M,
23        #[pin] handler: H
24    }
25}
26
27impl<M, H> AsyncHandler for Wrap<M, H>
28where
29    M: Middleware + Unpin,
30    H: AsyncHandler + Unpin
31{
32    fn poll_handle(self: Pin<&mut Self>, cx: &mut FutureContext<'_>, rtmp_context: &mut RtmpContext) -> Poll<IOResult<()>> {
33        let this = self.project();
34        this.middleware.poll_handle_wrapped(cx, rtmp_context, this.handler)
35    }
36}
37
38pub fn wrap<M, H>(middleware: M, handler: H) -> Wrap<M, H>
39where
40    M: Middleware + Unpin,
41    H: AsyncHandler + Unpin
42{
43    Wrap { middleware, handler }
44}