diff options
| author | Fuwn <[email protected]> | 2026-04-14 09:05:03 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-04-14 09:05:03 +0000 |
| commit | 92180ab10c5cd1394b3c288e4d730aa37c527629 (patch) | |
| tree | 17c1495e9724662206ef8c42091f972a0f2bc243 | |
| parent | perf(router)!: Remove AsyncMutex from route and error handlers (diff) | |
| download | archived-windmark-92180ab10c5cd1394b3c288e4d730aa37c527629.tar.xz archived-windmark-92180ab10c5cd1394b3c288e4d730aa37c527629.zip | |
perf(router)!: Pass HookContext by reference to modules and hooks
| -rw-r--r-- | examples/async_stateful_module.rs | 4 | ||||
| -rw-r--r-- | examples/callbacks.rs | 4 | ||||
| -rw-r--r-- | examples/stateful_module.rs | 4 | ||||
| -rw-r--r-- | src/handler/hooks/post_route.rs | 6 | ||||
| -rw-r--r-- | src/handler/hooks/pre_route.rs | 6 | ||||
| -rw-r--r-- | src/module/asynchronous.rs | 4 | ||||
| -rw-r--r-- | src/module/sync.rs | 4 | ||||
| -rw-r--r-- | src/router.rs | 26 |
8 files changed, 26 insertions, 32 deletions
diff --git a/examples/async_stateful_module.rs b/examples/async_stateful_module.rs index 53d88be..6391611 100644 --- a/examples/async_stateful_module.rs +++ b/examples/async_stateful_module.rs @@ -13,7 +13,7 @@ impl windmark::module::AsyncModule for Clicker { println!("module 'clicker' has been attached!"); } - async fn on_pre_route(&mut self, context: HookContext) { + async fn on_pre_route(&mut self, context: &HookContext) { *self.clicks.lock().unwrap() += 1; println!( @@ -23,7 +23,7 @@ impl windmark::module::AsyncModule for Clicker { ); } - async fn on_post_route(&mut self, context: HookContext) { + async fn on_post_route(&mut self, context: &HookContext) { println!( "module 'clicker' clicker has been called after the route '{}' with {} \ clicks!", diff --git a/examples/callbacks.rs b/examples/callbacks.rs index 76014d5..e9b330d 100644 --- a/examples/callbacks.rs +++ b/examples/callbacks.rs @@ -8,7 +8,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { .set_private_key_file("windmark_private.pem") .set_certificate_file("windmark_public.pem") .mount("/", windmark::success!("Hello!")) - .set_pre_route_callback(|context: HookContext| { + .set_pre_route_callback(|context: &HookContext| { println!( "accepted connection from {} to {}", context.peer_address.unwrap().ip(), @@ -16,7 +16,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { ) }) .set_post_route_callback( - |context: HookContext, content: &mut windmark::response::Response| { + |context: &HookContext, content: &mut windmark::response::Response| { content.content = content.content.replace("Hello", "Hi"); println!( diff --git a/examples/stateful_module.rs b/examples/stateful_module.rs index 2ff698c..42c4ca1 100644 --- a/examples/stateful_module.rs +++ b/examples/stateful_module.rs @@ -12,7 +12,7 @@ impl windmark::module::Module for Clicker { println!("module 'clicker' has been attached!"); } - fn on_pre_route(&mut self, context: HookContext) { + fn on_pre_route(&mut self, context: &HookContext) { self.clicks += 1; println!( @@ -22,7 +22,7 @@ impl windmark::module::Module for Clicker { ); } - fn on_post_route(&mut self, context: HookContext) { + fn on_post_route(&mut self, context: &HookContext) { println!( "module 'clicker' clicker has been called after the route '{}' with {} \ clicks!", diff --git a/src/handler/hooks/post_route.rs b/src/handler/hooks/post_route.rs index 6bc24b2..1e1a6b6 100644 --- a/src/handler/hooks/post_route.rs +++ b/src/handler/hooks/post_route.rs @@ -2,13 +2,13 @@ use crate::{context::HookContext, response::Response}; #[allow(clippy::module_name_repetitions)] pub trait PostRouteHook: Send + Sync { - fn call(&mut self, context: HookContext, response: &mut Response); + fn call(&mut self, context: &HookContext, response: &mut Response); } impl<T> PostRouteHook for T -where T: FnMut(HookContext, &mut Response) + Send + Sync +where T: FnMut(&HookContext, &mut Response) + Send + Sync { - fn call(&mut self, context: HookContext, response: &mut Response) { + fn call(&mut self, context: &HookContext, response: &mut Response) { (*self)(context, response); } } diff --git a/src/handler/hooks/pre_route.rs b/src/handler/hooks/pre_route.rs index 9b1eb7f..1d11d66 100644 --- a/src/handler/hooks/pre_route.rs +++ b/src/handler/hooks/pre_route.rs @@ -2,11 +2,11 @@ use crate::context::HookContext; #[allow(clippy::module_name_repetitions)] pub trait PreRouteHook: Send + Sync { - fn call(&mut self, context: HookContext); + fn call(&mut self, context: &HookContext); } impl<T> PreRouteHook for T -where T: FnMut(HookContext) + Send + Sync +where T: FnMut(&HookContext) + Send + Sync { - fn call(&mut self, context: HookContext) { (*self)(context) } + fn call(&mut self, context: &HookContext) { (*self)(context) } } diff --git a/src/module/asynchronous.rs b/src/module/asynchronous.rs index 8822ea4..1cd5673 100644 --- a/src/module/asynchronous.rs +++ b/src/module/asynchronous.rs @@ -6,8 +6,8 @@ pub trait AsyncModule: Send + Sync { async fn on_attach(&mut self, _: &mut crate::router::Router) {} /// Called before a route is mounted. - async fn on_pre_route(&mut self, _: HookContext) {} + async fn on_pre_route(&mut self, _: &HookContext) {} /// Called after a route is mounted. - async fn on_post_route(&mut self, _: HookContext) {} + async fn on_post_route(&mut self, _: &HookContext) {} } diff --git a/src/module/sync.rs b/src/module/sync.rs index a38b541..d32d81f 100644 --- a/src/module/sync.rs +++ b/src/module/sync.rs @@ -5,8 +5,8 @@ pub trait Module { fn on_attach(&mut self, _: &mut crate::router::Router) {} /// Called before a route is mounted. - fn on_pre_route(&mut self, _: HookContext) {} + fn on_pre_route(&mut self, _: &HookContext) {} /// Called after a route is mounted. - fn on_post_route(&mut self, _: HookContext) {} + fn on_post_route(&mut self, _: &HookContext) {} } diff --git a/src/router.rs b/src/router.rs index 0e64a67..fe8fadd 100644 --- a/src/router.rs +++ b/src/router.rs @@ -197,22 +197,18 @@ impl RequestHandler { route.as_ref().ok().map(|route| route.params.clone()), peer_certificate.clone(), ); - let hook_context_clone = hook_context.clone(); - for module in &mut *self.async_modules.lock().await { - module.on_pre_route(hook_context_clone.clone()).await; + module.on_pre_route(&hook_context).await; } - let hook_context_clone = hook_context.clone(); - if let Ok(mut modules) = self.modules.lock() { for module in &mut *modules { - module.on_pre_route(hook_context_clone.clone()); + module.on_pre_route(&hook_context); } } if let Ok(mut callback) = self.pre_route_callback.lock() { - callback.call(hook_context.clone()); + callback.call(&hook_context); } let mut content = if let Ok(ref route) = route { @@ -266,22 +262,18 @@ impl RequestHandler { .await }; - let hook_context_clone = hook_context.clone(); - for module in &mut *self.async_modules.lock().await { - module.on_post_route(hook_context_clone.clone()).await; + module.on_post_route(&hook_context).await; } - let hook_context_clone = hook_context.clone(); - if let Ok(mut modules) = self.modules.lock() { for module in &mut *modules { - module.on_post_route(hook_context_clone.clone()); + module.on_post_route(&hook_context); } } if let Ok(mut callback) = self.post_route_callback.lock() { - callback.call(hook_context, &mut content); + callback.call(&hook_context, &mut content); } let status_code = @@ -1138,9 +1130,11 @@ impl Default for Router { default_logger: false, #[cfg(feature = "logger")] log_filter: String::new(), - pre_route_callback: Arc::new(Mutex::new(Box::new(|_| {}))), + pre_route_callback: Arc::new(Mutex::new(Box::new( + (|_| {}) as fn(&HookContext), + ))), post_route_callback: Arc::new(Mutex::new(Box::new( - |_, _: &'_ mut Response| {}, + (|_, _: &mut Response| {}) as fn(&HookContext, &mut Response), ))), character_set: "utf-8".to_string(), languages: vec!["en".to_string()], |