Skip to content

Commit

Permalink
Merge pull request #57 from tirr-c/middleware-lifetime
Browse files Browse the repository at this point in the history
Relieve middleware FutureObj lifetime bounds
  • Loading branch information
tirr-c authored Nov 24, 2018
2 parents eba71f0 + c046ce6 commit 17f4940
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
7 changes: 3 additions & 4 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,15 @@ impl<Data: Clone + Send + Sync + 'static> Service for Server<Data> {
async move {
if let Some((endpoint, params)) = router.route(&path, &method) {
for m in middleware.iter() {
match await!(m.request(&mut data, req, &params)) {
Ok(new_req) => req = new_req,
Err(resp) => return Ok(resp.map(Into::into)),
if let Err(resp) = await!(m.request(&mut data, &mut req, &params)) {
return Ok(resp.map(Into::into));
}
}

let (head, mut resp) = await!(endpoint.call(data.clone(), req, params));

for m in middleware.iter() {
resp = await!(m.response(&mut data, &head, resp))
await!(m.response(&mut data, &head, &mut resp));
}

Ok(resp.map(Into::into))
Expand Down
25 changes: 14 additions & 11 deletions src/middleware/default_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ impl DefaultHeaders {
}

impl<Data> Middleware<Data> for DefaultHeaders {
fn response(
&self,
data: &mut Data,
head: &Head,
mut resp: Response,
) -> FutureObj<'static, Response> {
let headers = resp.headers_mut();
for (key, value) in self.headers.iter() {
headers.entry(key).unwrap().or_insert_with(|| value.clone());
}
FutureObj::new(Box::new(async { resp }))
fn response<'a>(
&'a self,
data: &'a mut Data,
head: &'a Head,
resp: &'a mut Response,
) -> FutureObj<'a, ()> {
FutureObj::new(Box::new(
async move {
let headers = resp.headers_mut();
for (key, value) in self.headers.iter() {
headers.entry(key).unwrap().or_insert_with(|| value.clone());
}
},
))
}
}
28 changes: 14 additions & 14 deletions src/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ pub use self::default_headers::DefaultHeaders;
pub trait Middleware<Data>: Send + Sync {
/// Asynchronously transform the incoming request, or abort further handling by immediately
/// returning a response.
fn request(
&self,
data: &mut Data,
req: Request,
params: &RouteMatch<'_>,
) -> FutureObj<'static, Result<Request, Response>> {
FutureObj::new(Box::new(async { Ok(req) }))
fn request<'a>(
&'a self,
data: &'a mut Data,
req: &'a mut Request,
params: &'a RouteMatch<'_>,
) -> FutureObj<'a, Result<(), Response>> {
FutureObj::new(Box::new(async { Ok(()) }))
}

/// Asynchronously transform the outgoing response.
fn response(
&self,
data: &mut Data,
head: &Head,
resp: Response,
) -> FutureObj<'static, Response> {
FutureObj::new(Box::new(async { resp }))
fn response<'a>(
&'a self,
data: &'a mut Data,
head: &'a Head,
resp: &'a mut Response,
) -> FutureObj<'a, ()> {
FutureObj::new(Box::new(async {}))
}

// TODO: provide the following, intended to fire *after* the body has been fully sent
Expand Down

0 comments on commit 17f4940

Please sign in to comment.