Skip to content

Commit eeb1992

Browse files
committed
fix(lifetimes): correct lifetime pattern to separate out server state from mw state
Correct lifetimes in response to errors found by rust-lang/rust#27641; the lifetime parameters on request ('a, 'b, 'k) seem to play the following roles: 'a, 'b -- these represent the processing of this individual request. They can vary. 'k -- this represents the lifetime of the server's internal, mutable storage. It is fixed. If you only have two parameters 'x and 'y to supply, then, the correct pattern is `'x, 'x, 'y`, because then `'x` plays the role of the intersection of `'a` and `'b`, but `'y` is pinned to the server's internal storage.
1 parent 3fd622f commit eeb1992

File tree

6 files changed

+8
-8
lines changed

6 files changed

+8
-8
lines changed

src/favicon_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct FaviconHandler {
1919
}
2020

2121
impl Middleware for FaviconHandler {
22-
fn invoke<'a, 'b>(&'a self, req: &mut Request<'b, 'a, 'b>, res: Response<'a, net::Fresh>)
22+
fn invoke<'a, 'b>(&'a self, req: &mut Request<'a, 'a, 'b>, res: Response<'a, net::Fresh>)
2323
-> MiddlewareResult<'a> {
2424
if FaviconHandler::is_favicon_request(req) {
2525
self.handle_request(req, res)

src/macros/middleware.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ macro_rules! _middleware_inner {
4848
#[inline(always)]
4949
fn restrict_closure<F>(f: F) -> F
5050
where F: for<'r, 'b, 'a>
51-
Fn(&'r mut Request<'b, 'a, 'b>, Response<'a>)
51+
Fn(&'r mut Request<'a, 'a, 'b>, Response<'a>)
5252
-> MiddlewareResult<'a> + Send + Sync { f }
5353

5454
restrict_closure(move |as_pat!($req), $res_binding| {

src/middleware.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ pub enum Action<T=(), U=()> {
1717
// the usage of + Send is weird here because what we really want is + Static
1818
// but that's not possible as of today. We have to use + Send for now.
1919
pub trait Middleware: Send + 'static + Sync {
20-
fn invoke<'a, 'b>(&'a self, _req: &mut Request<'b, 'a, 'b>, res: Response<'a, net::Fresh>) -> MiddlewareResult<'a> {
20+
fn invoke<'a, 'b>(&'a self, _req: &mut Request<'a, 'a, 'b>, res: Response<'a, net::Fresh>) -> MiddlewareResult<'a> {
2121
Ok(Continue(res))
2222
}
2323
}
2424

25-
impl<T> Middleware for T where T: for<'r, 'b, 'a> Fn(&'r mut Request<'b, 'a, 'b>, Response<'a>) -> MiddlewareResult<'a> + Send + Sync + 'static {
26-
fn invoke<'a, 'b>(&'a self, req: &mut Request<'b, 'a, 'b>, res: Response<'a>) -> MiddlewareResult<'a> {
25+
impl<T> Middleware for T where T: for<'r, 'b, 'a> Fn(&'r mut Request<'a, 'a, 'b>, Response<'a>) -> MiddlewareResult<'a> + Send + Sync + 'static {
26+
fn invoke<'a, 'b>(&'a self, req: &mut Request<'a, 'a, 'b>, res: Response<'a>) -> MiddlewareResult<'a> {
2727
(*self)(req, res)
2828
}
2929
}

src/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use hyper::server::Request as HyperRequest;
55
use hyper::uri::RequestUri::AbsolutePath;
66

77
///A container for all the request data
8-
pub struct Request<'a, 'b: 'k, 'k: 'a> {
8+
pub struct Request<'a, 'b, 'k: 'a> {
99
///the original `hyper::server::Request`
1010
pub origin: HyperRequest<'a, 'k>,
1111
///a `HashMap<String, String>` holding all params with names and values

src/router/router.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl HttpRouter for Router {
9595
}
9696

9797
impl Middleware for Router {
98-
fn invoke<'a, 'b>(&'a self, req: &mut Request<'b, 'a, 'b>, mut res: Response<'a>)
98+
fn invoke<'a, 'b>(&'a self, req: &mut Request<'a, 'a, 'b>, mut res: Response<'a>)
9999
-> MiddlewareResult<'a> {
100100
debug!("Router::invoke for '{:?}'", req.origin.uri);
101101

src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ struct ArcServer(Arc<Server>);
1919

2020
impl Handler for ArcServer {
2121
fn handle<'a, 'k>(&'a self, req: Request<'a, 'k>, res: Response<'a>) {
22+
let req: Request<'a, 'k> = req;
2223
let nickel_req = request::Request::from_internal(req);
2324
let nickel_res = response::Response::from_internal(res, &self.0.templates);
24-
2525
self.0.middleware_stack.invoke(nickel_req, nickel_res);
2626
}
2727
}

0 commit comments

Comments
 (0)