aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-04-03 02:44:25 +0000
committerFuwn <[email protected]>2023-04-03 02:44:25 +0000
commited52d304178d5d37a0af710f06e8fe8eb65b2c24 (patch)
treea1ce35c6a54d576361a6378c3cf59ea60ebc15e1 /src
parentfeat(response): allow multiple languages (diff)
downloadwindmark-ed52d304178d5d37a0af710f06e8fe8eb65b2c24.tar.xz
windmark-ed52d304178d5d37a0af710f06e8fe8eb65b2c24.zip
feat(router): GET RID OF DIRTY BOXES
Diffstat (limited to 'src')
-rw-r--r--src/handler.rs10
-rw-r--r--src/response/macros.rs20
-rw-r--r--src/router.rs20
3 files changed, 25 insertions, 25 deletions
diff --git a/src/handler.rs b/src/handler.rs
index 3429f3b..f22e3b3 100644
--- a/src/handler.rs
+++ b/src/handler.rs
@@ -22,8 +22,14 @@ use crate::{
returnable::{CallbackContext, RouteContext},
};
-pub type RouteResponse =
- Box<dyn FnMut(RouteContext<'_>) -> Response + Send + Sync>;
+pub trait RouteResponse:
+ FnMut(RouteContext<'_>) -> Response + Send + Sync
+{
+}
+
+impl<T> RouteResponse for T where T: FnMut(RouteContext<'_>) -> Response + Send + Sync
+{}
+
pub type ErrorResponse =
Box<dyn FnMut(returnable::ErrorContext<'_>) -> Response + Send + Sync>;
pub type Callback = Box<dyn FnMut(CallbackContext<'_>) + Send + Sync>;
diff --git a/src/response/macros.rs b/src/response/macros.rs
index f49da2d..54e6a8b 100644
--- a/src/response/macros.rs
+++ b/src/response/macros.rs
@@ -23,10 +23,10 @@ macro_rules! response {
#[macro_export]
macro_rules! $name {
($body:expr /* $(,)? */) => {
- ::std::boxed::Box::new(|_| windmark::Response::$name($body))
+ |_: ::windmark::returnable::RouteContext<'_>| ::windmark::Response::$name($body)
};
($context:ident, $body:expr /* $(,)? */) => {
- ::std::boxed::Box::new(|$context| windmark::Response::$name($body))
+ |$context: ::windmark::returnable::RouteContext<'_>| ::windmark::Response::$name($body)
};
}
)*
@@ -57,9 +57,9 @@ response!(certificate_not_valid);
#[macro_export]
macro_rules! binary_success {
($body:expr, $mime:expr) => {
- ::std::boxed::Box::new(|_| {
+ |_: ::windmark::returnable::RouteContext<'_>| {
::windmark::Response::binary_success($body, $mime)
- })
+ }
};
($body:expr) => {{
#[cfg(not(feature = "auto-deduce-mime"))]
@@ -68,19 +68,19 @@ macro_rules! binary_success {
feature to be enabled"
);
- ::std::boxed::Box::new(|_| {
+ |_: ::windmark::returnable::RouteContext<'_>| {
#[cfg(feature = "auto-deduce-mime")]
return ::windmark::Response::binary_success_auto($body);
// Suppress item not found warning
#[cfg(not(feature = "auto-deduce-mime"))]
::windmark::Response::binary_success($body, "application/octet-stream")
- })
+ }
}};
($context:ident, $body:expr, $mime:expr) => {
- ::std::boxed::Box::new(|$context| {
+ |$context: ::windmark::returnable::RouteContext<'_>| {
::windmark::Response::binary_success($body, $mime)
- })
+ }
};
($context:ident, $body:expr) => {{
#[cfg(not(feature = "auto-deduce-mime"))]
@@ -89,13 +89,13 @@ macro_rules! binary_success {
feature to be enabled"
);
- ::std::boxed::Box::new(|$context| {
+ |$context: ::windmark::returnable::RouteContext<'_>| {
#[cfg(feature = "auto-deduce-mime")]
return ::windmark::Response::binary_success_auto($body);
// Suppress item not found warning
#[cfg(not(feature = "auto-deduce-mime"))]
::windmark::Response::binary_success($body, "application/octet-stream")
- })
+ }
}};
}
diff --git a/src/router.rs b/src/router.rs
index b245722..9917923 100644
--- a/src/router.rs
+++ b/src/router.rs
@@ -16,6 +16,8 @@
// Copyright (C) 2022-2022 Fuwn <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only
+#![allow(clippy::significant_drop_tightening)]
+
use std::{
error::Error,
sync::{Arc, Mutex},
@@ -54,7 +56,7 @@ macro_rules! or_error {
/// response generation, panics, logging, and more.
#[derive(Clone)]
pub struct Router {
- routes: matchit::Router<Arc<Mutex<RouteResponse>>>,
+ routes: matchit::Router<Arc<Mutex<Box<dyn RouteResponse>>>>,
error_handler: Arc<Mutex<ErrorResponse>>,
private_key_file_name: String,
ca_file_name: String,
@@ -123,17 +125,9 @@ impl Router {
/// # Examples
///
/// ```rust
- /// use windmark::Response;
- ///
/// windmark::Router::new()
- /// .mount(
- /// "/",
- /// Box::new(|_| Response::success("This is the index page!")),
- /// )
- /// .mount(
- /// "/test",
- /// Box::new(|_| Response::success("This is a test page!")),
- /// );
+ /// .mount("/", |_| windmark::success!("This is the index page!"))
+ /// .mount("/test", |_| windmark::success!("This is a test page!"));
/// ```
///
/// # Panics
@@ -142,11 +136,11 @@ impl Router {
pub fn mount(
&mut self,
route: impl Into<String> + AsRef<str>,
- handler: RouteResponse,
+ handler: impl RouteResponse + 'static,
) -> &mut Self {
self
.routes
- .insert(route.into(), Arc::new(Mutex::new(handler)))
+ .insert(route.into(), Arc::new(Mutex::new(Box::new(handler))))
.unwrap();
self