From b31c8e3308a29cc76f124b42e3d0d7123a2a6b35 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 16 Mar 2019 20:23:09 -0700 Subject: [PATCH] rename web::State to web::Data --- actix_todo/src/api.rs | 12 ++++++------ async_db/src/main.rs | 4 ++-- diesel/src/main.rs | 8 ++++---- form/src/main.rs | 2 +- juniper/src/main.rs | 2 +- r2d2/src/main.rs | 2 +- state/src/main.rs | 14 +++++++------- template_tera/src/main.rs | 2 +- 8 files changed, 23 insertions(+), 23 deletions(-) diff --git a/actix_todo/src/api.rs b/actix_todo/src/api.rs index 069e5db8..833e2b2d 100644 --- a/actix_todo/src/api.rs +++ b/actix_todo/src/api.rs @@ -9,8 +9,8 @@ use crate::db; use crate::session::{self, FlashMessage}; pub fn index( - pool: web::State, - tmpl: web::State, + pool: web::Data, + tmpl: web::Data, session: Session, ) -> impl Future { web::block(move || db::get_all_tasks(&pool)) @@ -45,7 +45,7 @@ pub struct CreateForm { pub fn create( params: web::Form, - pool: web::State, + pool: web::Data, session: Session, ) -> impl Future { if params.description.is_empty() { @@ -86,7 +86,7 @@ pub struct UpdateForm { } pub fn update( - db: web::State, + db: web::Data, params: web::Path, form: web::Form, session: Session, @@ -102,7 +102,7 @@ pub fn update( } fn toggle( - pool: web::State, + pool: web::Data, params: web::Path, ) -> impl Future { web::block(move || db::toggle_task(params.id, &pool)) @@ -114,7 +114,7 @@ fn toggle( } fn delete( - pool: web::State, + pool: web::Data, params: web::Path, session: Session, ) -> impl Future { diff --git a/async_db/src/main.rs b/async_db/src/main.rs index e9583999..e5648b23 100644 --- a/async_db/src/main.rs +++ b/async_db/src/main.rs @@ -21,7 +21,7 @@ use db::{Pool, Queries, WeatherAgg}; /// Version 1: Calls 4 queries in sequential order, as an asynchronous handler fn asyncio_weather( - db: web::State, + db: web::Data, ) -> impl Future { let mut result: Vec> = vec![]; @@ -52,7 +52,7 @@ fn asyncio_weather( /// Version 2: Calls 4 queries in parallel, as an asynchronous handler /// Returning Error types turn into None values in the response fn parallel_weather( - db: web::State, + db: web::Data, ) -> impl Future { let fut_result = vec![ Box::new(db::execute(&db, Queries::GetTopTenHottestYears)), diff --git a/diesel/src/main.rs b/diesel/src/main.rs index 8ff8ce68..044db371 100644 --- a/diesel/src/main.rs +++ b/diesel/src/main.rs @@ -24,7 +24,7 @@ type Pool = r2d2::Pool>; /// Diesel query fn query( nm: String, - pool: web::State, + pool: web::Data, ) -> Result { use self::schema::users::dsl::*; @@ -44,7 +44,7 @@ fn query( /// Async request handler fn add( name: web::Path, - pool: web::State, + pool: web::Data, ) -> impl Future { // run diesel blocking code web::block(move || query(name.into_inner(), pool)).then(|res| match res { @@ -63,7 +63,7 @@ const MAX_SIZE: usize = 262_144; // max payload size is 256k /// This handler manually load request payload and parse json object fn index_add

( pl: web::Payload

, - pool: web::State, + pool: web::Data, ) -> impl Future where P: Stream, @@ -110,7 +110,7 @@ where fn add2( item: web::Json, - pool: web::State, + pool: web::Data, ) -> impl Future { // run diesel blocking code web::block(move || query(item.into_inner().name, pool)).then(|res| match res { diff --git a/form/src/main.rs b/form/src/main.rs index c0c38690..3219ccba 100644 --- a/form/src/main.rs +++ b/form/src/main.rs @@ -45,7 +45,7 @@ fn handle_post_1(params: web::Form) -> Result { /// State and POST Params fn handle_post_2( - state: web::State, + state: web::Data, params: web::Form, ) -> HttpResponse { HttpResponse::Ok().content_type("text/plain").body(format!( diff --git a/juniper/src/main.rs b/juniper/src/main.rs index 34967eb1..433688e0 100644 --- a/juniper/src/main.rs +++ b/juniper/src/main.rs @@ -24,7 +24,7 @@ fn graphiql() -> HttpResponse { } fn graphql( - st: web::State>, + st: web::Data>, data: web::Json, ) -> impl Future { web::block(move || { diff --git a/r2d2/src/main.rs b/r2d2/src/main.rs index 9b1060d2..7b7f91b3 100644 --- a/r2d2/src/main.rs +++ b/r2d2/src/main.rs @@ -10,7 +10,7 @@ use uuid; /// Async request handler. Ddb pool is stored in application state. fn index( path: web::Path, - db: web::State>, + db: web::Data>, ) -> impl Future { // execute sync code in threadpool web::block(move || { diff --git a/state/src/main.rs b/state/src/main.rs index f67e9290..642d34ef 100644 --- a/state/src/main.rs +++ b/state/src/main.rs @@ -1,12 +1,12 @@ #![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] -//! Application may have multiple states that are shared across -//! all handlers within same Application. State could be added -//! with `App::state()` method, multiple different states could be added. +//! Application may have multiple data objects that are shared across +//! all handlers within same Application. Data could be added +//! with `App::data()` method, multiple different data objects could be added. //! //! > **Note**: http server accepts an application factory rather than an //! application > instance. Http server constructs an application instance for -//! each thread, > thus application state -//! > must be constructed multiple times. If you want to share state between +//! each thread, > thus application data +//! > must be constructed multiple times. If you want to share data between //! different > threads, a shared object should be used, e.g. `Arc`. //! //! Check [user guide](https://actix.rs/book/actix-web/sec-2-application.html) for more info. @@ -17,7 +17,7 @@ use std::sync::{Arc, Mutex}; use actix_web::{middleware, web, App, HttpRequest, HttpResponse, HttpServer}; /// simple handle -fn index(state: web::State>>, req: HttpRequest) -> HttpResponse { +fn index(state: web::Data>>, req: HttpRequest) -> HttpResponse { println!("{:?}", req); *(state.lock().unwrap()) += 1; @@ -33,7 +33,7 @@ fn main() -> io::Result<()> { //move is necessary to give closure below ownership of counter HttpServer::new(move || { App::new() - .state(counter.clone()) // <- create app with shared state + .data(counter.clone()) // <- create app with shared state // enable logger .middleware(middleware::Logger::default()) // register simple handler, handle all methods diff --git a/template_tera/src/main.rs b/template_tera/src/main.rs index b442e82a..37c9d93b 100644 --- a/template_tera/src/main.rs +++ b/template_tera/src/main.rs @@ -7,7 +7,7 @@ use actix_web::{error, middleware, web, App, Error, HttpResponse, HttpServer}; // store tera template in application state fn index( - tmpl: web::State, + tmpl: web::Data, query: web::Query>, ) -> Result { let s = if let Some(name) = query.get("name") {