diff --git a/crates/core/src/handler.rs b/crates/core/src/handler.rs index e6e65a6ee..b0704aeaf 100644 --- a/crates/core/src/handler.rs +++ b/crates/core/src/handler.rs @@ -145,6 +145,15 @@ pub trait Handler: Send + Sync + 'static { ctrl: &mut FlowCtrl, ); + /// Wrap to `ArcHandler`. + #[inline] + fn arc(self) -> ArcHandler + where + Self: Sized, + { + ArcHandler(Arc::new(self)) + } + /// Wrap to `HoopedHandler`. #[inline] fn hooped(self) -> HoopedHandler @@ -177,6 +186,23 @@ pub trait Handler: Send + Sync + 'static { } } +/// A handler that wraps another [Handler] to enable it to be cloneable. +#[derive(Clone)] +pub struct ArcHandler(Arc); + +#[async_trait] +impl Handler for ArcHandler { + async fn handle( + &self, + req: &mut Request, + depot: &mut Depot, + res: &mut Response, + ctrl: &mut FlowCtrl, + ) { + self.0.handle(req, depot, res, ctrl).await + } +} + #[doc(hidden)] pub struct EmptyHandler; #[async_trait]