sheave_core/handlers/
middlewares.rs1use 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 #[doc(hidden)]
21 #[derive(Debug)]
22 pub struct Wrap<M, H> {
23 #[pin] middleware: M,
24 #[pin] handler: H
25 }
26}
27
28#[doc(hidden)]
29impl<M, H> AsyncHandler for Wrap<M, H>
30where
31 M: Middleware + Unpin,
32 H: AsyncHandler + Unpin
33{
34 fn poll_handle(self: Pin<&mut Self>, cx: &mut FutureContext<'_>, rtmp_context: &mut RtmpContext) -> Poll<IOResult<()>> {
35 let this = self.project();
36 this.middleware.poll_handle_wrapped(cx, rtmp_context, this.handler)
37 }
38}
39
40#[doc(hidden)]
41pub fn wrap<M, H>(middleware: M, handler: H) -> Wrap<M, H>
42where
43 M: Middleware + Unpin,
44 H: AsyncHandler + Unpin
45{
46 Wrap { middleware, handler }
47}