From ed52d304178d5d37a0af710f06e8fe8eb65b2c24 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Mon, 3 Apr 2023 02:44:25 +0000 Subject: feat(router): GET RID OF DIRTY BOXES --- src/handler.rs | 10 ++++++++-- src/response/macros.rs | 20 ++++++++++---------- src/router.rs | 20 +++++++------------- 3 files changed, 25 insertions(+), 25 deletions(-) (limited to 'src') 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) -> Response + Send + Sync>; +pub trait RouteResponse: + FnMut(RouteContext<'_>) -> Response + Send + Sync +{ +} + +impl RouteResponse for T where T: FnMut(RouteContext<'_>) -> Response + Send + Sync +{} + pub type ErrorResponse = Box) -> Response + Send + Sync>; pub type Callback = Box) + 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 // 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>>, + routes: matchit::Router>>>, error_handler: Arc>, 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 + AsRef, - 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 -- cgit v1.2.3