Skip to content

Commit

Permalink
chore(core): remove Sync from CatchError
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 16, 2023
1 parent 6c8bd04 commit 3b5596b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
13 changes: 4 additions & 9 deletions viz-core/src/handler/catch_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{async_trait, Handler, IntoResponse, Response, Result};
pub struct CatchError<H, F, R, E> {
h: H,
f: F,
_maker: PhantomData<(R, E)>,
_maker: PhantomData<fn(E) -> R>,
}

impl<H, F, R, E> Clone for CatchError<H, F, R, E>
Expand Down Expand Up @@ -41,21 +41,16 @@ where
I: Send + 'static,
H: Handler<I, Output = Result<O>> + Clone,
O: IntoResponse + Send,
R: IntoResponse + Send + Sync + 'static,
E: std::error::Error + Send + 'static,
F: Handler<E, Output = R> + Clone,
E: std::error::Error + Send + Sync + 'static,
R: IntoResponse + 'static,
{
type Output = Result<Response>;

async fn call(&self, i: I) -> Self::Output {
match self.h.call(i).await {
Ok(r) => Ok(r.into_response()),
Err(e) if e.is::<E>() => Ok(self
.f
.call(e.downcast::<E>().unwrap())
.await
.into_response()),
Err(e) => Err(e),
Err(e) => Ok(self.f.call(e.downcast::<E>()?).await.into_response()),
}
}
}
22 changes: 7 additions & 15 deletions viz-core/tests/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,15 @@ async fn handler() -> Result<()> {
struct CatchError<H, F, R, E> {
h: H,
f: F,
r: PhantomData<R>,
e: PhantomData<E>,
_maker: PhantomData<fn(E) -> R>,
}

impl<H: Clone, F: Clone, R, E> Clone for CatchError<H, F, R, E> {
fn clone(&self) -> Self {
Self {
h: self.h.clone(),
f: self.f.clone(),
r: PhantomData,
e: PhantomData,
_maker: PhantomData,
}
}
}
Expand All @@ -33,8 +31,7 @@ async fn handler() -> Result<()> {
Self {
h,
f,
r: PhantomData,
e: PhantomData,
_maker: PhantomData,
}
}
}
Expand All @@ -43,23 +40,18 @@ async fn handler() -> Result<()> {
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,
O: IntoResponse + Send,
E: std::error::Error + Send + 'static,
F: Handler<E, Output = R> + Clone,
R: IntoResponse + Send + Sync + 'static,
E: std::error::Error + Send + Sync + 'static,
R: IntoResponse + 'static,
{
type Output = Result<Response>;

async fn call(&self, i: I) -> Self::Output {
match self.h.call(i).await {
Ok(r) => Ok(r.into_response()),
Err(e) if e.is::<E>() => Ok(self
.f
.call(e.downcast::<E>().unwrap())
.await
.into_response()),
Err(e) => Err(e),
Err(e) => Ok(self.f.call(e.downcast::<E>()?).await.into_response()),
}
}
}
Expand Down

0 comments on commit 3b5596b

Please sign in to comment.