From cc07b8dbd00f3f7683bae0990d657e1c8f6586fd Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sun, 26 Jun 2022 19:21:59 +0200 Subject: [PATCH] fix examples --- axum-extra/src/routing/mod.rs | 34 +++++++++---------- axum/src/docs/extract.md | 2 ++ .../src/main.rs | 14 ++++---- .../customize-extractor-error/src/main.rs | 6 ++-- examples/customize-path-rejection/src/main.rs | 4 +-- .../src/main.rs | 2 +- examples/jwt/src/main.rs | 4 +-- examples/key-value-store/src/main.rs | 2 +- examples/oauth/src/main.rs | 4 +-- examples/sessions/src/main.rs | 4 +-- examples/sqlx-postgres/src/main.rs | 4 +-- examples/todos/src/main.rs | 4 +-- examples/tokio-postgres/src/main.rs | 4 +-- examples/validator/src/main.rs | 6 ++-- examples/versioning/src/main.rs | 4 +-- 15 files changed, 49 insertions(+), 49 deletions(-) diff --git a/axum-extra/src/routing/mod.rs b/axum-extra/src/routing/mod.rs index 8e693e2b..4b742242 100644 --- a/axum-extra/src/routing/mod.rs +++ b/axum-extra/src/routing/mod.rs @@ -1,6 +1,6 @@ //! Additional types for defining routes. -use axum::{handler::Handler, Router}; +use axum::Router; mod resource; @@ -32,7 +32,7 @@ pub trait RouterExt: sealed::Sealed { #[cfg(feature = "typed-routing")] fn typed_get(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath; @@ -45,7 +45,7 @@ pub trait RouterExt: sealed::Sealed { #[cfg(feature = "typed-routing")] fn typed_delete(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath; @@ -58,7 +58,7 @@ pub trait RouterExt: sealed::Sealed { #[cfg(feature = "typed-routing")] fn typed_head(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath; @@ -71,7 +71,7 @@ pub trait RouterExt: sealed::Sealed { #[cfg(feature = "typed-routing")] fn typed_options(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath; @@ -84,7 +84,7 @@ pub trait RouterExt: sealed::Sealed { #[cfg(feature = "typed-routing")] fn typed_patch(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath; @@ -97,7 +97,7 @@ pub trait RouterExt: sealed::Sealed { #[cfg(feature = "typed-routing")] fn typed_post(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath; @@ -110,7 +110,7 @@ pub trait RouterExt: sealed::Sealed { #[cfg(feature = "typed-routing")] fn typed_put(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath; @@ -123,7 +123,7 @@ pub trait RouterExt: sealed::Sealed { #[cfg(feature = "typed-routing")] fn typed_trace(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath; } @@ -135,7 +135,7 @@ where #[cfg(feature = "typed-routing")] fn typed_get(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath, { @@ -145,7 +145,7 @@ where #[cfg(feature = "typed-routing")] fn typed_delete(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath, { @@ -155,7 +155,7 @@ where #[cfg(feature = "typed-routing")] fn typed_head(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath, { @@ -165,7 +165,7 @@ where #[cfg(feature = "typed-routing")] fn typed_options(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath, { @@ -175,7 +175,7 @@ where #[cfg(feature = "typed-routing")] fn typed_patch(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath, { @@ -185,7 +185,7 @@ where #[cfg(feature = "typed-routing")] fn typed_post(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath, { @@ -195,7 +195,7 @@ where #[cfg(feature = "typed-routing")] fn typed_put(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath, { @@ -205,7 +205,7 @@ where #[cfg(feature = "typed-routing")] fn typed_trace(self, handler: H) -> Self where - H: Handler, + H: axum::handler::Handler, T: FirstElementIs

+ 'static, P: TypedPath, { diff --git a/axum/src/docs/extract.md b/axum/src/docs/extract.md index 1de7a84f..3e0fdb7b 100644 --- a/axum/src/docs/extract.md +++ b/axum/src/docs/extract.md @@ -390,6 +390,8 @@ use http::{StatusCode, header::{HeaderValue, USER_AGENT}}; struct ExtractUserAgent(HeaderValue); +// if you need to consume the request body use `axum::extract::Once` instead of +// `R`, otherwise use a generic type. #[async_trait] impl FromRequest for ExtractUserAgent where diff --git a/examples/consume-body-in-extractor-or-middleware/src/main.rs b/examples/consume-body-in-extractor-or-middleware/src/main.rs index 1fdd9022..7b1d12cf 100644 --- a/examples/consume-body-in-extractor-or-middleware/src/main.rs +++ b/examples/consume-body-in-extractor-or-middleware/src/main.rs @@ -7,7 +7,7 @@ use axum::{ async_trait, body::{self, BoxBody, Bytes, Full}, - extract::{FromRequest, RequestParts}, + extract::{FromRequest, Once, RequestParts}, http::{Request, StatusCode}, middleware::{self, Next}, response::{IntoResponse, Response}, @@ -72,26 +72,24 @@ fn do_thing_with_request_body(bytes: Bytes) { tracing::debug!(body = ?bytes); } -async fn handler(_: PrintRequestBody, body: Bytes) { +async fn handler(PrintRequestBody(body): PrintRequestBody) { tracing::debug!(?body, "handler received body"); } // extractor that shows how to consume the request body upfront -struct PrintRequestBody; +struct PrintRequestBody(BoxBody); #[async_trait] -impl FromRequest for PrintRequestBody { +impl FromRequest for PrintRequestBody { type Rejection = Response; - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { let request = Request::from_request(req) .await .map_err(|err| err.into_response())?; let request = buffer_request_body(request).await?; - *req = RequestParts::new(request); - - Ok(Self) + Ok(Self(request.into_body())) } } diff --git a/examples/customize-extractor-error/src/main.rs b/examples/customize-extractor-error/src/main.rs index bc0973b4..d28874f1 100644 --- a/examples/customize-extractor-error/src/main.rs +++ b/examples/customize-extractor-error/src/main.rs @@ -6,7 +6,7 @@ use axum::{ async_trait, - extract::{rejection::JsonRejection, FromRequest, RequestParts}, + extract::{rejection::JsonRejection, FromRequest, RequestParts, Once}, http::StatusCode, routing::post, BoxError, Router, @@ -53,7 +53,7 @@ struct User { struct Json(T); #[async_trait] -impl FromRequest for Json +impl FromRequest for Json where // these trait bounds are copied from `impl FromRequest for axum::Json` T: DeserializeOwned, @@ -63,7 +63,7 @@ where { type Rejection = (StatusCode, axum::Json); - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { match axum::Json::::from_request(req).await { Ok(value) => Ok(Self(value.0)), Err(rejection) => { diff --git a/examples/customize-path-rejection/src/main.rs b/examples/customize-path-rejection/src/main.rs index 4e268949..c9c7d805 100644 --- a/examples/customize-path-rejection/src/main.rs +++ b/examples/customize-path-rejection/src/main.rs @@ -52,7 +52,7 @@ struct Params { struct Path(T); #[async_trait] -impl FromRequest for Path +impl FromRequest for Path where // these trait bounds are copied from `impl FromRequest for axum::extract::path::Path` T: DeserializeOwned + Send, @@ -60,7 +60,7 @@ where { type Rejection = (StatusCode, axum::Json); - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { match axum::extract::Path::::from_request(req).await { Ok(value) => Ok(Self(value.0)), Err(rejection) => { diff --git a/examples/error-handling-and-dependency-injection/src/main.rs b/examples/error-handling-and-dependency-injection/src/main.rs index d92b43bf..31289ec2 100644 --- a/examples/error-handling-and-dependency-injection/src/main.rs +++ b/examples/error-handling-and-dependency-injection/src/main.rs @@ -68,8 +68,8 @@ async fn users_show( /// Handler for `POST /users`. async fn users_create( - Json(params): Json, Extension(user_repo): Extension, + Json(params): Json, ) -> Result, AppError> { let user = user_repo.create(params).await?; diff --git a/examples/jwt/src/main.rs b/examples/jwt/src/main.rs index 0ac4053e..4878244e 100644 --- a/examples/jwt/src/main.rs +++ b/examples/jwt/src/main.rs @@ -122,13 +122,13 @@ impl AuthBody { } #[async_trait] -impl FromRequest for Claims +impl FromRequest for Claims where B: Send, { type Rejection = AuthError; - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { // Extract the token from the authorization header let TypedHeader(Authorization(bearer)) = TypedHeader::>::from_request(req) diff --git a/examples/key-value-store/src/main.rs b/examples/key-value-store/src/main.rs index 0ad5b7f6..99ecb12c 100644 --- a/examples/key-value-store/src/main.rs +++ b/examples/key-value-store/src/main.rs @@ -95,8 +95,8 @@ async fn kv_get( async fn kv_set( Path(key): Path, - ContentLengthLimit(bytes): ContentLengthLimit, // ~5mb Extension(state): Extension, + ContentLengthLimit(bytes): ContentLengthLimit, // ~5mb ) { state.write().unwrap().db.insert(key, bytes); } diff --git a/examples/oauth/src/main.rs b/examples/oauth/src/main.rs index 6357a7fc..084aad34 100644 --- a/examples/oauth/src/main.rs +++ b/examples/oauth/src/main.rs @@ -205,14 +205,14 @@ impl IntoResponse for AuthRedirect { } #[async_trait] -impl FromRequest for User +impl FromRequest for User where B: Send, { // If anything goes wrong or no session is found, redirect to the auth page type Rejection = AuthRedirect; - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { let Extension(store) = Extension::::from_request(req) .await .expect("`MemoryStore` extension is missing"); diff --git a/examples/sessions/src/main.rs b/examples/sessions/src/main.rs index 3251122c..d8dd9787 100644 --- a/examples/sessions/src/main.rs +++ b/examples/sessions/src/main.rs @@ -82,13 +82,13 @@ enum UserIdFromSession { } #[async_trait] -impl FromRequest for UserIdFromSession +impl FromRequest for UserIdFromSession where B: Send, { type Rejection = (StatusCode, &'static str); - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { let Extension(store) = Extension::::from_request(req) .await .expect("`MemoryStore` extension missing"); diff --git a/examples/sqlx-postgres/src/main.rs b/examples/sqlx-postgres/src/main.rs index 9d101618..3d6cbbd6 100644 --- a/examples/sqlx-postgres/src/main.rs +++ b/examples/sqlx-postgres/src/main.rs @@ -77,13 +77,13 @@ async fn using_connection_pool_extractor( struct DatabaseConnection(sqlx::pool::PoolConnection); #[async_trait] -impl FromRequest for DatabaseConnection +impl FromRequest for DatabaseConnection where B: Send, { type Rejection = (StatusCode, String); - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { let Extension(pool) = Extension::::from_request(req) .await .map_err(internal_error)?; diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 9a33416b..0211ac13 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -107,8 +107,8 @@ struct CreateTodo { } async fn todos_create( - Json(input): Json, Extension(db): Extension, + Json(input): Json, ) -> impl IntoResponse { let todo = Todo { id: Uuid::new_v4(), @@ -129,8 +129,8 @@ struct UpdateTodo { async fn todos_update( Path(id): Path, - Json(input): Json, Extension(db): Extension, + Json(input): Json, ) -> Result { let mut todo = db .read() diff --git a/examples/tokio-postgres/src/main.rs b/examples/tokio-postgres/src/main.rs index 4489f616..defacf83 100644 --- a/examples/tokio-postgres/src/main.rs +++ b/examples/tokio-postgres/src/main.rs @@ -71,13 +71,13 @@ async fn using_connection_pool_extractor( struct DatabaseConnection(PooledConnection<'static, PostgresConnectionManager>); #[async_trait] -impl FromRequest for DatabaseConnection +impl FromRequest for DatabaseConnection where B: Send, { type Rejection = (StatusCode, String); - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { let Extension(pool) = Extension::::from_request(req) .await .map_err(internal_error)?; diff --git a/examples/validator/src/main.rs b/examples/validator/src/main.rs index c8ce8c08..9af581d6 100644 --- a/examples/validator/src/main.rs +++ b/examples/validator/src/main.rs @@ -12,7 +12,7 @@ use async_trait::async_trait; use axum::{ - extract::{Form, FromRequest, RequestParts}, + extract::{Form, FromRequest, RequestParts, Once}, http::StatusCode, response::{Html, IntoResponse, Response}, routing::get, @@ -60,7 +60,7 @@ async fn handler(ValidatedForm(input): ValidatedForm) -> Html pub struct ValidatedForm(pub T); #[async_trait] -impl FromRequest for ValidatedForm +impl FromRequest for ValidatedForm where T: DeserializeOwned + Validate, B: http_body::Body + Send, @@ -69,7 +69,7 @@ where { type Rejection = ServerError; - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { let Form(value) = Form::::from_request(req).await?; value.validate()?; Ok(ValidatedForm(value)) diff --git a/examples/versioning/src/main.rs b/examples/versioning/src/main.rs index 48ade3c9..91966362 100644 --- a/examples/versioning/src/main.rs +++ b/examples/versioning/src/main.rs @@ -48,13 +48,13 @@ enum Version { } #[async_trait] -impl FromRequest for Version +impl FromRequest for Version where B: Send, { type Rejection = Response; - async fn from_request(req: &mut RequestParts) -> Result { + async fn from_request(req: &mut RequestParts) -> Result { let params = Path::>::from_request(req) .await .map_err(IntoResponse::into_response)?;