diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/hyper_body.rs | 27 |
1 files changed, 15 insertions, 12 deletions
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)?) + }) } } |