Skip to content

Commit

Permalink
feat(core): Add ArcHandler (#958)
Browse files Browse the repository at this point in the history
  • Loading branch information
andeya authored Oct 25, 2024
1 parent 05684e6 commit 1ad59e2
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions crates/core/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<H: Handler>(self) -> HoopedHandler
Expand Down Expand Up @@ -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<dyn Handler>);

#[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]
Expand Down

0 comments on commit 1ad59e2

Please sign in to comment.