Skip to content

Commit a1e8a88

Browse files
committed
Simplify ErasedHandler
1 parent 1f7934e commit a1e8a88

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

crates/miniserve/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#![warn(clippy::pedantic)]
22

3-
use std::{collections::HashMap, future::Future, io, sync::Arc};
3+
use std::{collections::HashMap, future::Future, io, pin::Pin, sync::Arc};
44
use tokio::net::{TcpListener, TcpStream};
5-
use tokio_util::sync::ReusableBoxFuture;
65

76
/// Re-export for library clients.
87
pub use http;
@@ -40,9 +39,8 @@ where
4039
type Future = F;
4140
}
4241

43-
struct ErasedHandler(
44-
Box<dyn Fn(Request) -> ReusableBoxFuture<'static, Response> + Send + Sync + 'static>,
45-
);
42+
type ErasedHandler =
43+
Box<dyn Fn(Request) -> Pin<Box<dyn Future<Output = Response> + Send + Sync>> + Send + Sync>;
4644

4745
/// The main server data structure.
4846
#[derive(Default)]
@@ -64,10 +62,10 @@ impl Server {
6462
#[must_use]
6563
pub fn route<H: Handler>(mut self, route: impl Into<String>, handler: H) -> Self {
6664
let handler = Arc::new(handler);
67-
let erased = ErasedHandler(Box::new(move |req| {
65+
let erased = Box::new(move |req| {
6866
let handler_ref = Arc::clone(&handler);
69-
ReusableBoxFuture::new(async move { handler_ref(req).await })
70-
}));
67+
Box::pin(handler_ref(req)) as Pin<Box<dyn Future<Output = Response> + Send + Sync>>
68+
});
7169
self.routes.insert(route.into(), erased);
7270
self
7371
}

crates/miniserve/src/protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ async fn generate_response<'a>(
106106
return make_response(StatusCode::NOT_FOUND, "No valid route");
107107
};
108108

109-
let response_res = handler.0(request).await;
109+
let response_res = handler(request).await;
110110

111111
match response_res {
112112
Ok(content) => {

0 commit comments

Comments
 (0)