diff --git a/src/handler.rs b/src/handler.rs index 4e75a10..16bc12b 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -54,29 +54,6 @@ impl Service for Sink { } } -/// [`Event`]と`State`の直積です。 -/// -/// [`Event`]: crate::Event -#[must_use] -#[derive(Debug, Clone)] -pub struct EventWithState { - state: State, - event: Event, -} - -impl From<(State, Event)> for EventWithState { - fn from((state, event): (State, Event)) -> Self { - Self { state, event } - } -} - -impl From> for (State, Event) { - fn from(value: EventWithState) -> Self { - let EventWithState { state, event } = value; - (state, event) - } -} - /// 内部の`Service`に`State`を渡す[`Service`]です。 /// /// `WithState::call`の度に`State`がcloneされるため、`State`は[`Clone`]を実装する必要があります。 @@ -92,44 +69,38 @@ pub struct WithState { impl Service for WithState where - Srv: Service>, + 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> { - Poll::Ready(Ok(())) + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + 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 Service> for WithState +impl Service<(OState, Event)> for WithState where - Srv: Service>, + 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> { - Poll::Ready(Ok(())) + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + self.service.poll_ready(cx) } - fn call(&mut self, request: EventWithState) -> 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) } } diff --git a/src/macros.rs b/src/macros.rs index d0df7e4..cc47f55 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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) -> 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)), @@ -472,14 +472,14 @@ macro_rules! event_service { $crate::macros::event_service_call! { [< $e:camel >] (e) } } - impl ::tower::Service<$crate::handler::EventWithState> + impl ::tower::Service<(State, $crate::handler::Event)> for [< $e:camel Service >] ] > 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, 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, >>, @@ -489,7 +489,7 @@ macro_rules! event_service { $crate::macros::event_service_call! { state; [< $e:camel >] (e) => e } } - impl ::tower::Service<$crate::handler::EventWithState> + impl ::tower::Service<(State, $crate::handler::Event)> for [< $e:camel Service >] ],) > where Service: ::tower::Service< @@ -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, 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, >>, @@ -509,7 +509,7 @@ macro_rules! event_service { $crate::macros::event_service_call! { state; [< $e:camel >] (e) => (e,) } } - impl ::tower::Service<$crate::handler::EventWithState> + impl ::tower::Service<(State, $crate::handler::Event)> for [< $e:camel Service >] ] )> where Service: ::tower::Service< @@ -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, 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, >>,