Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: remove StateData trait #488

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/handlers/async_handlers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use gotham::state::{FromState, State};
type ResponseContentFuture =
Pin<Box<dyn Future<Output = Result<Vec<u8>, gotham::hyper::Error>> + Send>>;

#[derive(Deserialize, StateData, StaticResponseExtender)]
#[derive(Deserialize, StaticResponseExtender)]
struct QueryStringExtractor {
length: i8,
}
Expand Down
6 changes: 3 additions & 3 deletions examples/handlers/multipart/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use futures::prelude::*;
use gotham::handler::HandlerFuture;
use gotham::helpers::http::response::create_response;
use gotham::hyper::header::CONTENT_TYPE;
use gotham::hyper::{body, Body, HeaderMap, StatusCode};
use gotham::hyper::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
use gotham::hyper::{body, Body, StatusCode};
use gotham::router::builder::{build_simple_router, DefineSingleRoute, DrawRoutes};
use gotham::router::Router;
use gotham::state::{FromState, State};
Expand All @@ -15,7 +15,7 @@ use std::pin::Pin;
/// Extracts the elements of the POST request and responds with the form keys and values
fn form_handler(mut state: State) -> Pin<Box<HandlerFuture>> {
const BOUNDARY: &str = "boundary=";
let header_map = HeaderMap::take_from(&mut state);
let header_map = HeaderMap::<HeaderValue>::take_from(&mut state);
lucacasonato marked this conversation as resolved.
Show resolved Hide resolved
let boundary = header_map
.get(CONTENT_TYPE)
.and_then(|ct| {
Expand Down
5 changes: 3 additions & 2 deletions examples/handlers/request_data/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A basic example showing the request components
use futures::prelude::*;
use gotham::hyper::{body, Body, HeaderMap, Method, Response, StatusCode, Uri, Version};
use gotham::hyper::header::{HeaderMap, HeaderValue};
use gotham::hyper::{body, Body, Method, Response, StatusCode, Uri, Version};
use std::pin::Pin;

use gotham::handler::HandlerFuture;
Expand All @@ -14,7 +15,7 @@ fn print_request_elements(state: &State) {
let method = Method::borrow_from(state);
let uri = Uri::borrow_from(state);
let http_version = Version::borrow_from(state);
let headers = HeaderMap::borrow_from(state);
let headers = HeaderMap::<HeaderValue>::borrow_from(state);
println!("Method: {:?}", method);
println!("URI: {:?}", uri);
println!("HTTP Version: {:?}", http_version);
Expand Down
2 changes: 1 addition & 1 deletion examples/handlers/simple_async_handlers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use tokio::time::delay_until;

type SleepFuture = Pin<Box<dyn Future<Output = Vec<u8>> + Send>>;

#[derive(Deserialize, StateData, StaticResponseExtender)]
#[derive(Deserialize, StaticResponseExtender)]
struct QueryStringExtractor {
seconds: u64,
}
Expand Down
4 changes: 2 additions & 2 deletions examples/handlers/simple_async_handlers_await/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use gotham::router::builder::DefineSingleRoute;
use gotham::router::builder::{build_simple_router, DrawRoutes};
use gotham::router::Router;
use gotham::state::{FromState, State};
use gotham_derive::{StateData, StaticResponseExtender};
use gotham_derive::StaticResponseExtender;
use serde_derive::Deserialize;

use tokio::time::delay_until;

type SleepFuture = Pin<Box<dyn Future<Output = Vec<u8>> + Send>>;

#[derive(Deserialize, StateData, StaticResponseExtender)]
#[derive(Deserialize, StaticResponseExtender)]
struct QueryStringExtractor {
seconds: u64,
}
Expand Down
5 changes: 2 additions & 3 deletions examples/middleware/introduction/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::pin::Pin;

use gotham::handler::HandlerFuture;
use gotham::helpers::http::response::create_empty_response;
use gotham::hyper::header::{HeaderMap, USER_AGENT};
use gotham::hyper::header::{HeaderMap, HeaderValue, USER_AGENT};
use gotham::hyper::{Body, Response, StatusCode};
use gotham::middleware::Middleware;
use gotham::pipeline::new_pipeline;
Expand All @@ -19,7 +19,6 @@ use gotham::state::{FromState, State};
/// A simple struct which holds an identifier for the user agent which made the request.
///
/// It is created by our Middleware and then accessed via `state` by both our Middleware and Handler.
#[derive(StateData)]
pub struct ExampleMiddlewareData {
pub user_agent: String,
pub supported: bool,
Expand Down Expand Up @@ -50,7 +49,7 @@ impl Middleware for ExampleMiddleware {
where
Chain: FnOnce(State) -> Pin<Box<HandlerFuture>>,
{
let user_agent = match HeaderMap::borrow_from(&state).get(USER_AGENT) {
let user_agent = match HeaderMap::<HeaderValue>::borrow_from(&state).get(USER_AGENT) {
Some(ua) => ua.to_str().unwrap().to_string(),
None => "None".to_string(),
};
Expand Down
4 changes: 2 additions & 2 deletions examples/middleware/multiple_pipelines/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern crate gotham_derive;
extern crate serde_derive;

use futures::prelude::*;
use gotham::hyper::header::{HeaderMap, ACCEPT};
use gotham::hyper::header::{HeaderMap, HeaderValue, ACCEPT};
use gotham::hyper::{Body, Response, StatusCode};
use std::pin::Pin;

Expand Down Expand Up @@ -54,7 +54,7 @@ impl Middleware for ApiMiddleware {
where
Chain: FnOnce(State) -> Pin<Box<HandlerFuture>> + 'static,
{
let accepts = HeaderMap::borrow_from(&state)
let accepts = HeaderMap::<HeaderValue>::borrow_from(&state)
.get(ACCEPT)
.map(|ct| ct.to_str().unwrap().to_string());

Expand Down
6 changes: 3 additions & 3 deletions examples/path/globs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ use gotham::router::builder::*;
use gotham::router::Router;
use gotham::state::{FromState, State};

#[derive(Deserialize, StateData, StaticResponseExtender)]
#[derive(Deserialize, StaticResponseExtender)]
struct PathExtractor {
// This will be a Vec containing each path segment as a separate String, with no '/'s.
#[serde(rename = "*")]
parts: Vec<String>,
}

#[derive(Deserialize, StateData, StaticResponseExtender)]
#[derive(Deserialize, StaticResponseExtender)]
struct NamedPathExtractor {
parts: Vec<String>,
}

#[derive(Deserialize, StateData, StaticResponseExtender)]
#[derive(Deserialize, StaticResponseExtender)]
struct MultiGlobExtractor {
top: Vec<String>,
bottom: Vec<String>,
Expand Down
7 changes: 2 additions & 5 deletions examples/path/introduction/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ use gotham::state::{FromState, State};
/// deriving it. The Gotham router uses this property during Request path evaluation to
/// create and instance of your struct, populate it and store it into state ready for
/// access by application code.
/// 2. That the struct implements `gotham::state::data::StateData` so that it can be stored,
/// retrieved and removed from state. You generally get this for free by deriving
/// `StateData` as shown here.
/// 3. That the struct implements the
/// 2. That the struct implements the
/// `gotham::router::response::extender::StaticResponseExtender` trait so that bad request
/// path data can be appropriately refused by the Router. You generally get this for free by
/// deriving `StaticResponseExtender` as shown here which results in bad requests being
/// refuted with a HTTP 400 (BadRequest) response status code.
///
/// Naming of fields in extraction structs is important, the same names must appear in the path,
/// proceeded by a colon to indicate a variable, when defining routes.
#[derive(Deserialize, StateData, StaticResponseExtender)]
#[derive(Deserialize, StaticResponseExtender)]
struct PathExtractor {
name: String,
}
Expand Down
2 changes: 1 addition & 1 deletion examples/path/regex/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use gotham::router::builder::*;
use gotham::router::Router;
use gotham::state::{FromState, State};

#[derive(Deserialize, StateData, StaticResponseExtender)]
#[derive(Deserialize, StaticResponseExtender)]
struct PathExtractor {
id: usize,
}
Expand Down
7 changes: 2 additions & 5 deletions examples/query_string/introduction/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,15 @@ use gotham::state::{FromState, State};
/// simply deriving it. The Gotham router uses this property during Request query string
/// evaluation to create and instance of your struct, populate it and store it into state
/// ready for access by application code.
/// 2. That the struct implements `gotham::state::data::StateData` trait so that it can be
/// stored, retrieved and removed from state. You generally get this for free by deriving
/// `StateData` as shown here.
/// 3. That the struct implements the
/// 2. That the struct implements the
/// `gotham::router::response::extender::StaticResponseExtender` trait so that bad request
/// query string data can be appropriately refused by the Router. You generally get this
/// for free by deriving `StaticResponseExtender` as shown here which results in bad
/// requests being refuted with a HTTP 400 (BadRequest) response status code.
///
/// Naming of fields in extraction structs is important, the same names must appear in the
/// query string.
#[derive(Deserialize, StateData, StaticResponseExtender)]
#[derive(Deserialize, StaticResponseExtender)]
struct QueryStringExtractor {
name: String,
}
Expand Down
4 changes: 1 addition & 3 deletions examples/sessions/custom_data_type/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Storing and retrieving session data with a custom data type, in a type safe
//! way, with the Gotham web framework.
#[macro_use]
extern crate gotham_derive;
#[macro_use]
extern crate serde_derive;

use gotham::middleware::session::{NewSessionMiddleware, SessionData};
Expand All @@ -13,7 +11,7 @@ use gotham::router::Router;
use gotham::state::{FromState, State};

// A custom type for storing data associated with the user's session.
#[derive(Clone, Deserialize, Serialize, StateData)]
#[derive(Clone, Deserialize, Serialize)]
struct VisitData {
count: usize,
last_visit: String,
Expand Down
5 changes: 1 addition & 4 deletions examples/shared_state/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@

#![cfg_attr(feature = "cargo-clippy", allow(clippy::mutex_atomic))]

#[macro_use]
extern crate gotham_derive;

use gotham::middleware::state::StateMiddleware;
use gotham::pipeline::single::single_pipeline;
use gotham::pipeline::single_middleware;
Expand All @@ -26,7 +23,7 @@ use std::sync::{Arc, Mutex};
///
/// This struct must implement `Clone` and `StateData` to be applicable
/// for use with the `StateMiddleware`, and be shared via `Middleware`.
#[derive(Clone, StateData)]
#[derive(Clone)]
struct RequestCounter {
inner: Arc<Mutex<usize>>,
}
Expand Down
11 changes: 5 additions & 6 deletions gotham/src/extractor/path.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use hyper::{body::HttpBody, Body, Response};
use serde::{Deserialize, Deserializer};
use std::any::Any;

use crate::router::response::extender::StaticResponseExtender;
use crate::state::{State, StateData};
use crate::state::State;

/// Defines a binding for storing the dynamic segments of the `Request` path in `State`. On failure
/// the `StaticResponseExtender` implementation extends the `Response` to indicate why the
Expand Down Expand Up @@ -35,7 +36,7 @@ use crate::state::{State, StateData};
/// # use gotham::router::builder::*;
/// # use gotham::test::TestServer;
/// #
/// #[derive(Deserialize, StateData, StaticResponseExtender)]
/// #[derive(Deserialize, StaticResponseExtender)]
/// struct MyPathParams {
/// id: i32,
/// slug: String,
Expand Down Expand Up @@ -76,7 +77,7 @@ use crate::state::{State, StateData};
/// # assert_eq!(body, "id = 1551, slug = ten-reasons-serde-is-amazing");
/// # }
pub trait PathExtractor<B>:
for<'de> Deserialize<'de> + StaticResponseExtender<ResBody = B> + StateData
for<'de> Deserialize<'de> + StaticResponseExtender<ResBody = B> + Any + Send
where
B: HttpBody,
{
Expand All @@ -85,7 +86,7 @@ where
impl<T, B> PathExtractor<B> for T
where
B: HttpBody,
for<'de> T: Deserialize<'de> + StaticResponseExtender<ResBody = B> + StateData,
for<'de> T: Deserialize<'de> + StaticResponseExtender<ResBody = B> + Any + Send,
{
}

Expand All @@ -107,8 +108,6 @@ impl<'de> Deserialize<'de> for NoopPathExtractor {
}
}

impl StateData for NoopPathExtractor {}

impl StaticResponseExtender for NoopPathExtractor {
type ResBody = Body;
fn extend(_state: &mut State, _res: &mut Response<Body>) {}
Expand Down
11 changes: 5 additions & 6 deletions gotham/src/extractor/query_string.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use hyper::{body::HttpBody, Body, Response};
use serde::{Deserialize, Deserializer};
use std::any::Any;

use crate::router::response::extender::StaticResponseExtender;
use crate::state::{State, StateData};
use crate::state::State;

/// Defines a binding for storing the query parameters from the `Request` URI in `State`. On
/// failure the `StaticResponseExtender` implementation extends the `Response` to indicate why the
Expand Down Expand Up @@ -35,7 +36,7 @@ use crate::state::{State, StateData};
/// # use gotham::router::builder::*;
/// # use gotham::test::TestServer;
/// #
/// #[derive(Deserialize, StateData, StaticResponseExtender)]
/// #[derive(Deserialize, StaticResponseExtender)]
/// struct MyQueryParams {
/// x: i32,
/// y: MyEnum,
Expand Down Expand Up @@ -84,7 +85,7 @@ use crate::state::{State, StateData};
/// # assert_eq!(body, "x = 15, y = B");
/// # }
pub trait QueryStringExtractor<B>:
for<'de> Deserialize<'de> + StaticResponseExtender<ResBody = B> + StateData
for<'de> Deserialize<'de> + StaticResponseExtender<ResBody = B> + Any + Send
where
B: HttpBody,
{
Expand All @@ -93,7 +94,7 @@ where
impl<T, B> QueryStringExtractor<B> for T
where
B: HttpBody,
for<'de> T: Deserialize<'de> + StaticResponseExtender<ResBody = B> + StateData,
for<'de> T: Deserialize<'de> + StaticResponseExtender<ResBody = B> + Any + Send,
{
}

Expand All @@ -117,8 +118,6 @@ impl<'de> Deserialize<'de> for NoopQueryStringExtractor {
}
}

impl StateData for NoopQueryStringExtractor {}

impl StaticResponseExtender for NoopQueryStringExtractor {
type ResBody = Body;
fn extend(_state: &mut State, _res: &mut Response<Body>) {}
Expand Down
4 changes: 1 addition & 3 deletions gotham/src/handler/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use tokio::io::AsyncRead;
use self::accepted_encoding::accepted_encodings;
use crate::handler::{Handler, HandlerError, HandlerFuture, NewHandler};
use crate::router::response::extender::StaticResponseExtender;
use crate::state::{FromState, State, StateData};
use crate::state::{FromState, State};

use std::cmp;
use std::convert::From;
Expand Down Expand Up @@ -358,8 +358,6 @@ pub struct FilePathExtractor {
parts: Vec<String>,
}

impl StateData for FilePathExtractor {}

impl StaticResponseExtender for FilePathExtractor {
type ResBody = Body;
fn extend(_state: &mut State, _res: &mut Response<Self::ResBody>) {}
Expand Down
1 change: 0 additions & 1 deletion gotham/src/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ pub mod timer;
/// #[derive(NewMiddleware, Copy, Clone)]
/// struct MiddlewareWithStateData;
///
/// #[derive(StateData)]
/// struct MiddlewareStateData {
/// i: i32,
/// }
Expand Down
9 changes: 1 addition & 8 deletions gotham/src/middleware/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use super::cookie::CookieParser;
use super::{Middleware, NewMiddleware};
use crate::handler::{HandlerError, HandlerFuture};
use crate::helpers::http::response::create_empty_response;
use crate::state::{self, FromState, State, StateData};
use crate::state::{self, FromState, State};

mod backend;
mod rng;
Expand Down Expand Up @@ -372,11 +372,6 @@ where
}
}

impl<T> StateData for SessionData<T> where
T: Default + Serialize + for<'de> Deserialize<'de> + Send + 'static
{
}

impl<T> Deref for SessionData<T>
where
T: Default + Serialize + for<'de> Deserialize<'de> + Send + 'static,
Expand All @@ -398,8 +393,6 @@ where
}
}

impl StateData for SessionDropData {}

trait SessionTypePhantom<T>: Send + Sync + RefUnwindSafe
where
T: Send,
Expand Down