Don't force handlers to return Results

This commit is contained in:
David Pedersen
2021-05-31 20:42:57 +02:00
parent 6f5b2708d5
commit d33be9683c
6 changed files with 26 additions and 21 deletions
+8 -8
View File
@@ -24,7 +24,7 @@ pub trait Handler<B, In>: Sized {
#[doc(hidden)]
type Sealed: sealed::HiddentTrait;
async fn call(self, req: Request<Body>) -> Result<Self::Response, Error>;
async fn call(self, req: Request<Body>) -> Result<Response<B>, Error>;
fn layer<L>(self, layer: L) -> Layered<L::Service, In>
where
@@ -38,15 +38,15 @@ pub trait Handler<B, In>: Sized {
impl<F, Fut, B, Res> Handler<B, ()> for F
where
F: Fn(Request<Body>) -> Fut + Send + Sync,
Fut: Future<Output = Result<Res, Error>> + Send,
Fut: Future<Output = Res> + Send,
Res: IntoResponse<B>,
{
type Response = Res;
type Sealed = sealed::Hidden;
async fn call(self, req: Request<Body>) -> Result<Self::Response, Error> {
self(req).await
async fn call(self, req: Request<Body>) -> Result<Response<B>, Error> {
self(req).await.into_response()
}
}
@@ -59,7 +59,7 @@ macro_rules! impl_handler {
impl<F, Fut, B, Res, $head, $($tail,)*> Handler<B, ($head, $($tail,)*)> for F
where
F: Fn(Request<Body>, $head, $($tail,)*) -> Fut + Send + Sync,
Fut: Future<Output = Result<Res, Error>> + Send,
Fut: Future<Output = Res> + Send,
Res: IntoResponse<B>,
$head: FromRequest + Send,
$( $tail: FromRequest + Send, )*
@@ -68,13 +68,13 @@ macro_rules! impl_handler {
type Sealed = sealed::Hidden;
async fn call(self, mut req: Request<Body>) -> Result<Self::Response, Error> {
async fn call(self, mut req: Request<Body>) -> Result<Response<B>, Error> {
let $head = $head::from_request(&mut req).await?;
$(
let $tail = $tail::from_request(&mut req).await?;
)*
let res = self(req, $head, $($tail,)*).await?;
Ok(res)
let res = self(req, $head, $($tail,)*).await;
res.into_response()
}
}
+9
View File
@@ -7,6 +7,15 @@ pub trait IntoResponse<B> {
fn into_response(self) -> Result<Response<B>, Error>;
}
impl<B, T> IntoResponse<B> for Result<T, Error>
where
T: IntoResponse<B>,
{
fn into_response(self) -> Result<Response<B>, Error> {
self.and_then(IntoResponse::into_response)
}
}
impl<B> IntoResponse<B> for Response<B> {
fn into_response(self) -> Result<Response<B>, Error> {
Ok(self)
+1 -5
View File
@@ -263,11 +263,7 @@ async fn boxing() {
let client = reqwest::Client::new();
let res = client
.get(format!("http://{}", addr))
.send()
.await
.unwrap();
let res = client.get(format!("http://{}", addr)).send().await.unwrap();
assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.text().await.unwrap(), "hi from GET");