From 3f95205696b5d2e79d10626b8b593ac48c0628e0 Mon Sep 17 00:00:00 2001 From: Cameron Dershem Date: Sun, 16 Jun 2019 23:37:14 -0400 Subject: [PATCH] Front page examples return 'impl Responder' --- examples/easy-form-handling/src/main.rs | 9 +++------ examples/main-example/src/main.rs | 6 +++--- examples/powerful-extractors/src/main.rs | 6 +++--- examples/request-routing/src/main.rs | 10 +++++----- layouts/index.html | 23 ++++++++++------------- 5 files changed, 24 insertions(+), 30 deletions(-) diff --git a/examples/easy-form-handling/src/main.rs b/examples/easy-form-handling/src/main.rs index 168b394..da0a115 100644 --- a/examples/easy-form-handling/src/main.rs +++ b/examples/easy-form-handling/src/main.rs @@ -1,5 +1,5 @@ // -use actix_web::{web, App, HttpResponse, HttpServer}; +use actix_web::{web, App, HttpResponse, HttpServer, Responder}; use serde::Deserialize; #[derive(Deserialize)] @@ -14,11 +14,8 @@ fn index() -> HttpResponse { .body(include_str!("../static/form.html")) } -fn register(params: web::Form) -> actix_web::Result { - Ok(HttpResponse::Ok().body(format!( - "Hello {} from {}!", - params.username, params.country - ))) +fn register(params: web::Form) -> impl Responder { + format!("Hello {} from {}!", params.username, params.country) } fn main() { diff --git a/examples/main-example/src/main.rs b/examples/main-example/src/main.rs index e09d82d..bc8649d 100644 --- a/examples/main-example/src/main.rs +++ b/examples/main-example/src/main.rs @@ -1,9 +1,9 @@ // -use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer}; +use actix_web::{web, App, HttpRequest, HttpServer, Responder}; -fn greet(req: HttpRequest) -> HttpResponse { +fn greet(req: HttpRequest) -> impl Responder { let name = req.match_info().get("name").unwrap_or("World"); - HttpResponse::Ok().body(format!("Hello {}!", &name)) + format!("Hello {}!", &name) } fn main() { diff --git a/examples/powerful-extractors/src/main.rs b/examples/powerful-extractors/src/main.rs index befb7a8..e6b11f8 100644 --- a/examples/powerful-extractors/src/main.rs +++ b/examples/powerful-extractors/src/main.rs @@ -1,4 +1,4 @@ -use actix_web::{web, App, HttpResponse, HttpServer}; +use actix_web::{web, App, HttpResponse, HttpServer, Responder}; use serde::{Deserialize, Serialize}; #[derive(Deserialize, Serialize)] @@ -20,9 +20,9 @@ fn store_event_in_db(timestamp: f64, kind: String, tags: Vec) -> Event { } } -fn capture_event(evt: web::Json) -> actix_web::Result { +fn capture_event(evt: web::Json) -> impl Responder { let new_event = store_event_in_db(evt.timestamp, evt.kind.clone(), evt.tags.clone()); - Ok(HttpResponse::Ok().json(new_event)) + format!("got event {}", new_event.id.unwrap()) } fn index() -> HttpResponse { diff --git a/examples/request-routing/src/main.rs b/examples/request-routing/src/main.rs index fdb7f45..8a20019 100644 --- a/examples/request-routing/src/main.rs +++ b/examples/request-routing/src/main.rs @@ -1,12 +1,12 @@ // -use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer}; +use actix_web::{web, App, HttpRequest, HttpServer, Responder}; -fn index(_req: HttpRequest) -> HttpResponse { - HttpResponse::Ok().body("Hello from the index page.") +fn index(_req: HttpRequest) -> impl Responder { + "Hello from the index page." } -fn hello(path: web::Path) -> HttpResponse { - HttpResponse::Ok().body(format!("Hello {}!", &path)) +fn hello(path: web::Path) -> impl Responder { + format!("Hello {}!", &path) } fn main() { diff --git a/layouts/index.html b/layouts/index.html index 8ae3988..e262e4d 100644 --- a/layouts/index.html +++ b/layouts/index.html @@ -41,9 +41,9 @@
{{ highlight `use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer}; -fn greet(req: HttpRequest) -> HttpResponse { +fn greet(req: HttpRequest) -> impl Responder { let name = req.match_info().get("name").unwrap_or("World"); - HttpResponse::Ok().body(format!("Hello {}!", &name)) + format!("Hello {}!", &name) } fn main() { @@ -98,11 +98,11 @@ struct Event { kind: String, tags: Vec, } -fn capture_event(evt: web::Json) -> actix_web::Result { +fn capture_event(evt: web::Json) -> impl Responder { let new_event = store_event_in_db(evt.timestamp, evt.kind.clone(), evt.tags.clone()); - Ok(HttpResponse::Ok().json(new_event)) + format!("got event {}", new_event.id.unwrap()) }` "rust" "" }}
@@ -118,11 +118,8 @@ struct Register { country: String, } -fn register(params: web::Form) -> actix_web::Result { - Ok(HttpResponse::Ok().body(format!( - "Hello {} from {}!", - params.username, params.country - ))) +fn register(params: web::Form) -> impl Responder { + format!("Hello {} from {}!", params.username, params.country) }` "rust" "" }}
@@ -132,12 +129,12 @@ fn register(params: web::Form) -> actix_web::Result { URLs and invoke individual handlers. For extra flexibility, scopes can be used.

- {{ highlight `fn index(_req: HttpRequest) -> HttpResponse { - HttpResponse::Ok().body("Hello from the index page!") + {{ highlight `fn index(_req: HttpRequest) -> impl Responder { + "Hello from the index page!" } -fn hello(path: web::Path) -> HttpResponse { - HttpResponse::Ok().body(format!("Hello {}!", &path)) +fn hello(path: web::Path) -> impl Responder { + format!("Hello {}!", &path) } fn main() {