From e38efed55736339ba40e19eceffa66d22c7af6dc Mon Sep 17 00:00:00 2001 From: Fangdun Tsai Date: Tue, 12 Dec 2023 03:04:44 +0800 Subject: [PATCH] refactor: RequestExt --- viz-core/src/from_request.rs | 3 +-- viz-core/src/request.rs | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/viz-core/src/from_request.rs b/viz-core/src/from_request.rs index 8347af61..74549449 100644 --- a/viz-core/src/from_request.rs +++ b/viz-core/src/from_request.rs @@ -1,9 +1,8 @@ //! Extracts data from the [Request] by types. use std::convert::Infallible; -use std::future::Future; -use crate::{IntoResponse, Request}; +use crate::{Future, IntoResponse, Request}; /// An interface for extracting data from the HTTP [`Request`]. pub trait FromRequest: Sized { diff --git a/viz-core/src/request.rs b/viz-core/src/request.rs index 44d1fd87..64be4cb6 100644 --- a/viz-core/src/request.rs +++ b/viz-core/src/request.rs @@ -1,9 +1,9 @@ use std::mem::replace; use crate::{ - async_trait, header, + header, types::{PayloadError, RealIp}, - Bytes, FromRequest, Incoming, IncomingBody, Request, Result, + Bytes, FromRequest, Future, Incoming, IncomingBody, Request, Result, }; use headers::HeaderMapExt; use http_body_util::{BodyExt, Collected}; @@ -38,7 +38,6 @@ use crate::types::Session; use crate::types::{ParamsError, PathDeserializer, RouteInfo}; /// The [Request] Extension. -#[async_trait] pub trait RequestExt: Sized { /// Get URL's schema of this request. fn schema(&self) -> Option<&http::uri::Scheme>; @@ -77,7 +76,7 @@ pub trait RequestExt: Sized { fn content_type(&self) -> Option; /// Extract the data from this request by the specified type. - async fn extract(&mut self) -> Result + fn extract(&mut self) -> impl Future> + Send where T: FromRequest; @@ -95,25 +94,29 @@ pub trait RequestExt: Sized { /// Return with a [Bytes][mdn] representation of the request body. /// /// [mdn]: - async fn bytes(&mut self) -> Result; + fn bytes(&mut self) -> impl Future> + Send; /// Return with a [Bytes][mdn] by a limit representation of the request body. /// /// [mdn]: #[cfg(feature = "limits")] - async fn bytes_with(&mut self, limit: Option, max: u64) -> Result; + fn bytes_with( + &mut self, + limit: Option, + max: u64, + ) -> impl Future> + Send; /// Return with a [Text][mdn] representation of the request body. /// /// [mdn]: - async fn text(&mut self) -> Result; + fn text(&mut self) -> impl Future> + Send; /// Return with a `application/x-www-form-urlencoded` [FormData][mdn] by the specified type /// representation of the request body. /// /// [mdn]: #[cfg(feature = "form")] - async fn form(&mut self) -> Result + fn form(&mut self) -> impl Future> + Send where T: serde::de::DeserializeOwned; @@ -121,7 +124,7 @@ pub trait RequestExt: Sized { /// /// [mdn]: #[cfg(feature = "json")] - async fn json(&mut self) -> Result + fn json(&mut self) -> impl Future> + Send where T: serde::de::DeserializeOwned; @@ -130,7 +133,7 @@ pub trait RequestExt: Sized { /// /// [mdn]: #[cfg(feature = "multipart")] - async fn multipart(&mut self) -> Result; + fn multipart(&mut self) -> impl Future> + Send; /// Return a shared state by the specified type. #[cfg(feature = "state")] @@ -198,7 +201,6 @@ pub trait RequestExt: Sized { fn realip(&self) -> Option; } -#[async_trait] impl RequestExt for Request { fn schema(&self) -> Option<&http::uri::Scheme> { self.uri().scheme()