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

View file

@ -3,7 +3,7 @@ use hyper::Server;
use std::net::SocketAddr;
use tower::{make::Shared, ServiceBuilder};
use tower_http::trace::TraceLayer;
use tower_web::{body::Body, response::Html, Error};
use tower_web::{body::Body, response::Html};
#[tokio::main]
async fn main() {
@ -28,6 +28,6 @@ async fn main() {
server.await.unwrap();
}
async fn handler(_req: Request<Body>) -> Result<Html<&'static str>, Error> {
Ok(Html("<h1>Hello, World!</h1>"))
async fn handler(_req: Request<Body>) -> Html<&'static str> {
Html("<h1>Hello, World!</h1>")
}

View file

@ -72,7 +72,7 @@ async fn set(
params: extract::UrlParams<(String,)>,
value: extract::BytesMaxLength<{ 1024 * 5_000 }>, // ~5mb
state: extract::Extension<SharedState>,
) -> Result<response::Empty, Error> {
) -> response::Empty {
let state = state.into_inner();
let db = &mut state.lock().unwrap().db;
@ -81,5 +81,5 @@ async fn set(
db.insert(key.to_string(), value);
Ok(response::Empty)
response::Empty
}

View file

@ -2,7 +2,7 @@ use http::Request;
use hyper::Server;
use std::net::SocketAddr;
use tower::make::Shared;
use tower_web::{body::Body, Error};
use tower_web::body::Body;
#[tokio::main]
async fn main() {
@ -227,6 +227,6 @@ async fn main() {
server.await.unwrap();
}
async fn handler(_req: Request<Body>) -> Result<&'static str, Error> {
Ok("Hello, World!")
async fn handler(_req: Request<Body>) -> &'static str {
"Hello, World!"
}

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()
}
}

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)

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");