From 694e039c8f4ccd930b994a25ed243c1a25107d34 Mon Sep 17 00:00:00 2001 From: Jeb Rosen Date: Sun, 4 Aug 2019 15:36:55 -0700 Subject: [PATCH] Update 'fairings' example for async. --- examples/fairings/src/main.rs | 38 ++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/examples/fairings/src/main.rs b/examples/fairings/src/main.rs index 7b7ca500..da86f1eb 100644 --- a/examples/fairings/src/main.rs +++ b/examples/fairings/src/main.rs @@ -35,20 +35,24 @@ impl Fairing for Counter { } } - fn on_response(&self, request: &Request<'_>, response: &mut Response<'_>) { - if response.status() != Status::NotFound { - return - } + fn on_response<'a, 'r>(&'a self, request: &'a Request<'r>, response: &'a mut Response<'r>) + -> std::pin::Pin + Send + 'a>> + { + Box::pin(async move { + if response.status() != Status::NotFound { + return + } - if request.method() == Method::Get && request.uri().path() == "/counts" { - let get_count = self.get.load(Ordering::Relaxed); - let post_count = self.post.load(Ordering::Relaxed); + if request.method() == Method::Get && request.uri().path() == "/counts" { + let get_count = self.get.load(Ordering::Relaxed); + let post_count = self.post.load(Ordering::Relaxed); - let body = format!("Get: {}\nPost: {}", get_count, post_count); - response.set_status(Status::Ok); - response.set_header(ContentType::Plain); - response.set_sized_body(Cursor::new(body)); - } + let body = format!("Get: {}\nPost: {}", get_count, post_count); + response.set_status(Status::Ok); + response.set_header(ContentType::Plain); + response.set_sized_body(Cursor::new(body)); + } + }) } } @@ -82,10 +86,12 @@ fn rocket() -> rocket::Rocket { } })) .attach(AdHoc::on_response("Response Rewriter", |req, res| { - if req.uri().path() == "/" { - println!(" => Rewriting response body."); - res.set_sized_body(Cursor::new("Hello, fairings!")); - } + Box::pin(async move { + if req.uri().path() == "/" { + println!(" => Rewriting response body."); + res.set_sized_body(Cursor::new("Hello, fairings!")); + } + }) })) }