From cc2a7a0645b2229699518dba2e3cdc4ce5fde733 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Sun, 29 Dec 2019 02:08:25 +0900 Subject: [PATCH] Update request-handlers --- examples/request-handlers/Cargo.toml | 3 ++- examples/request-handlers/src/handlers_arc.rs | 12 ++++++------ examples/request-handlers/src/main.rs | 12 ++++++------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/examples/request-handlers/Cargo.toml b/examples/request-handlers/Cargo.toml index 325b659..156950c 100644 --- a/examples/request-handlers/Cargo.toml +++ b/examples/request-handlers/Cargo.toml @@ -4,4 +4,5 @@ version = "1.0.0" edition = "2018" [dependencies] -actix-web = "1.0" +actix-web = "2.0" +actix-rt = "1.0" diff --git a/examples/request-handlers/src/handlers_arc.rs b/examples/request-handlers/src/handlers_arc.rs index cd9acbe..766dd15 100644 --- a/examples/request-handlers/src/handlers_arc.rs +++ b/examples/request-handlers/src/handlers_arc.rs @@ -8,17 +8,18 @@ struct AppState { count: Arc, } -fn show_count(data: web::Data) -> impl Responder { +async fn show_count(data: web::Data) -> impl Responder { format!("count: {}", data.count.load(Ordering::Relaxed)) } -fn add_one(data: web::Data) -> impl Responder { +async fn add_one(data: web::Data) -> impl Responder { data.count.fetch_add(1, Ordering::Relaxed); format!("count: {}", data.count.load(Ordering::Relaxed)) } -pub fn main() { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { use actix_web::{App, HttpServer}; let data = AppState { @@ -31,9 +32,8 @@ pub fn main() { .route("/", web::to(show_count)) .route("/add", web::to(add_one)) }) - .bind("127.0.0.1:8088") - .unwrap() + .bind("127.0.0.1:8088")? .run() - .unwrap(); + .await } // diff --git a/examples/request-handlers/src/main.rs b/examples/request-handlers/src/main.rs index 9fb1734..2d29c5f 100644 --- a/examples/request-handlers/src/main.rs +++ b/examples/request-handlers/src/main.rs @@ -8,18 +8,19 @@ struct AppState { count: Cell, } -fn show_count(data: web::Data) -> impl Responder { +async fn show_count(data: web::Data) -> impl Responder { format!("count: {}", data.count.get()) } -fn add_one(data: web::Data) -> impl Responder { +async fn add_one(data: web::Data) -> impl Responder { let count = data.count.get(); data.count.set(count + 1); format!("count: {}", data.count.get()) } -fn main() { +#[actix_rt::main] +async fn main() -> std::io::Result<()> { use actix_web::{App, HttpServer}; let data = AppState { @@ -32,9 +33,8 @@ fn main() { .route("/", web::to(show_count)) .route("/add", web::to(add_one)) }) - .bind("127.0.0.1:8088") - .unwrap() + .bind("127.0.0.1:8088")? .run() - .unwrap(); + .await } //