Skip to content

Commit d9a1514

Browse files
committed
Remove state from next/middleware layer
1 parent dd98a40 commit d9a1514

File tree

10 files changed

+39
-52
lines changed

10 files changed

+39
-52
lines changed

examples/middleware.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl UserDatabase {
2727
// it would likely be closely tied to a specific application
2828
fn user_loader<'a>(
2929
mut request: Request,
30-
next: Next<'a, UserDatabase>,
30+
next: Next<'a>,
3131
) -> Pin<Box<dyn Future<Output = Result> + Send + 'a>> {
3232
Box::pin(async {
3333
if let Some(user) = request.state().find_user().await {
@@ -61,8 +61,8 @@ impl RequestCounterMiddleware {
6161
struct RequestCount(usize);
6262

6363
#[tide::utils::async_trait]
64-
impl<State: Clone + Send + Sync + 'static> Middleware<State> for RequestCounterMiddleware {
65-
async fn handle(&self, mut req: Request, next: Next<'_, State>) -> Result {
64+
impl Middleware for RequestCounterMiddleware {
65+
async fn handle(&self, mut req: Request, next: Next<'_>) -> Result {
6666
let count = self.requests_counted.fetch_add(1, Ordering::Relaxed);
6767
tide::log::trace!("request counter", { count: count });
6868
req.set_ext(RequestCount(count));

src/endpoint.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ where
6666
}
6767
}
6868

69-
pub(crate) struct MiddlewareEndpoint<E, State> {
69+
pub(crate) struct MiddlewareEndpoint<E> {
7070
endpoint: E,
71-
middleware: Vec<Arc<dyn Middleware<State>>>,
71+
middleware: Vec<Arc<dyn Middleware>>,
7272
}
7373

74-
impl<E: Clone, State> Clone for MiddlewareEndpoint<E, State> {
74+
impl<E: Clone> Clone for MiddlewareEndpoint<E> {
7575
fn clone(&self) -> Self {
7676
Self {
7777
endpoint: self.endpoint.clone(),
@@ -80,7 +80,7 @@ impl<E: Clone, State> Clone for MiddlewareEndpoint<E, State> {
8080
}
8181
}
8282

83-
impl<E, State> std::fmt::Debug for MiddlewareEndpoint<E, State> {
83+
impl<E> std::fmt::Debug for MiddlewareEndpoint<E> {
8484
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
8585
write!(
8686
fmt,
@@ -90,14 +90,13 @@ impl<E, State> std::fmt::Debug for MiddlewareEndpoint<E, State> {
9090
}
9191
}
9292

93-
impl<E, State> MiddlewareEndpoint<E, State>
93+
impl<E> MiddlewareEndpoint<E>
9494
where
95-
State: Clone + Send + Sync + 'static,
9695
E: Endpoint,
9796
{
9897
pub(crate) fn wrap_with_middleware(
9998
ep: E,
100-
middleware: &[Arc<dyn Middleware<State>>],
99+
middleware: &[Arc<dyn Middleware>],
101100
) -> Box<dyn Endpoint + Send + Sync + 'static> {
102101
if middleware.is_empty() {
103102
Box::new(ep)
@@ -111,9 +110,8 @@ where
111110
}
112111

113112
#[async_trait]
114-
impl<E, State> Endpoint for MiddlewareEndpoint<E, State>
113+
impl<E> Endpoint for MiddlewareEndpoint<E>
115114
where
116-
State: Clone + Send + Sync + 'static,
117115
E: Endpoint,
118116
{
119117
async fn call(&self, req: Request) -> crate::Result {

src/log/middleware.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ impl LogMiddleware {
2626
}
2727

2828
/// Log a request and a response.
29-
async fn log<'a, State: Clone + Send + Sync + 'static>(
30-
&'a self,
31-
mut req: Request,
32-
next: Next<'a, State>,
33-
) -> crate::Result {
29+
async fn log<'a>(&'a self, mut req: Request, next: Next<'a>) -> crate::Result {
3430
if req.ext::<LogMiddlewareHasBeenRun>().is_some() {
3531
return Ok(next.run(req).await);
3632
}
@@ -94,8 +90,8 @@ impl LogMiddleware {
9490
}
9591

9692
#[async_trait::async_trait]
97-
impl<State: Clone + Send + Sync + 'static> Middleware<State> for LogMiddleware {
98-
async fn handle(&self, req: Request, next: Next<'_, State>) -> crate::Result {
93+
impl Middleware for LogMiddleware {
94+
async fn handle(&self, req: Request, next: Next<'_>) -> crate::Result {
9995
self.log(req, next).await
10096
}
10197
}

src/middleware.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use std::pin::Pin;
1010

1111
/// Middleware that wraps around the remaining middleware chain.
1212
#[async_trait]
13-
pub trait Middleware<State>: Send + Sync + 'static {
13+
pub trait Middleware: Send + Sync + 'static {
1414
/// Asynchronously handle the request, and return a response.
15-
async fn handle(&self, request: Request, next: Next<'_, State>) -> crate::Result;
15+
async fn handle(&self, request: Request, next: Next<'_>) -> crate::Result;
1616

1717
/// Set the middleware's name. By default it uses the type signature.
1818
fn name(&self) -> &str {
@@ -21,30 +21,26 @@ pub trait Middleware<State>: Send + Sync + 'static {
2121
}
2222

2323
#[async_trait]
24-
impl<State, F> Middleware<State> for F
24+
impl<F> Middleware for F
2525
where
26-
State: Clone + Send + Sync + 'static,
2726
F: Send
2827
+ Sync
2928
+ 'static
30-
+ for<'a> Fn(
31-
Request,
32-
Next<'a, State>,
33-
) -> Pin<Box<dyn Future<Output = crate::Result> + 'a + Send>>,
29+
+ for<'a> Fn(Request, Next<'a>) -> Pin<Box<dyn Future<Output = crate::Result> + 'a + Send>>,
3430
{
35-
async fn handle(&self, req: Request, next: Next<'_, State>) -> crate::Result {
31+
async fn handle(&self, req: Request, next: Next<'_>) -> crate::Result {
3632
(self)(req, next).await
3733
}
3834
}
3935

4036
/// The remainder of a middleware chain, including the endpoint.
4137
#[allow(missing_debug_implementations)]
42-
pub struct Next<'a, State> {
38+
pub struct Next<'a> {
4339
pub(crate) endpoint: &'a DynEndpoint,
44-
pub(crate) next_middleware: &'a [Arc<dyn Middleware<State>>],
40+
pub(crate) next_middleware: &'a [Arc<dyn Middleware>],
4541
}
4642

47-
impl<State: Clone + Send + Sync + 'static> Next<'_, State> {
43+
impl Next<'_> {
4844
/// Asynchronously execute the remaining middleware chain.
4945
pub async fn run(mut self, req: Request) -> Response {
5046
if let Some((current, next)) = self.next_middleware.split_first() {

src/route.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ use crate::{router::Router, Endpoint, Middleware};
1717
///
1818
/// [`Server::at`]: ./struct.Server.html#method.at
1919
#[allow(missing_debug_implementations)]
20-
pub struct Route<'a, State> {
20+
pub struct Route<'a> {
2121
router: &'a mut Router,
2222
path: String,
23-
middleware: Vec<Arc<dyn Middleware<State>>>,
23+
middleware: Vec<Arc<dyn Middleware>>,
2424
/// Indicates whether the path of current route is treated as a prefix. Set by
2525
/// [`strip_prefix`].
2626
///
2727
/// [`strip_prefix`]: #method.strip_prefix
2828
prefix: bool,
2929
}
3030

31-
impl<'a, State: Clone + Send + Sync + 'static> Route<'a, State> {
32-
pub(crate) fn new(router: &'a mut Router, path: String) -> Route<'a, State> {
31+
impl<'a> Route<'a> {
32+
pub(crate) fn new(router: &'a mut Router, path: String) -> Route<'a> {
3333
Route {
3434
router,
3535
path,
@@ -39,7 +39,7 @@ impl<'a, State: Clone + Send + Sync + 'static> Route<'a, State> {
3939
}
4040

4141
/// Extend the route with the given `path`.
42-
pub fn at<'b>(&'b mut self, path: &str) -> Route<'b, State> {
42+
pub fn at<'b>(&'b mut self, path: &str) -> Route<'b> {
4343
let mut p = self.path.clone();
4444

4545
if !p.ends_with('/') && !path.starts_with('/') {
@@ -79,7 +79,7 @@ impl<'a, State: Clone + Send + Sync + 'static> Route<'a, State> {
7979
/// Apply the given middleware to the current route.
8080
pub fn with<M>(&mut self, middleware: M) -> &mut Self
8181
where
82-
M: Middleware<State>,
82+
M: Middleware,
8383
{
8484
log::trace!(
8585
"Adding middleware {} to route {:?}",
@@ -126,7 +126,6 @@ impl<'a, State: Clone + Send + Sync + 'static> Route<'a, State> {
126126
/// [`Server`]: struct.Server.html
127127
pub fn nest<InnerState>(&mut self, service: crate::Server<InnerState>) -> &mut Self
128128
where
129-
State: Clone + Send + Sync + 'static,
130129
InnerState: Clone + Send + Sync + 'static,
131130
{
132131
let prefix = self.prefix;

src/security/cors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ impl CorsMiddleware {
136136
}
137137

138138
#[async_trait::async_trait]
139-
impl<State: Clone + Send + Sync + 'static> Middleware<State> for CorsMiddleware {
140-
async fn handle(&self, req: Request, next: Next<'_, State>) -> Result {
139+
impl Middleware for CorsMiddleware {
140+
async fn handle(&self, req: Request, next: Next<'_>) -> Result {
141141
// TODO: how should multiple origin values be handled?
142142
let origins = req.header(&headers::ORIGIN).cloned();
143143

src/server.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct Server<State> {
3737
/// The inner Arc-s allow MiddlewareEndpoint-s to be cloned internally.
3838
/// We don't use a Mutex around the Vec here because adding a middleware during execution should be an error.
3939
#[allow(clippy::rc_buffer)]
40-
middleware: Arc<Vec<Arc<dyn Middleware<State>>>>,
40+
middleware: Arc<Vec<Arc<dyn Middleware>>>,
4141
}
4242

4343
impl Server<()> {
@@ -166,7 +166,7 @@ where
166166
/// There is no fallback route matching, i.e. either a resource is a full
167167
/// match or not, which means that the order of adding resources has no
168168
/// effect.
169-
pub fn at<'a>(&'a mut self, path: &str) -> Route<'a, State> {
169+
pub fn at<'a>(&'a mut self, path: &str) -> Route<'a> {
170170
let router = Arc::get_mut(&mut self.router)
171171
.expect("Registering routes is not possible after the Server has started");
172172
Route::new(router, path.to_owned())
@@ -183,7 +183,7 @@ where
183183
/// order in which it is applied.
184184
pub fn with<M>(&mut self, middleware: M) -> &mut Self
185185
where
186-
M: Middleware<State>,
186+
M: Middleware,
187187
{
188188
log::trace!("Adding middleware {}", middleware.name());
189189
let m = Arc::get_mut(&mut self.middleware)

src/utils.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ use std::future::Future;
2525
pub struct Before<F>(pub F);
2626

2727
#[async_trait]
28-
impl<State, F, Fut> Middleware<State> for Before<F>
28+
impl<F, Fut> Middleware for Before<F>
2929
where
30-
State: Clone + Send + Sync + 'static,
3130
F: Fn(Request) -> Fut + Send + Sync + 'static,
3231
Fut: Future<Output = Request> + Send + Sync + 'static,
3332
{
34-
async fn handle(&self, request: Request, next: Next<'_, State>) -> crate::Result {
33+
async fn handle(&self, request: Request, next: Next<'_>) -> crate::Result {
3534
let request = (self.0)(request).await;
3635
Ok(next.run(request).await)
3736
}
@@ -59,13 +58,12 @@ where
5958
#[derive(Debug)]
6059
pub struct After<F>(pub F);
6160
#[async_trait]
62-
impl<State, F, Fut> Middleware<State> for After<F>
61+
impl<F, Fut> Middleware for After<F>
6362
where
64-
State: Clone + Send + Sync + 'static,
6563
F: Fn(Response) -> Fut + Send + Sync + 'static,
6664
Fut: Future<Output = crate::Result> + Send + Sync + 'static,
6765
{
68-
async fn handle(&self, request: Request, next: Next<'_, State>) -> crate::Result {
66+
async fn handle(&self, request: Request, next: Next<'_>) -> crate::Result {
6967
let response = next.run(request).await;
7068
(self.0)(response).await
7169
}

tests/function_middleware.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod test_utils;
66

77
fn auth_middleware<'a>(
88
request: tide::Request,
9-
next: tide::Next<'a, ()>,
9+
next: tide::Next<'a>,
1010
) -> Pin<Box<dyn Future<Output = tide::Result> + 'a + Send>> {
1111
let authenticated = match request.header("X-Auth") {
1212
Some(header) => header == "secret_key",

tests/route_middleware.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ impl TestMiddleware {
1414
}
1515

1616
#[async_trait::async_trait]
17-
impl<State: Clone + Send + Sync + 'static> Middleware<State> for TestMiddleware {
17+
impl Middleware for TestMiddleware {
1818
async fn handle(
1919
&self,
2020
req: tide::Request,
21-
next: tide::Next<'_, State>,
21+
next: tide::Next<'_>,
2222
) -> tide::Result<tide::Response> {
2323
let mut res = next.run(req).await;
2424
res.insert_header(self.0.clone(), self.1);

0 commit comments

Comments
 (0)