Skip to content

Commit

Permalink
chore(core): tweak State
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 14, 2023
1 parent 28b3c25 commit aa60ad6
Showing 1 changed file with 7 additions and 35 deletions.
42 changes: 7 additions & 35 deletions viz-core/src/types/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use std::{
any::type_name,
fmt,
ops::{Deref, DerefMut},
};

Expand All @@ -12,6 +11,7 @@ use crate::{
};

/// Extracts state from the extensions of a request.
#[derive(Debug, Clone, Copy, Default)]
pub struct State<T: ?Sized>(pub T);

impl<T> State<T> {
Expand All @@ -35,24 +35,6 @@ impl<T> AsRef<T> for State<T> {
}
}

impl<T> Clone for State<T>
where
T: Clone,
{
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

impl<T> Default for State<T>
where
T: Default,
{
fn default() -> Self {
Self(T::default())
}
}

impl<T> Deref for State<T> {
type Target = T;

Expand All @@ -67,15 +49,6 @@ impl<T> DerefMut for State<T> {
}
}

impl<T> fmt::Debug for State<T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
T::fmt(self, f)
}
}

#[async_trait]
impl<T> FromRequest for State<T>
where
Expand All @@ -92,25 +65,24 @@ impl<H, T> Transform<H> for State<T>
where
T: Clone + Send + Sync + 'static,
{
type Output = State<(H, T)>;
type Output = State<(T, H)>;

fn transform(&self, h: H) -> Self::Output {
State((h, self.0.clone()))
State((self.0.clone(), h))
}
}

// TODO: Maybe should be a `before` handler
#[async_trait]
impl<H, O, T> Handler<Request> for State<(H, T)>
impl<T, H, O> Handler<Request> for State<(T, H)>
where
O: IntoResponse,
H: Handler<Request, Output = Result<O>> + Clone,
T: Clone + Send + Sync + 'static,
H: Handler<Request, Output = Result<O>> + Clone,
O: IntoResponse,
{
type Output = Result<Response>;

async fn call(&self, mut req: Request) -> Self::Output {
let Self((h, t)) = self;
let Self((t, h)) = self;
req.extensions_mut().insert(t.clone());
h.call(req).await.map(IntoResponse::into_response)
}
Expand Down

0 comments on commit aa60ad6

Please sign in to comment.