aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-04-10 19:13:43 +0200
committerMartin Fischer <martin@push-f.com>2021-04-10 19:33:43 +0200
commit0ee5da0f82ffbc233bfc8bc8d338cf605728de60 (patch)
treef5f997c5fce75b355e9063ef4c25cfa787ad45d4 /src
parent0296cab7787b9d993ba2d932a0b261e6a9b77108 (diff)
drop dependency on async-trait
Also has the benefit that the futures are now Sync.
Diffstat (limited to 'src')
-rw-r--r--src/hyper_body.rs27
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)?)
+ })
}
}