Skip to content

Commit

Permalink
chore: revert Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 31, 2023
1 parent 1ae5e88 commit b3c6dff
Show file tree
Hide file tree
Showing 42 changed files with 553 additions and 594 deletions.
49 changes: 24 additions & 25 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ members = [
"viz-tower",
"viz-test",

"examples/hello-world",
"examples/unix-socket",
"examples/static-files/embed",
"examples/static-files/serve",
"examples/static-files/include-dir",
"examples/limits",
"examples/forms/form",
"examples/forms/multipart",
"examples/websocket-chat",
"examples/sse",
"examples/session",
"examples/csrf",
"examples/cors",
"examples/rustls",
# "examples/hello-world",
# "examples/unix-socket",
# "examples/static-files/embed",
# "examples/static-files/serve",
# "examples/static-files/include-dir",
# "examples/limits",
# "examples/forms/form",
# "examples/forms/multipart",
# "examples/websocket-chat",
# "examples/sse",
# "examples/session",
# "examples/csrf",
# "examples/cors",
# "examples/rustls",
# "examples/static-routes",
"examples/routing/todos",
"examples/routing/openapi",
"examples/otel/*",
"examples/compression",
"examples/templates/*",
"examples/tracing",
"examples/graceful-shutdown",
"examples/databases/*",
"examples/htmlx",
"examples/tower",
# "examples/routing/todos",
# "examples/routing/openapi",
# "examples/otel/*",
# "examples/compression",
# "examples/templates/*",
# "examples/tracing",
# "examples/graceful-shutdown",
# "examples/databases/*",
# "examples/htmlx",
# "examples/tower",
]

[workspace.package]
Expand Down Expand Up @@ -121,7 +121,6 @@ opt-level = 3
debug = false

[workspace.lints.rust]
unsafe_code = "forbid"
rust_2018_idioms = "warn"
single_use_lifetimes = "warn"
non_ascii_idents = "warn"
Expand Down
2 changes: 1 addition & 1 deletion viz-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ default = [
"websocket",
"cookie",
"session",
"fs"
"fs",
]

state = []
Expand Down
17 changes: 8 additions & 9 deletions viz-core/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Traits and types for handling an HTTP.
use crate::{BoxFuture, Future};
use crate::{async_trait, Future};

mod cloneable;

Expand Down Expand Up @@ -49,9 +49,6 @@ pub use map_into_response::MapInToResponse;
mod or_else;
pub use or_else::OrElse;

mod try_handler;
pub use try_handler::TryHandler;

mod transform;
pub use transform::Transform;

Expand All @@ -61,24 +58,26 @@ pub use service::ServiceHandler;
/// A simplified asynchronous interface for handling input and output.
///
/// Composable request handlers.
pub trait Handler<Input> {
#[async_trait]
pub trait Handler<Input>: Send + Sync + 'static {
/// The returned type after the call operator is used.
type Output;

/// Performs the call operation.
fn call(&self, input: Input) -> BoxFuture<Self::Output>;
async fn call(&self, input: Input) -> Self::Output;
}

#[async_trait]
impl<F, I, Fut, O> Handler<I> for F
where
I: Send + 'static,
F: Fn(I) -> Fut + ?Sized + Clone + Send + 'static,
F: Fn(I) -> Fut + ?Sized + Clone + Send + Sync + 'static,
Fut: Future<Output = O> + Send + 'static,
{
type Output = Fut::Output;

fn call(&self, i: I) -> BoxFuture<Self::Output> {
Box::pin((self)(i))
async fn call(&self, i: I) -> Self::Output {
(self)(i).await
}
}

Expand Down
11 changes: 6 additions & 5 deletions viz-core/src/handler/after.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{future::FutureExt, BoxFuture, Handler, Result};
use crate::{async_trait, Handler, Result};

/// Maps the output `Result<T>` after the handler called.
#[derive(Debug, Clone)]
Expand All @@ -15,16 +15,17 @@ impl<H, F> After<H, F> {
}
}

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

fn call(&self, i: I) -> BoxFuture<Self::Output> {
let f = self.f.clone();
Box::pin(self.h.call(i).then(move |o| f.call(o)))
async fn call(&self, i: I) -> Self::Output {
self.f.call(self.h.call(i).await).await
}
}
13 changes: 7 additions & 6 deletions viz-core/src/handler/and_then.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{future::TryFutureExt, BoxFuture, Handler, Result};
use crate::{async_trait, Handler, Result};

/// Calls `op` if the output is `Ok`, otherwise returns the `Err` value of the output.
#[derive(Debug, Clone)]
Expand All @@ -15,16 +15,17 @@ impl<H, F> AndThen<H, F> {
}
}

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

fn call(&self, i: I) -> BoxFuture<Self::Output> {
let f = self.f.clone();
Box::pin(self.h.call(i).and_then(move |o| f.call(o)))
async fn call(&self, i: I) -> Self::Output {
self.f.call(self.h.call(i).await?).await
}
}
10 changes: 6 additions & 4 deletions viz-core/src/handler/around.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{BoxFuture, Handler, Result};
use crate::{async_trait, Handler, Result};

/// Represents a middleware parameter, which is a tuple that includes Requset and `BoxHandler`.
pub type Next<I, H> = (I, H);
Expand All @@ -18,15 +18,17 @@ impl<H, F> Around<H, F> {
}
}

#[async_trait]
impl<H, F, I, O> Handler<I> for Around<H, F>
where
H: Handler<I, Output = Result<O>> + Clone + 'static,
I: Send + 'static,
H: Handler<I, Output = Result<O>> + Clone,
F: Handler<Next<I, H>, Output = H::Output>,
O: 'static,
{
type Output = F::Output;

fn call(&self, i: I) -> BoxFuture<Self::Output> {
Box::pin(self.f.call((i, self.h.clone())))
async fn call(&self, i: I) -> Self::Output {
self.f.call((i, self.h.clone())).await
}
}
12 changes: 6 additions & 6 deletions viz-core/src/handler/before.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{future::TryFutureExt, BoxFuture, Handler, Result};
use crate::{async_trait, Handler, Result};

/// Maps the input before the handler calls.
#[derive(Debug, Clone)]
Expand All @@ -15,17 +15,17 @@ impl<H, F> Before<H, F> {
}
}

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

fn call(&self, i: I) -> BoxFuture<Self::Output> {
let h = self.h.clone();
Box::pin(self.f.call(i).and_then(move |i| h.call(i)))
async fn call(&self, i: I) -> Self::Output {
self.h.call(self.f.call(i).await?).await
}
}
21 changes: 15 additions & 6 deletions viz-core/src/handler/boxed.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;

use super::cloneable::BoxCloneable;
use crate::{BoxFuture, Handler, Request, Response, Result};
use crate::{async_trait, Handler, Request, Response, Result};

/// A [`Clone`] + [`Send`] boxed [`Handler`].
pub struct BoxHandler<I = Request, O = Result<Response>>(BoxCloneable<I, O>);
Expand All @@ -10,23 +10,32 @@ impl<I, O> BoxHandler<I, O> {
/// Creates a new `BoxHandler`.
pub fn new<H>(h: H) -> Self
where
H: Handler<I, Output = O> + Send + Clone + 'static,
H: Handler<I, Output = O> + Send + Sync + Clone + 'static,
{
Self(Box::new(h))
}
}

impl<I, O> Clone for BoxHandler<I, O> {
impl<I, O> Clone for BoxHandler<I, O>
where
I: 'static,
O: 'static,
{
fn clone(&self) -> Self {
Self(self.0.clone_box())
}
}

impl<I, O> Handler<I> for BoxHandler<I, O> {
#[async_trait]
impl<I, O> Handler<I> for BoxHandler<I, O>
where
I: Send + 'static,
O: 'static,
{
type Output = O;

fn call(&self, i: I) -> BoxFuture<Self::Output> {
self.0.call(i)
async fn call(&self, i: I) -> Self::Output {
self.0.call(i).await
}
}

Expand Down
24 changes: 11 additions & 13 deletions viz-core/src/handler/catch_error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

use crate::{future::TryFutureExt, BoxFuture, Error, Handler, IntoResponse, Response, Result};
use crate::{async_trait, Handler, IntoResponse, Response, Result};

/// Catches rejected error while calling the handler.
#[derive(Debug)]
Expand Down Expand Up @@ -36,24 +36,22 @@ impl<H, F, E, R> CatchError<H, F, E, R> {
}
}

#[async_trait]
impl<H, I, O, F, E, R> Handler<I> for CatchError<H, F, E, R>
where
I: Send + 'static,
H: Handler<I, Output = Result<O>>,
O: IntoResponse + 'static,
O: IntoResponse + Send,
E: std::error::Error + Send + 'static,
F: Handler<E, Output = R> + Send + Clone + 'static,
R: IntoResponse,
F: Handler<E, Output = R>,
R: IntoResponse + 'static,
{
type Output = Result<Response>;

fn call(&self, i: I) -> BoxFuture<Self::Output> {
let f = self.f.clone();
Box::pin(
self.h
.call(i)
.map_ok(IntoResponse::into_response)
.map_err(Error::downcast::<E>)
.or_else(move |r| async move { Ok(f.call(r?).await.into_response()) }),
)
async fn call(&self, i: I) -> Self::Output {
match self.h.call(i).await {
Ok(r) => Ok(r.into_response()),
Err(e) => Ok(self.f.call(e.downcast::<E>()?).await.into_response()),
}
}
}
29 changes: 14 additions & 15 deletions viz-core/src/handler/catch_unwind.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
future::{FutureExt, TryFutureExt},
BoxFuture, Handler, IntoResponse, Response, Result,
};
use crate::{async_trait, future::FutureExt, Handler, IntoResponse, Response, Result};

/// Catches unwinding panics while calling the handler.
#[derive(Debug, Clone)]
Expand All @@ -18,22 +15,24 @@ impl<H, F> CatchUnwind<H, F> {
}
}

#[async_trait]
impl<H, F, I, O, R> Handler<I> for CatchUnwind<H, F>
where
H: Handler<I, Output = Result<O>> + 'static,
O: IntoResponse + 'static,
F: Handler<Box<dyn ::core::any::Any + Send>, Output = R> + Send + Clone + 'static,
I: Send + 'static,
H: Handler<I, Output = Result<O>>,
O: IntoResponse + Send,
F: Handler<Box<dyn ::core::any::Any + Send>, Output = R>,
R: IntoResponse + 'static,
{
type Output = Result<Response>;

fn call(&self, i: I) -> BoxFuture<Self::Output> {
let f = self.f.clone();
Box::pin(
::core::panic::AssertUnwindSafe(self.h.call(i))
.catch_unwind()
.map_ok(IntoResponse::into_response)
.or_else(move |e| f.call(e).map(IntoResponse::into_response).map(Result::Ok)),
)
async fn call(&self, i: I) -> Self::Output {
match ::core::panic::AssertUnwindSafe(self.h.call(i))
.catch_unwind()
.await
{
Ok(r) => r.map(IntoResponse::into_response),
Err(e) => Ok(self.f.call(e).await.into_response()),
}
}
}
5 changes: 3 additions & 2 deletions viz-core/src/handler/cloneable.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use super::Handler;

pub(crate) type BoxCloneable<Input, Output> = Box<dyn Cloneable<Input, Output = Output> + Send>;
pub(crate) type BoxCloneable<Input, Output> =
Box<dyn Cloneable<Input, Output = Output> + Send + Sync + 'static>;

pub(crate) trait Cloneable<Input>: Handler<Input> {
fn clone_box(&self) -> BoxCloneable<Input, Self::Output>;
}

impl<Input, T> Cloneable<Input> for T
where
T: Handler<Input> + Send + Clone + 'static,
T: Handler<Input> + Send + Sync + Clone + 'static,
{
fn clone_box(&self) -> BoxCloneable<Input, Self::Output> {
Box::new(self.clone())
Expand Down
Loading

0 comments on commit b3c6dff

Please sign in to comment.