Skip to main content

a3s_boot/pipeline/
guard.rs

1use super::ExecutionContext;
2use crate::{BoxFuture, Result};
3use std::sync::Arc;
4
5/// Decides whether a route handler can run.
6pub trait Guard: Send + Sync + 'static {
7    fn can_activate(&self, context: ExecutionContext) -> BoxFuture<'static, Result<bool>>;
8}
9
10impl<F, Fut> Guard for F
11where
12    F: Fn(ExecutionContext) -> Fut + Send + Sync + 'static,
13    Fut: std::future::Future<Output = Result<bool>> + Send + 'static,
14{
15    fn can_activate(&self, context: ExecutionContext) -> BoxFuture<'static, Result<bool>> {
16        Box::pin(self(context))
17    }
18}
19
20impl Guard for Arc<dyn Guard> {
21    fn can_activate(&self, context: ExecutionContext) -> BoxFuture<'static, Result<bool>> {
22        self.as_ref().can_activate(context)
23    }
24}