diff options
-rw-r--r-- | Cargo.toml | 3 | ||||
-rw-r--r-- | src/hyper_body.rs | 27 |
2 files changed, 16 insertions, 14 deletions
@@ -12,7 +12,7 @@ keywords = ["web", "cookie", "hyper", "hmac"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -hyper_body = ["hyper", "async-trait"] +hyper_body = ["hyper"] hyper_body_json = ["serde_json", "hyper_body"] security = ["base64", "hmac", "rand", "sha2"] @@ -25,7 +25,6 @@ mime = "0.3" thiserror = "1.0" hyper = { version = "0.14", optional = true } -async-trait = { version = "0.1", optional = true } serde_json = { version = "1.0", optional = true } base64 = { version = "0.13", optional = true } diff --git a/src/hyper_body.rs b/src/hyper_body.rs index c6824fb..50efdc3 100644 --- a/src/hyper_body.rs +++ b/src/hyper_body.rs @@ -1,6 +1,7 @@ //! Extends `hyper::Body` with [`SputnikBody`]. +use std::{future::Future, pin::Pin}; + use hyper::http::{self, response::Builder}; -use async_trait::async_trait; use serde::de::DeserializeOwned; use crate::response::EmptyBuilder; @@ -11,31 +12,33 @@ impl EmptyBuilder<hyper::Body> for Builder { } } -/// Adds deserialization methods to [`hyper::Body`]. -#[async_trait] +/// Adds deserialization methods to `hyper::Body`. pub trait SputnikBody { /// Parses a `application/x-www-form-urlencoded` request body into a given struct. - async fn into_form<T: DeserializeOwned>(self) -> Result<T, FormError>; + fn into_form<T: DeserializeOwned>(self) -> Pin<Box<dyn Future<Output=Result<T, FormError>> + Send + Sync>>; /// Attempts to deserialize the request body as JSON. #[cfg(feature = "hyper_body_json")] #[cfg_attr(docsrs, doc(cfg(feature = "hyper_body_json")))] - async fn into_json<T: DeserializeOwned>(self) -> Result<T, JsonError>; + fn into_json<T: DeserializeOwned>(self) -> Pin<Box<dyn Future<Output=Result<T, JsonError>> + Send + Sync>>; } -#[async_trait] impl SputnikBody for hyper::Body { - async fn into_form<T: DeserializeOwned>(self) -> Result<T, FormError> { - let full_body = hyper::body::to_bytes(self).await.map_err(BodyError)?; - Ok(serde_urlencoded::from_bytes::<T>(&full_body)?) + fn into_form<T: DeserializeOwned>(self) -> Pin<Box<dyn Future<Output=Result<T, FormError>> + Send + Sync>> { + Box::pin(async move { + let full_body = hyper::body::to_bytes(self).await.map_err(BodyError)?; + Ok(serde_urlencoded::from_bytes::<T>(&full_body)?) + }) } #[cfg(feature = "hyper_body_json")] #[cfg_attr(docsrs, doc(cfg(feature = "hyper_body_json")))] - async fn into_json<T: DeserializeOwned>(self) -> Result<T, JsonError> { - let full_body = hyper::body::to_bytes(self).await.map_err(BodyError)?; - Ok(serde_json::from_slice::<T>(&full_body)?) + fn into_json<T: DeserializeOwned>(self) -> Pin<Box<dyn Future<Output=Result<T, JsonError>> + Send + Sync>> { + Box::pin(async move { + let full_body = hyper::body::to_bytes(self).await.map_err(BodyError)?; + Ok(serde_json::from_slice::<T>(&full_body)?) + }) } } |