sheave_core/handlers/
middlewares.rs

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
use std::{
    io::Result as IOResult,
    pin::Pin,
    task::{
        Context as FutureContext,
        Poll
    }
};
use pin_project_lite::pin_project;
use super::{
    AsyncHandler,
    RtmpContext
};

pub trait Middleware {
    fn poll_handle_wrapped<H: AsyncHandler + Unpin>(self: Pin<&mut Self>, cx: &mut FutureContext<'_>, rtmp_context: &mut RtmpContext, handler: Pin<&mut H>) -> Poll<IOResult<()>>;
}

pin_project! {
    #[derive(Debug)]
    pub struct Wrap<M, H> {
        #[pin] middleware: M,
        #[pin] handler: H
    }
}

impl<M, H> AsyncHandler for Wrap<M, H>
where
    M: Middleware + Unpin,
    H: AsyncHandler + Unpin
{
    fn poll_handle(self: Pin<&mut Self>, cx: &mut FutureContext<'_>, rtmp_context: &mut RtmpContext) -> Poll<IOResult<()>> {
        let this = self.project();
        this.middleware.poll_handle_wrapped(cx, rtmp_context, this.handler)
    }
}

pub fn wrap<M, H>(middleware: M, handler: H) -> Wrap<M, H>
where
    M: Middleware + Unpin,
    H: AsyncHandler + Unpin
{
    Wrap { middleware, handler }
}