Skip to content

Commit 4725533

Browse files
authored
Merge pull request #244 from H1rono/with-state
`WithState`まわり修正
2 parents c0c16ba + b5de3d0 commit 4725533

File tree

2 files changed

+18
-47
lines changed

2 files changed

+18
-47
lines changed

src/handler.rs

+10-39
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,6 @@ impl<T> Service<T> for Sink {
5454
}
5555
}
5656

57-
/// [`Event`]と`State`の直積です。
58-
///
59-
/// [`Event`]: crate::Event
60-
#[must_use]
61-
#[derive(Debug, Clone)]
62-
pub struct EventWithState<State> {
63-
state: State,
64-
event: Event,
65-
}
66-
67-
impl<State> From<(State, Event)> for EventWithState<State> {
68-
fn from((state, event): (State, Event)) -> Self {
69-
Self { state, event }
70-
}
71-
}
72-
73-
impl<State> From<EventWithState<State>> for (State, Event) {
74-
fn from(value: EventWithState<State>) -> Self {
75-
let EventWithState { state, event } = value;
76-
(state, event)
77-
}
78-
}
79-
8057
/// 内部の`Service`に`State`を渡す[`Service`]です。
8158
///
8259
/// `WithState::call`の度に`State`がcloneされるため、`State`は[`Clone`]を実装する必要があります。
@@ -92,44 +69,38 @@ pub struct WithState<State, Service> {
9269

9370
impl<State, Srv> Service<Event> for WithState<State, Srv>
9471
where
95-
Srv: Service<EventWithState<State>>,
72+
Srv: Service<(State, Event)>,
9673
State: Clone,
9774
{
9875
type Response = Srv::Response;
9976
type Error = Srv::Error;
10077
type Future = Srv::Future;
10178

102-
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
103-
Poll::Ready(Ok(()))
79+
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
80+
self.service.poll_ready(cx)
10481
}
10582

10683
fn call(&mut self, request: Event) -> Self::Future {
107-
let request = EventWithState {
108-
state: self.state.clone(),
109-
event: request,
110-
};
84+
let request = (self.state.clone(), request);
11185
self.service.call(request)
11286
}
11387
}
11488

115-
impl<OState, State, Srv> Service<EventWithState<OState>> for WithState<State, Srv>
89+
impl<OState, State, Srv> Service<(OState, Event)> for WithState<State, Srv>
11690
where
117-
Srv: Service<EventWithState<State>>,
91+
Srv: Service<(State, Event)>,
11892
State: Clone,
11993
{
12094
type Response = Srv::Response;
12195
type Error = Srv::Error;
12296
type Future = Srv::Future;
12397

124-
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
125-
Poll::Ready(Ok(()))
98+
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
99+
self.service.poll_ready(cx)
126100
}
127101

128-
fn call(&mut self, request: EventWithState<OState>) -> Self::Future {
129-
let request = EventWithState {
130-
state: self.state.clone(),
131-
event: request.event,
132-
};
102+
fn call(&mut self, (_, request): (OState, Event)) -> Self::Future {
103+
let request = (self.state.clone(), request);
133104
self.service.call(request)
134105
}
135106
}

src/macros.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ macro_rules! event_service_call {
426426
$s:ident;
427427
$v:ident ( $i:ident ) => $e:expr
428428
) => {
429-
fn call(&mut self, req: $crate::handler::EventWithState<State>) -> Self::Future {
430-
let ($s, event) = req.into();
429+
fn call(&mut self, req: (State, $crate::handler::Event)) -> Self::Future {
430+
let ($s, event) = req;
431431
match event {
432432
$crate::Event::$v($i) => ::futures::future::Either::Left(
433433
$crate::handler::WrapErrorFuture::new(self.inner.call($e)),
@@ -472,14 +472,14 @@ macro_rules! event_service {
472472
$crate::macros::event_service_call! { [< $e:camel >] (e) }
473473
}
474474

475-
impl<State, Service, Fallback> ::tower::Service<$crate::handler::EventWithState<State>>
475+
impl<State, Service, Fallback> ::tower::Service<(State, $crate::handler::Event)>
476476
for [< $e:camel Service >] <Service, Fallback, $crate::payloads::[< $e:camel Payload >] >
477477
where
478478
Service: ::tower::Service<$crate::payloads::[< $e:camel Payload >], Response = ()>,
479479
Service::Error: ::std::convert::Into<::std::boxed::Box<
480480
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
481481
>>,
482-
Fallback: ::tower::Service<$crate::handler::EventWithState<State>, Response = ()>,
482+
Fallback: ::tower::Service<(State, $crate::handler::Event), Response = ()>,
483483
Fallback::Error: ::std::convert::Into<::std::boxed::Box<
484484
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
485485
>>,
@@ -489,7 +489,7 @@ macro_rules! event_service {
489489
$crate::macros::event_service_call! { state; [< $e:camel >] (e) => e }
490490
}
491491

492-
impl<State, Service, Fallback> ::tower::Service<$crate::handler::EventWithState<State>>
492+
impl<State, Service, Fallback> ::tower::Service<(State, $crate::handler::Event)>
493493
for [< $e:camel Service >] <Service, Fallback, ($crate::payloads::[< $e:camel Payload >],) >
494494
where
495495
Service: ::tower::Service<
@@ -499,7 +499,7 @@ macro_rules! event_service {
499499
Service::Error: ::std::convert::Into<::std::boxed::Box<
500500
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
501501
>>,
502-
Fallback: ::tower::Service<$crate::handler::EventWithState<State>, Response = ()>,
502+
Fallback: ::tower::Service<(State, $crate::handler::Event), Response = ()>,
503503
Fallback::Error: ::std::convert::Into<::std::boxed::Box<
504504
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
505505
>>,
@@ -509,7 +509,7 @@ macro_rules! event_service {
509509
$crate::macros::event_service_call! { state; [< $e:camel >] (e) => (e,) }
510510
}
511511

512-
impl<State, Service, Fallback> ::tower::Service<$crate::handler::EventWithState<State>>
512+
impl<State, Service, Fallback> ::tower::Service<(State, $crate::handler::Event)>
513513
for [< $e:camel Service >] <Service, Fallback, (State, $crate::payloads::[< $e:camel Payload >] )>
514514
where
515515
Service: ::tower::Service<
@@ -519,7 +519,7 @@ macro_rules! event_service {
519519
Service::Error: ::std::convert::Into<::std::boxed::Box<
520520
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
521521
>>,
522-
Fallback: ::tower::Service<$crate::handler::EventWithState<State>, Response = ()>,
522+
Fallback: ::tower::Service<(State, $crate::handler::Event), Response = ()>,
523523
Fallback::Error: ::std::convert::Into<::std::boxed::Box<
524524
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
525525
>>,

0 commit comments

Comments
 (0)