hyperlane/hook/type.rs
1use crate::*;
2
3/// A type alias for a shared hook hook.
4///
5/// This type is used for storing handlers in a shared context, allowing multiple
6/// parts of the application to safely access and execute the same hook.
7pub type HookHandler<T> = Arc<dyn FnContextPinBoxSendSync<T>>;
8
9/// A type alias for a hook hook chain.
10///
11/// This type is used to represent a chain of middleware or hooks that can be
12/// executed sequentially.
13pub type HookHandlerChain<T> = Vec<HookHandler<T>>;
14
15/// A type alias for an asynchronous task.
16///
17/// This is a common return type for asynchronous handlers, providing a type-erased
18/// future that can be easily managed by the async runtime.
19pub type AsyncTask = Pin<Box<dyn Future<Output = ()> + Send + 'static>>;
20
21/// A type alias for a sendable asynchronous task with a generic output.
22///
23/// This is often used to represent an asynchronous task that can be sent across threads.
24pub type SendableAsyncTask<T> = Pin<Box<dyn Future<Output = T> + Send>>;
25
26/// A type alias for a shared asynchronous task factory.
27///
28/// This is useful for creating and sharing asynchronous task factories.
29pub type SharedAsyncTaskFactory<T> = Arc<dyn FnPinBoxFutureSend<T>>;
30
31/// A type alias for a hook hook factory function.
32///
33/// This function pointer type is used to create ServerHookHandler instances
34/// based on generic types. It allows delayed instantiation of hooks.
35pub type ServerHookHandlerFactory = fn() -> ServerHookHandler;
36
37/// Type alias for a shared server hook hook.
38///
39/// This type allows storing handlers (route and middleware) of different concrete types
40/// in the same collection. The hook takes a `&Context` and returns
41/// a pinned, boxed future that resolves to `()`.
42pub type ServerHookHandler = Arc<dyn Fn(&Context) -> SendableAsyncTask<()> + Send + Sync>;
43
44/// Type alias for a list of server hooks.
45///
46/// Used to store middleware handlers in the request/response processing pipeline.
47pub type ServerHookList = Vec<ServerHookHandler>;
48
49/// Type alias for a map of server hook handlers.
50///
51/// Used for fast lookup of exact-match route.
52pub type ServerHookMap = HashMapXxHash3_64<String, ServerHookHandler>;
53
54/// Type alias for a collection of pattern-based server hook route grouped by segment count.
55///
56/// The outer HashMap uses segment count as key for fast filtering.
57/// The inner Vec stores patterns with the same segment count, maintaining insertion order.
58pub type ServerHookPatternRoute = HashMapXxHash3_64<usize, Vec<(RoutePattern, ServerHookHandler)>>;