Some simplifications

This commit is contained in:
David Pedersen 2021-06-01 08:32:58 +02:00
parent 8582c6fd1f
commit 093ad3622e
4 changed files with 7 additions and 19 deletions

View File

@ -30,8 +30,6 @@ pub trait Handler<B, In>: Sized {
fn layer<L>(self, layer: L) -> Layered<L::Service, In>
where
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)))
}
@ -40,7 +38,7 @@ pub trait Handler<B, In>: Sized {
#[async_trait]
impl<F, Fut, B, Res> Handler<B, ()> for F
where
F: Fn(Request<Body>) -> Fut + Send + Sync,
F: FnOnce(Request<Body>) -> Fut + Send + Sync,
Fut: Future<Output = Res> + Send,
Res: IntoResponse<B>,
{
@ -61,7 +59,7 @@ macro_rules! impl_handler {
#[allow(non_snake_case)]
impl<F, Fut, B, Res, $head, $($tail,)*> Handler<B, ($head, $($tail,)*)> for F
where
F: Fn(Request<Body>, $head, $($tail,)*) -> Fut + Send + Sync,
F: FnOnce(Request<Body>, $head, $($tail,)*) -> Fut + Send + Sync,
Fut: Future<Output = Res> + Send,
Res: IntoResponse<B>,
$head: FromRequest<B> + Send,

View File

@ -1,9 +1,9 @@
use crate::Body;
use bytes::Bytes;
use http::{HeaderMap, HeaderValue, Response, StatusCode, header};
use http::{header, HeaderMap, HeaderValue, Response, StatusCode};
use serde::Serialize;
use std::convert::Infallible;
use tower::{util::Either, BoxError};
use tower::util::Either;
pub trait IntoResponse<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
where
B: Default,

View File

@ -174,7 +174,7 @@ impl<R> RouteBuilder<R> {
R: Service<Request<Body>, Response = Response<B>, Error = Infallible> + Send + 'static,
R::Future: Send,
// TODO(david): do we still need default here
B: Default + From<String> + 'static,
B: From<String> + 'static,
{
let svc = ServiceBuilder::new()
.layer(BufferLayer::new(1024))

View File

@ -275,6 +275,8 @@ async fn boxing() {
// 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.
pub async fn run_in_background<S, ResBody>(svc: S) -> SocketAddr
where