diff --git a/examples/graphql.rs b/examples/graphql.rs index a78c71c00..f79527c12 100644 --- a/examples/graphql.rs +++ b/examples/graphql.rs @@ -8,19 +8,19 @@ use juniper::graphql_object; use std::sync::{atomic, Arc}; use tide::{error::ResultExt, response, App, Context, EndpointResult}; -// First, we define `Data` that holds accumulator state. This is accessible as App data in +// First, we define `State` that holds accumulator state. This is accessible as state in // Tide, and as executor context in Juniper. #[derive(Clone, Default)] -struct Data(Arc); +struct State(Arc); -impl juniper::Context for Data {} +impl juniper::Context for State {} // We define `Query` unit struct here. GraphQL queries will refer to this struct. The struct itself -// doesn't have any associated data (and there's no need to do so), but instead it exposes the +// doesn't have any associated state (and there's no need to do so), but instead it exposes the // accumulator state from the context. struct Query; -graphql_object!(Query: Data |&self| { +graphql_object!(Query: State |&self| { // GraphQL integers are signed and 32 bits long. field accumulator(&executor) -> i32 as "Current value of the accumulator" { executor.context().0.load(atomic::Ordering::Relaxed) as i32 @@ -31,7 +31,7 @@ graphql_object!(Query: Data |&self| { // `Query`, but it provides the way to "mutate" the accumulator state. struct Mutation; -graphql_object!(Mutation: Data |&self| { +graphql_object!(Mutation: State |&self| { field add(&executor, by: i32) -> i32 as "Add given value to the accumulator." { executor.context().0.fetch_add(by as isize, atomic::Ordering::Relaxed) as i32 + by } @@ -43,7 +43,7 @@ type Schema = juniper::RootNode<'static, Query, Mutation>; // Finally, we'll bridge between Tide and Juniper. `GraphQLRequest` from Juniper implements // `Deserialize`, so we use `Json` extractor to deserialize the request body. -async fn handle_graphql(mut cx: Context) -> EndpointResult { +async fn handle_graphql(mut cx: Context) -> EndpointResult { let query: juniper::http::GraphQLRequest = cx.body_json().await.client_err()?; let schema = Schema::new(Query, Mutation); let response = query.execute(&schema, cx.state()); @@ -58,7 +58,7 @@ async fn handle_graphql(mut cx: Context) -> EndpointResult { } fn main() { - let mut app = App::with_state(Data::default()); + let mut app = App::with_state(State::default()); app.at("/graphql").post(handle_graphql); app.run("127.0.0.1:8000").unwrap(); } diff --git a/src/querystring.rs b/src/querystring.rs index 883108a32..ba28b443b 100644 --- a/src/querystring.rs +++ b/src/querystring.rs @@ -7,7 +7,7 @@ pub trait ContextExt<'de> { fn url_query>(&'de self) -> Result; } -impl<'de, Data> ContextExt<'de> for Context { +impl<'de, State> ContextExt<'de> for Context { #[inline] fn url_query>(&'de self) -> Result { let query = self.uri().query(); diff --git a/tide-compression/src/lib.rs b/tide-compression/src/lib.rs index ec4a989a4..a162748a4 100644 --- a/tide-compression/src/lib.rs +++ b/tide-compression/src/lib.rs @@ -128,8 +128,8 @@ impl Compression { } } -impl Middleware for Compression { - fn handle<'a>(&'a self, cx: Context, next: Next<'a, Data>) -> BoxFuture<'a, Response> { +impl Middleware for Compression { + fn handle<'a>(&'a self, cx: Context, next: Next<'a, State>) -> BoxFuture<'a, Response> { FutureExt::boxed(async move { let encoding = match self.preferred_encoding(cx.headers()) { Ok(encoding) => encoding, @@ -212,11 +212,11 @@ impl Decompression { } } -impl Middleware for Decompression { +impl Middleware for Decompression { fn handle<'a>( &'a self, - mut cx: Context, - next: Next<'a, Data>, + mut cx: Context, + next: Next<'a, State>, ) -> BoxFuture<'a, Response> { FutureExt::boxed(async move { match self.decode(cx.request_mut()) { diff --git a/tide-cookies/src/middleware.rs b/tide-cookies/src/middleware.rs index e8dafd243..9c34ff332 100644 --- a/tide-cookies/src/middleware.rs +++ b/tide-cookies/src/middleware.rs @@ -26,11 +26,11 @@ impl CookiesMiddleware { } } -impl Middleware for CookiesMiddleware { +impl Middleware for CookiesMiddleware { fn handle<'a>( &'a self, - mut cx: Context, - next: Next<'a, Data>, + mut cx: Context, + next: Next<'a, State>, ) -> BoxFuture<'a, Response> { FutureExt::boxed(async move { let cookie_data = cx diff --git a/tide-core/src/app.rs b/tide-core/src/app.rs index e0e0549a0..484fd81db 100644 --- a/tide-core/src/app.rs +++ b/tide-core/src/app.rs @@ -133,7 +133,7 @@ use crate::{ pub struct App { router: Router, middleware: Vec>>, - data: State, + state: State, } impl App<()> { @@ -155,7 +155,7 @@ impl App { App { router: Router::new(), middleware: Vec::new(), - data: state, + state, } } @@ -229,7 +229,7 @@ impl App { pub fn into_http_service(self) -> Server { Server { router: Arc::new(self.router), - data: Arc::new(self.data), + state: Arc::new(self.state), middleware: Arc::new(self.middleware), } } @@ -273,7 +273,7 @@ impl App { #[allow(missing_debug_implementations)] pub struct Server { router: Arc>, - data: Arc, + state: Arc, middleware: Arc>>>, } @@ -291,12 +291,12 @@ impl HttpService for Server { let method = req.method().to_owned(); let router = self.router.clone(); let middleware = self.middleware.clone(); - let data = self.data.clone(); + let state = self.state.clone(); FutureExt::boxed(async move { let fut = { let Selection { endpoint, params } = router.route(&path, method); - let cx = Context::new(data, req, params); + let cx = Context::new(state, req, params); let next = Next { endpoint, @@ -319,19 +319,19 @@ mod tests { use super::*; use crate::{middleware::Next, router::Selection, Context, Response}; - fn simulate_request<'a, Data: Default + Clone + Send + Sync + 'static>( - app: &'a App, + fn simulate_request<'a, State: Default + Clone + Send + Sync + 'static>( + app: &'a App, path: &'a str, method: http::Method, ) -> BoxFuture<'a, Response> { let Selection { endpoint, params } = app.router.route(path, method.clone()); - let data = Arc::new(Data::default()); + let state = Arc::new(State::default()); let req = http::Request::builder() .method(method) .body(http_service::Body::empty()) .unwrap(); - let cx = Context::new(data, req, params); + let cx = Context::new(state, req, params); let next = Next { endpoint, next_middleware: &app.middleware, diff --git a/tide-core/src/context.rs b/tide-core/src/context.rs index 543ac8a5d..d3e1bacd5 100644 --- a/tide-core/src/context.rs +++ b/tide-core/src/context.rs @@ -3,7 +3,7 @@ use http_service::Body; use route_recognizer::Params; use std::{str::FromStr, sync::Arc}; -/// Data associated with a request-response lifecycle. +/// State associated with a request-response lifecycle. /// /// The `Context` gives endpoints access to basic information about the incoming /// request, route parameters, and various ways of accessing the request's body. @@ -60,7 +60,7 @@ impl Context { &mut self.request } - /// Access app-global data. + /// Access the state. pub fn state(&self) -> &State { &self.state } diff --git a/tide-core/src/middleware.rs b/tide-core/src/middleware.rs index 47d1caa13..2b881f27f 100644 --- a/tide-core/src/middleware.rs +++ b/tide-core/src/middleware.rs @@ -9,11 +9,14 @@ pub trait Middleware: 'static + Send + Sync { fn handle<'a>(&'a self, cx: Context, next: Next<'a, State>) -> BoxFuture<'a, Response>; } -impl Middleware for F +impl Middleware for F where - F: Send + Sync + 'static + for<'a> Fn(Context, Next<'a, Data>) -> BoxFuture<'a, Response>, + F: Send + + Sync + + 'static + + for<'a> Fn(Context, Next<'a, State>) -> BoxFuture<'a, Response>, { - fn handle<'a>(&'a self, cx: Context, next: Next<'a, Data>) -> BoxFuture<'a, Response> { + fn handle<'a>(&'a self, cx: Context, next: Next<'a, State>) -> BoxFuture<'a, Response> { (self)(cx, next) } } diff --git a/tide-core/src/router.rs b/tide-core/src/router.rs index eb97a90ba..1ce768dad 100644 --- a/tide-core/src/router.rs +++ b/tide-core/src/router.rs @@ -62,7 +62,7 @@ impl Router { } } -fn not_found_endpoint(_cx: Context) -> BoxFuture<'static, Response> { +fn not_found_endpoint(_cx: Context) -> BoxFuture<'static, Response> { FutureExt::boxed(async move { http::Response::builder() .status(http::StatusCode::NOT_FOUND) diff --git a/tide-headers/src/lib.rs b/tide-headers/src/lib.rs index 36b080567..d5692bd19 100644 --- a/tide-headers/src/lib.rs +++ b/tide-headers/src/lib.rs @@ -49,8 +49,8 @@ impl DefaultHeaders { } } -impl Middleware for DefaultHeaders { - fn handle<'a>(&'a self, cx: Context, next: Next<'a, Data>) -> BoxFuture<'a, Response> { +impl Middleware for DefaultHeaders { + fn handle<'a>(&'a self, cx: Context, next: Next<'a, State>) -> BoxFuture<'a, Response> { FutureExt::boxed(async move { let mut res = next.run(cx).await; let headers = res.headers_mut(); diff --git a/tide-log/src/lib.rs b/tide-log/src/lib.rs index 88edf072b..cc0787657 100644 --- a/tide-log/src/lib.rs +++ b/tide-log/src/lib.rs @@ -47,10 +47,10 @@ impl RequestLogger { Self { target } } - async fn log_basic<'a, Data: Send + Sync + 'static>( + async fn log_basic<'a, State: Send + Sync + 'static>( &'a self, - ctx: Context, - next: Next<'a, Data>, + ctx: Context, + next: Next<'a, State>, ) -> Response { let path = ctx.uri().path().to_owned(); let method = ctx.method().as_str().to_owned(); @@ -70,8 +70,8 @@ impl RequestLogger { } } -impl Middleware for RequestLogger { - fn handle<'a>(&'a self, ctx: Context, next: Next<'a, Data>) -> BoxFuture<'a, Response> { +impl Middleware for RequestLogger { + fn handle<'a>(&'a self, ctx: Context, next: Next<'a, State>) -> BoxFuture<'a, Response> { FutureExt::boxed(async move { self.log_basic(ctx, next).await }) } } diff --git a/tide-slog/src/lib.rs b/tide-slog/src/lib.rs index 1b1724fa7..b0b782208 100644 --- a/tide-slog/src/lib.rs +++ b/tide-slog/src/lib.rs @@ -48,8 +48,8 @@ impl Default for RequestLogger { /// Stores information during request phase and logs information once the response /// is generated. -impl Middleware for RequestLogger { - fn handle<'a>(&'a self, cx: Context, next: Next<'a, Data>) -> BoxFuture<'a, Response> { +impl Middleware for RequestLogger { + fn handle<'a>(&'a self, cx: Context, next: Next<'a, State>) -> BoxFuture<'a, Response> { FutureExt::boxed(async move { let path = cx.uri().path().to_owned(); let method = cx.method().as_str().to_owned();