Skip to content

Commit

Permalink
chore: opt handler structs
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 13, 2023
1 parent 7286e3c commit 8dcbe6a
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 49 deletions.
2 changes: 1 addition & 1 deletion viz-core/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub trait HandlerExt<I>: Handler<I> {
}

/// Maps the handler's output type to the [`Response`][crate::Response].
fn map_into_response<O>(self) -> MapInToResponse<Self, O>
fn map_into_response(self) -> MapInToResponse<Self>
where
Self: Sized,
{
Expand Down
1 change: 0 additions & 1 deletion viz-core/src/handler/after.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ impl<H, F> After<H, F> {
impl<H, F, I, O> Handler<I> for After<H, F>
where
I: Send + 'static,
O: Send,
H: Handler<I, Output = Result<O>> + Clone,
F: Handler<Result<O>, Output = Result<O>> + Clone,
{
Expand Down
2 changes: 1 addition & 1 deletion viz-core/src/handler/and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ impl<H, F> AndThen<H, F> {
impl<H, F, I, O> Handler<I> for AndThen<H, F>
where
I: Send + 'static,
O: Send,
H: Handler<I, Output = Result<O>> + Clone,
O: Send,
F: Handler<O, Output = H::Output> + Clone,
{
type Output = F::Output;
Expand Down
4 changes: 2 additions & 2 deletions viz-core/src/handler/catch_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ impl<H, F, R, E> CatchError<H, F, R, E> {
impl<H, F, I, O, R, E> Handler<I> for CatchError<H, F, R, E>
where
I: Send + 'static,
O: IntoResponse + Send,
H: Handler<I, Output = Result<O>> + Clone,
F: Handler<E, Output = R> + Clone,
O: IntoResponse + Send,
R: IntoResponse + Send + Sync + 'static,
F: Handler<E, Output = R> + Clone,
E: std::error::Error + Send + Sync + 'static,
{
type Output = Result<Response>;
Expand Down
4 changes: 2 additions & 2 deletions viz-core/src/handler/catch_unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ impl<H, F> CatchUnwind<H, F> {
impl<H, F, I, O, R> Handler<I> for CatchUnwind<H, F>
where
I: Send + 'static,
O: IntoResponse + Send,
H: Handler<I, Output = Result<O>> + Clone,
F: Handler<Box<dyn Any + Send>, Output = R> + Clone,
O: IntoResponse + Send + Sync + 'static,
R: IntoResponse + Send + Sync + 'static,
R: IntoResponse,
{
type Output = Result<Response>;

Expand Down
14 changes: 4 additions & 10 deletions viz-core/src/handler/fn_ext_hanlder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,14 @@ impl<H, E, O> FnExtHandler<H, E, O> {
#[async_trait]
impl<H, E, O> Handler<Request> for FnExtHandler<H, E, O>
where
E: FromRequest + Send + Sync + 'static,
E::Error: IntoResponse + Send + Sync,
E: FromRequest + 'static,
E::Error: IntoResponse + Send,
H: FnExt<E, Output = Result<O>>,
O: Send + Sync + 'static,
// O: IntoResponse + Send + Sync + 'static,
O: 'static,
{
type Output = H::Output;
// type Output = Result<Response>;

async fn call(&self, req: Request) -> Self::Output {
self.0
.call(req)
.await
// .map(IntoResponse::into_response)
.map_err(IntoResponse::into_error)
self.0.call(req).await.map_err(IntoResponse::into_error)
}
}
6 changes: 3 additions & 3 deletions viz-core/src/handler/into_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ pub trait IntoHandler<E, I> {

impl<H, E, O> IntoHandler<E, Request> for H
where
E: FromRequest + Send + Sync + 'static,
E::Error: IntoResponse + Send + Sync,
E: FromRequest + 'static,
E::Error: IntoResponse + Send,
H: FnExt<E, Output = Result<O>>,
O: Send + Sync + 'static,
O: 'static,
{
type Handler = FnExtHandler<H, E, O>;

Expand Down
2 changes: 1 addition & 1 deletion viz-core/src/handler/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ impl<H, F> Map<H, F> {
impl<H, F, I, O> Handler<I> for Map<H, F>
where
I: Send + 'static,
O: Send,
H: Handler<I, Output = Result<O>> + Clone,
O: Send,
F: Handler<O, Output = O> + Clone,
{
type Output = H::Output;
Expand Down
2 changes: 1 addition & 1 deletion viz-core/src/handler/map_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ impl<H, F> MapErr<H, F> {
impl<H, F, I, O> Handler<I> for MapErr<H, F>
where
I: Send + 'static,
O: Send,
H: Handler<I, Output = Result<O>> + Clone,
O: Send,
F: Handler<Error, Output = Error> + Clone,
{
type Output = H::Output;
Expand Down
28 changes: 9 additions & 19 deletions viz-core/src/handler/map_into_response.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
use std::marker::PhantomData;

use crate::{async_trait, Handler, IntoResponse, Response, Result};

/// Maps the handler's output type to the [`Response`].
#[derive(Debug)]
pub struct MapInToResponse<H, O>(pub(crate) H, PhantomData<O>);
#[derive(Debug, Clone)]
pub struct MapInToResponse<H>(pub(crate) H);

impl<H, O> MapInToResponse<H, O> {
impl<H> MapInToResponse<H> {
/// Creates a new `Responder`.
#[inline]
pub(super) fn new(h: H) -> Self {
Self(h, PhantomData)
}
}

impl<H, O> Clone for MapInToResponse<H, O>
where
H: Clone,
{
fn clone(&self) -> Self {
Self(self.0.clone(), PhantomData)
Self(h)
}
}

#[async_trait]
impl<H, I, O> Handler<I> for MapInToResponse<H, O>
impl<H, I, O> Handler<I> for MapInToResponse<H>
where
I: Send + 'static,
H: Handler<I, Output = Result<O>> + Clone,
O: IntoResponse + Send + Sync + 'static,
O: IntoResponse + Send,
{
type Output = Result<Response>;

async fn call(&self, args: I) -> Self::Output {
self.0.call(args).await.map(IntoResponse::into_response)
async fn call(&self, i: I) -> Self::Output {
self.0.call(i).await.map(IntoResponse::into_response)
}
}
2 changes: 1 addition & 1 deletion viz-core/src/handler/or_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ impl<H, F> OrElse<H, F> {
impl<H, F, I, O> Handler<I> for OrElse<H, F>
where
I: Send + 'static,
O: Send,
H: Handler<I, Output = Result<O>> + Clone,
O: Send,
F: Handler<Error, Output = H::Output> + Clone,
{
type Output = F::Output;
Expand Down
8 changes: 2 additions & 6 deletions viz-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! tuple_impls {
#[async_trait]
impl<$($T,)*> FromRequest for ($($T,)*)
where
$($T: FromRequest + Send + 'static,)*
$($T: FromRequest + Send,)*
$($T::Error: IntoResponse + Send,)*
{
type Error = Error;
Expand All @@ -25,21 +25,17 @@ macro_rules! tuple_impls {
#[async_trait]
impl<$($T,)* Fun, Fut, Out> FnExt<($($T,)*)> for Fun
where
$($T: FromRequest + Send + 'static,)*
$($T: FromRequest + Send,)*
$($T::Error: IntoResponse + Send,)*
Fun: Fn($($T,)*) -> Fut + Clone + Send + Sync + 'static,
Fut: Future<Output = Result<Out>> + Send,
Out: Send + Sync + 'static,
// Out: IntoResponse + Send + Sync + 'static,
{
type Output = Fut::Output;
// type Output = Result<Response>;

#[allow(unused, unused_mut)]
async fn call(&self, mut req: Request) -> Self::Output {
(self)($($T::extract(&mut req).await.map_err(IntoResponse::into_error)?,)*)
.await
// .await.map(IntoResponse::into_response)
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion viz-core/tests/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use viz_core::*;

#[tokio::test]
async fn handler() -> Result<()> {
pub struct CatchError<H, F, R, E> {
struct CatchError<H, F, R, E> {
h: H,
f: F,
r: PhantomData<R>,
Expand Down

0 comments on commit 8dcbe6a

Please sign in to comment.