diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 4b169700..b6b57b19 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -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) -> Result, Error> { - Ok(Html("

Hello, World!

")) +async fn handler(_req: Request) -> Html<&'static str> { + Html("

Hello, World!

") } diff --git a/examples/key_value_store.rs b/examples/key_value_store.rs index 3bcfc3e3..02b5658c 100644 --- a/examples/key_value_store.rs +++ b/examples/key_value_store.rs @@ -72,7 +72,7 @@ async fn set( params: extract::UrlParams<(String,)>, value: extract::BytesMaxLength<{ 1024 * 5_000 }>, // ~5mb state: extract::Extension, -) -> Result { +) -> 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 } diff --git a/examples/lots_of_routes.rs b/examples/lots_of_routes.rs index 96b8ad71..dac28f85 100644 --- a/examples/lots_of_routes.rs +++ b/examples/lots_of_routes.rs @@ -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) -> Result<&'static str, Error> { - Ok("Hello, World!") +async fn handler(_req: Request) -> &'static str { + "Hello, World!" } diff --git a/src/handler.rs b/src/handler.rs index 7c1787e7..24b24405 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -24,7 +24,7 @@ pub trait Handler: Sized { #[doc(hidden)] type Sealed: sealed::HiddentTrait; - async fn call(self, req: Request) -> Result; + async fn call(self, req: Request) -> Result, Error>; fn layer(self, layer: L) -> Layered where @@ -38,15 +38,15 @@ pub trait Handler: Sized { impl Handler for F where F: Fn(Request) -> Fut + Send + Sync, - Fut: Future> + Send, + Fut: Future + Send, Res: IntoResponse, { type Response = Res; type Sealed = sealed::Hidden; - async fn call(self, req: Request) -> Result { - self(req).await + async fn call(self, req: Request) -> Result, Error> { + self(req).await.into_response() } } @@ -59,7 +59,7 @@ macro_rules! impl_handler { impl Handler for F where F: Fn(Request, $head, $($tail,)*) -> Fut + Send + Sync, - Fut: Future> + Send, + Fut: Future + Send, Res: IntoResponse, $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) -> Result { + async fn call(self, mut req: Request) -> Result, 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() } } diff --git a/src/response.rs b/src/response.rs index a0782384..f15ae99f 100644 --- a/src/response.rs +++ b/src/response.rs @@ -7,6 +7,15 @@ pub trait IntoResponse { fn into_response(self) -> Result, Error>; } +impl IntoResponse for Result +where + T: IntoResponse, +{ + fn into_response(self) -> Result, Error> { + self.and_then(IntoResponse::into_response) + } +} + impl IntoResponse for Response { fn into_response(self) -> Result, Error> { Ok(self) diff --git a/src/tests.rs b/src/tests.rs index c6b371a1..55317f46 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -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");