hyperlane/hook/
trait.rs

1use crate::*;
2
3/// A generic trait for functions that take a `Context` and return a value.
4///
5/// This trait encapsulates the common behavior of being a sendable, synchronous
6/// function that accepts a `Context`. It is used as a base for other, more
7/// specific function traits.
8pub trait FnContextSendSync<R>: Fn(Context) -> R + Send + Sync {}
9
10/// A trait for functions that return a pinned, boxed, sendable future.
11///
12/// This trait is essential for creating type-erased async function pointers,
13/// which is a common pattern for storing and dynamically dispatching different
14/// asynchronous handlers in a collection.
15pub trait FnContextPinBoxSendSync<T>: FnContextSendSync<SendableAsyncTask<T>> {}
16
17/// A trait for static, sendable, synchronous functions that return a future.
18///
19/// This trait ensures that a handler function is safe to be sent across threads
20/// and has a static lifetime, making it suitable for use in long-lived components
21/// of the application, such as the main router.
22pub trait FnContextSendSyncStatic<Fut, T>: FnContextSendSync<Fut> + 'static
23where
24    Fut: Future<Output = T> + Send,
25{
26}
27
28/// A trait for futures that are sendable and have a static lifetime.
29///
30/// This marker trait simplifies generic bounds for asynchronous operations, ensuring
31/// that futures can be safely managed by the async runtime without lifetime issues.
32pub trait FutureSendStatic<T>: Future<Output = T> + Send + 'static {}
33
34/// A trait for `Send`-able futures with a generic output.
35pub trait FutureSend<T>: Future<Output = T> + Send {}
36
37/// A trait for thread-safe, reference-counted closures that produce a sendable async task.
38pub trait FnPinBoxFutureSend<T>: Fn() -> SendableAsyncTask<T> + Send + Sync {}
39
40/// Trait for server lifecycle hooks that process requests.
41///
42/// `ServerHook` provides a unified interface for different types of request processing
43/// handlers in the server lifecycle, including route handlers, middleware, and panic hooks.
44/// All hooks follow the same pattern: instantiation via `new` and execution via `handle`.
45///
46/// This trait is designed to work with the server's request processing pipeline, where
47/// each hook receives the `Context` directly for both initialization and processing.
48pub trait ServerHook: Send + Sync + 'static {
49    /// Creates a new instance of this hook from the context.
50    ///
51    /// This method is called by the framework to instantiate the hook,
52    /// passing in the `Context` directly.
53    ///
54    /// # Arguments
55    ///
56    /// - `&Context` - The request context containing all request/response data.
57    ///
58    /// # Returns
59    ///
60    /// A future that resolves to a new instance of this hook.
61    fn new(ctx: &Context) -> impl Future<Output = Self> + Send;
62
63    /// Executes the hook's processing logic.
64    ///
65    /// This method contains the actual logic for processing the request.
66    /// It receives the `Context` as a parameter for accessing request/response data.
67    ///
68    /// # Arguments
69    ///
70    /// - `&Context` - The request context for accessing request/response data.
71    ///
72    /// # Returns
73    ///
74    /// A future that resolves when the processing is complete.
75    fn handle(self, ctx: &Context) -> impl Future<Output = ()> + Send;
76}