mirror of
https://github.com/tokio-rs/axum.git
synced 2024-12-29 15:49:16 +01:00
Don't force handlers to return Result
s
This commit is contained in:
parent
6f5b2708d5
commit
d33be9683c
6 changed files with 26 additions and 21 deletions
|
@ -3,7 +3,7 @@ use hyper::Server;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use tower::{make::Shared, ServiceBuilder};
|
use tower::{make::Shared, ServiceBuilder};
|
||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
use tower_web::{body::Body, response::Html, Error};
|
use tower_web::{body::Body, response::Html};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
@ -28,6 +28,6 @@ async fn main() {
|
||||||
server.await.unwrap();
|
server.await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handler(_req: Request<Body>) -> Result<Html<&'static str>, Error> {
|
async fn handler(_req: Request<Body>) -> Html<&'static str> {
|
||||||
Ok(Html("<h1>Hello, World!</h1>"))
|
Html("<h1>Hello, World!</h1>")
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,7 +72,7 @@ async fn set(
|
||||||
params: extract::UrlParams<(String,)>,
|
params: extract::UrlParams<(String,)>,
|
||||||
value: extract::BytesMaxLength<{ 1024 * 5_000 }>, // ~5mb
|
value: extract::BytesMaxLength<{ 1024 * 5_000 }>, // ~5mb
|
||||||
state: extract::Extension<SharedState>,
|
state: extract::Extension<SharedState>,
|
||||||
) -> Result<response::Empty, Error> {
|
) -> response::Empty {
|
||||||
let state = state.into_inner();
|
let state = state.into_inner();
|
||||||
let db = &mut state.lock().unwrap().db;
|
let db = &mut state.lock().unwrap().db;
|
||||||
|
|
||||||
|
@ -81,5 +81,5 @@ async fn set(
|
||||||
|
|
||||||
db.insert(key.to_string(), value);
|
db.insert(key.to_string(), value);
|
||||||
|
|
||||||
Ok(response::Empty)
|
response::Empty
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use http::Request;
|
||||||
use hyper::Server;
|
use hyper::Server;
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use tower::make::Shared;
|
use tower::make::Shared;
|
||||||
use tower_web::{body::Body, Error};
|
use tower_web::body::Body;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
@ -227,6 +227,6 @@ async fn main() {
|
||||||
server.await.unwrap();
|
server.await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handler(_req: Request<Body>) -> Result<&'static str, Error> {
|
async fn handler(_req: Request<Body>) -> &'static str {
|
||||||
Ok("Hello, World!")
|
"Hello, World!"
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ pub trait Handler<B, In>: Sized {
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
type Sealed: sealed::HiddentTrait;
|
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>
|
fn layer<L>(self, layer: L) -> Layered<L::Service, In>
|
||||||
where
|
where
|
||||||
|
@ -38,15 +38,15 @@ pub trait Handler<B, In>: Sized {
|
||||||
impl<F, Fut, B, Res> Handler<B, ()> for F
|
impl<F, Fut, B, Res> Handler<B, ()> for F
|
||||||
where
|
where
|
||||||
F: Fn(Request<Body>) -> Fut + Send + Sync,
|
F: Fn(Request<Body>) -> Fut + Send + Sync,
|
||||||
Fut: Future<Output = Result<Res, Error>> + Send,
|
Fut: Future<Output = Res> + Send,
|
||||||
Res: IntoResponse<B>,
|
Res: IntoResponse<B>,
|
||||||
{
|
{
|
||||||
type Response = Res;
|
type Response = Res;
|
||||||
|
|
||||||
type Sealed = sealed::Hidden;
|
type Sealed = sealed::Hidden;
|
||||||
|
|
||||||
async fn call(self, req: Request<Body>) -> Result<Self::Response, Error> {
|
async fn call(self, req: Request<Body>) -> Result<Response<B>, Error> {
|
||||||
self(req).await
|
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
|
impl<F, Fut, B, Res, $head, $($tail,)*> Handler<B, ($head, $($tail,)*)> for F
|
||||||
where
|
where
|
||||||
F: Fn(Request<Body>, $head, $($tail,)*) -> Fut + Send + Sync,
|
F: Fn(Request<Body>, $head, $($tail,)*) -> Fut + Send + Sync,
|
||||||
Fut: Future<Output = Result<Res, Error>> + Send,
|
Fut: Future<Output = Res> + Send,
|
||||||
Res: IntoResponse<B>,
|
Res: IntoResponse<B>,
|
||||||
$head: FromRequest + Send,
|
$head: FromRequest + Send,
|
||||||
$( $tail: FromRequest + Send, )*
|
$( $tail: FromRequest + Send, )*
|
||||||
|
@ -68,13 +68,13 @@ macro_rules! impl_handler {
|
||||||
|
|
||||||
type Sealed = sealed::Hidden;
|
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 $head = $head::from_request(&mut req).await?;
|
||||||
$(
|
$(
|
||||||
let $tail = $tail::from_request(&mut req).await?;
|
let $tail = $tail::from_request(&mut req).await?;
|
||||||
)*
|
)*
|
||||||
let res = self(req, $head, $($tail,)*).await?;
|
let res = self(req, $head, $($tail,)*).await;
|
||||||
Ok(res)
|
res.into_response()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,15 @@ pub trait IntoResponse<B> {
|
||||||
fn into_response(self) -> Result<Response<B>, Error>;
|
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> {
|
impl<B> IntoResponse<B> for Response<B> {
|
||||||
fn into_response(self) -> Result<Response<B>, Error> {
|
fn into_response(self) -> Result<Response<B>, Error> {
|
||||||
Ok(self)
|
Ok(self)
|
||||||
|
|
|
@ -263,11 +263,7 @@ async fn boxing() {
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
let res = client
|
let res = client.get(format!("http://{}", addr)).send().await.unwrap();
|
||||||
.get(format!("http://{}", addr))
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
assert_eq!(res.status(), StatusCode::OK);
|
assert_eq!(res.status(), StatusCode::OK);
|
||||||
assert_eq!(res.text().await.unwrap(), "hi from GET");
|
assert_eq!(res.text().await.unwrap(), "hi from GET");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue