Skip to content

Commit

Permalink
Merge pull request #244 from H1rono/with-state
Browse files Browse the repository at this point in the history
`WithState`まわり修正
  • Loading branch information
H1rono authored Dec 8, 2024
2 parents c0c16ba + b5de3d0 commit 4725533
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 47 deletions.
49 changes: 10 additions & 39 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,6 @@ impl<T> Service<T> for Sink {
}
}

/// [`Event`]と`State`の直積です。
///
/// [`Event`]: crate::Event
#[must_use]
#[derive(Debug, Clone)]
pub struct EventWithState<State> {
state: State,
event: Event,
}

impl<State> From<(State, Event)> for EventWithState<State> {
fn from((state, event): (State, Event)) -> Self {
Self { state, event }
}
}

impl<State> From<EventWithState<State>> for (State, Event) {
fn from(value: EventWithState<State>) -> Self {
let EventWithState { state, event } = value;
(state, event)
}
}

/// 内部の`Service`に`State`を渡す[`Service`]です。
///
/// `WithState::call`の度に`State`がcloneされるため、`State`は[`Clone`]を実装する必要があります。
Expand All @@ -92,44 +69,38 @@ pub struct WithState<State, Service> {

impl<State, Srv> Service<Event> for WithState<State, Srv>
where
Srv: Service<EventWithState<State>>,
Srv: Service<(State, Event)>,
State: Clone,
{
type Response = Srv::Response;
type Error = Srv::Error;
type Future = Srv::Future;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}

fn call(&mut self, request: Event) -> Self::Future {
let request = EventWithState {
state: self.state.clone(),
event: request,
};
let request = (self.state.clone(), request);
self.service.call(request)
}
}

impl<OState, State, Srv> Service<EventWithState<OState>> for WithState<State, Srv>
impl<OState, State, Srv> Service<(OState, Event)> for WithState<State, Srv>
where
Srv: Service<EventWithState<State>>,
Srv: Service<(State, Event)>,
State: Clone,
{
type Response = Srv::Response;
type Error = Srv::Error;
type Future = Srv::Future;

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}

fn call(&mut self, request: EventWithState<OState>) -> Self::Future {
let request = EventWithState {
state: self.state.clone(),
event: request.event,
};
fn call(&mut self, (_, request): (OState, Event)) -> Self::Future {
let request = (self.state.clone(), request);
self.service.call(request)
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ macro_rules! event_service_call {
$s:ident;
$v:ident ( $i:ident ) => $e:expr
) => {
fn call(&mut self, req: $crate::handler::EventWithState<State>) -> Self::Future {
let ($s, event) = req.into();
fn call(&mut self, req: (State, $crate::handler::Event)) -> Self::Future {
let ($s, event) = req;
match event {
$crate::Event::$v($i) => ::futures::future::Either::Left(
$crate::handler::WrapErrorFuture::new(self.inner.call($e)),
Expand Down Expand Up @@ -472,14 +472,14 @@ macro_rules! event_service {
$crate::macros::event_service_call! { [< $e:camel >] (e) }
}

impl<State, Service, Fallback> ::tower::Service<$crate::handler::EventWithState<State>>
impl<State, Service, Fallback> ::tower::Service<(State, $crate::handler::Event)>
for [< $e:camel Service >] <Service, Fallback, $crate::payloads::[< $e:camel Payload >] >
where
Service: ::tower::Service<$crate::payloads::[< $e:camel Payload >], Response = ()>,
Service::Error: ::std::convert::Into<::std::boxed::Box<
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
>>,
Fallback: ::tower::Service<$crate::handler::EventWithState<State>, Response = ()>,
Fallback: ::tower::Service<(State, $crate::handler::Event), Response = ()>,
Fallback::Error: ::std::convert::Into<::std::boxed::Box<
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
>>,
Expand All @@ -489,7 +489,7 @@ macro_rules! event_service {
$crate::macros::event_service_call! { state; [< $e:camel >] (e) => e }
}

impl<State, Service, Fallback> ::tower::Service<$crate::handler::EventWithState<State>>
impl<State, Service, Fallback> ::tower::Service<(State, $crate::handler::Event)>
for [< $e:camel Service >] <Service, Fallback, ($crate::payloads::[< $e:camel Payload >],) >
where
Service: ::tower::Service<
Expand All @@ -499,7 +499,7 @@ macro_rules! event_service {
Service::Error: ::std::convert::Into<::std::boxed::Box<
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
>>,
Fallback: ::tower::Service<$crate::handler::EventWithState<State>, Response = ()>,
Fallback: ::tower::Service<(State, $crate::handler::Event), Response = ()>,
Fallback::Error: ::std::convert::Into<::std::boxed::Box<
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
>>,
Expand All @@ -509,7 +509,7 @@ macro_rules! event_service {
$crate::macros::event_service_call! { state; [< $e:camel >] (e) => (e,) }
}

impl<State, Service, Fallback> ::tower::Service<$crate::handler::EventWithState<State>>
impl<State, Service, Fallback> ::tower::Service<(State, $crate::handler::Event)>
for [< $e:camel Service >] <Service, Fallback, (State, $crate::payloads::[< $e:camel Payload >] )>
where
Service: ::tower::Service<
Expand All @@ -519,7 +519,7 @@ macro_rules! event_service {
Service::Error: ::std::convert::Into<::std::boxed::Box<
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
>>,
Fallback: ::tower::Service<$crate::handler::EventWithState<State>, Response = ()>,
Fallback: ::tower::Service<(State, $crate::handler::Event), Response = ()>,
Fallback::Error: ::std::convert::Into<::std::boxed::Box<
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
>>,
Expand Down

0 comments on commit 4725533

Please sign in to comment.