From d971186326a6300bad86d94ff1e973c4ca3603da Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 27 Mar 2022 11:26:47 +0000 Subject: feat(handler): fnmut partial --- src/handler.rs | 2 +- src/lib.rs | 24 ++++++++++-------------- 2 files changed, 11 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/handler.rs b/src/handler.rs index b1e6afe..7e297f8 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -27,4 +27,4 @@ pub type Callback = Box< + Send + Sync, >; -pub type Partial = fn(RouteContext<'_>) -> String; +pub type Partial = Box) -> String + Send + Sync>; diff --git a/src/lib.rs b/src/lib.rs index fe3489a..09baf20 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -118,8 +118,8 @@ pub struct Router { error_handler: Arc>, private_key_file_name: String, certificate_chain_file_name: String, - header: Partial, - footer: Partial, + header: Arc>, + footer: Arc>, ssl_acceptor: Arc, #[cfg(feature = "logger")] default_logger: bool, @@ -223,7 +223,7 @@ impl Router { /// }); /// ``` pub fn set_header(&mut self, handler: Partial) -> &mut Self { - self.header = handler; + self.header = Arc::new(Mutex::new(handler)); self } @@ -238,7 +238,7 @@ impl Router { /// }); /// ``` pub fn set_footer(&mut self, handler: Partial) -> &mut Self { - self.footer = handler; + self.footer = Arc::new(Mutex::new(handler)); self } @@ -334,10 +334,8 @@ impl Router { if let Ok(ref route) = route { header = { - let header = (self.header)(RouteContext::new( - stream.get_ref(), - &url, - &route.params, + let header = (*self.header).lock().unwrap().call_mut(( + RouteContext::new(stream.get_ref(), &url, &route.params), )); if header.is_empty() { @@ -347,10 +345,8 @@ impl Router { } }; footer = { - let footer = (self.footer)(RouteContext::new( - stream.get_ref(), - &url, - &route.params, + let footer = (*self.footer).lock().unwrap().call_mut(( + RouteContext::new(stream.get_ref(), &url, &route.params), )); if footer.is_empty() { @@ -622,8 +618,8 @@ impl Default for Router { }))), private_key_file_name: "".to_string(), certificate_chain_file_name: "".to_string(), - header: |_| "".to_string(), - footer: |_| "".to_string(), + header: Arc::new(Mutex::new(Box::new(|_| "".to_string()))), + footer: Arc::new(Mutex::new(Box::new(|_| "".to_string()))), ssl_acceptor: Arc::new( SslAcceptor::mozilla_intermediate(SslMethod::tls()) .unwrap() -- cgit v1.2.3