mirror of
https://github.com/tokio-rs/axum.git
synced 2024-12-28 23:38:20 +01:00
Some simplifications
This commit is contained in:
parent
8582c6fd1f
commit
093ad3622e
4 changed files with 7 additions and 19 deletions
|
@ -30,8 +30,6 @@ pub trait Handler<B, In>: Sized {
|
||||||
fn layer<L>(self, layer: L) -> Layered<L::Service, In>
|
fn layer<L>(self, layer: L) -> Layered<L::Service, In>
|
||||||
where
|
where
|
||||||
L: Layer<HandlerSvc<Self, B, In>>,
|
L: Layer<HandlerSvc<Self, B, In>>,
|
||||||
<L as Layer<HandlerSvc<Self, B, In>>>::Service: Service<Request<Body>>,
|
|
||||||
<<L as Layer<HandlerSvc<Self, B, In>>>::Service as Service<Request<Body>>>::Error: IntoResponse<B>,
|
|
||||||
{
|
{
|
||||||
Layered::new(layer.layer(HandlerSvc::new(self)))
|
Layered::new(layer.layer(HandlerSvc::new(self)))
|
||||||
}
|
}
|
||||||
|
@ -40,7 +38,7 @@ pub trait Handler<B, In>: Sized {
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
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: FnOnce(Request<Body>) -> Fut + Send + Sync,
|
||||||
Fut: Future<Output = Res> + Send,
|
Fut: Future<Output = Res> + Send,
|
||||||
Res: IntoResponse<B>,
|
Res: IntoResponse<B>,
|
||||||
{
|
{
|
||||||
|
@ -61,7 +59,7 @@ macro_rules! impl_handler {
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
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: FnOnce(Request<Body>, $head, $($tail,)*) -> Fut + Send + Sync,
|
||||||
Fut: Future<Output = Res> + Send,
|
Fut: Future<Output = Res> + Send,
|
||||||
Res: IntoResponse<B>,
|
Res: IntoResponse<B>,
|
||||||
$head: FromRequest<B> + Send,
|
$head: FromRequest<B> + Send,
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use crate::Body;
|
use crate::Body;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use http::{HeaderMap, HeaderValue, Response, StatusCode, header};
|
use http::{header, HeaderMap, HeaderValue, Response, StatusCode};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
use tower::{util::Either, BoxError};
|
use tower::util::Either;
|
||||||
|
|
||||||
pub trait IntoResponse<B> {
|
pub trait IntoResponse<B> {
|
||||||
fn into_response(self) -> Response<B>;
|
fn into_response(self) -> Response<B>;
|
||||||
|
@ -174,18 +174,6 @@ impl<B> IntoResponse<B> for BoxIntoResponse<B> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoResponse<Body> for BoxError {
|
|
||||||
fn into_response(self) -> Response<Body> {
|
|
||||||
// TODO(david): test for know error types like std::io::Error
|
|
||||||
// or common errors types from tower and map those more appropriately
|
|
||||||
|
|
||||||
Response::builder()
|
|
||||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
|
||||||
.body(Body::from(self.to_string()))
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<B> IntoResponse<B> for StatusCode
|
impl<B> IntoResponse<B> for StatusCode
|
||||||
where
|
where
|
||||||
B: Default,
|
B: Default,
|
||||||
|
|
|
@ -174,7 +174,7 @@ impl<R> RouteBuilder<R> {
|
||||||
R: Service<Request<Body>, Response = Response<B>, Error = Infallible> + Send + 'static,
|
R: Service<Request<Body>, Response = Response<B>, Error = Infallible> + Send + 'static,
|
||||||
R::Future: Send,
|
R::Future: Send,
|
||||||
// TODO(david): do we still need default here
|
// TODO(david): do we still need default here
|
||||||
B: Default + From<String> + 'static,
|
B: From<String> + 'static,
|
||||||
{
|
{
|
||||||
let svc = ServiceBuilder::new()
|
let svc = ServiceBuilder::new()
|
||||||
.layer(BufferLayer::new(1024))
|
.layer(BufferLayer::new(1024))
|
||||||
|
|
|
@ -275,6 +275,8 @@ async fn boxing() {
|
||||||
|
|
||||||
// TODO(david): tests for adding middleware to single services
|
// TODO(david): tests for adding middleware to single services
|
||||||
|
|
||||||
|
// TODO(david): tests for nesting services
|
||||||
|
|
||||||
/// Run a `tower::Service` in the background and get a URI for it.
|
/// Run a `tower::Service` in the background and get a URI for it.
|
||||||
pub async fn run_in_background<S, ResBody>(svc: S) -> SocketAddr
|
pub async fn run_in_background<S, ResBody>(svc: S) -> SocketAddr
|
||||||
where
|
where
|
||||||
|
|
Loading…
Reference in a new issue