Skip to content

Commit c9f3542

Browse files
authored
Merge pull request #249 from H1rono/clean-impl-service
Clean impl service
2 parents 7b66625 + f3ba4aa commit c9f3542

File tree

3 files changed

+22
-54
lines changed

3 files changed

+22
-54
lines changed

src/handler.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
//!
66
//! [`Handler`]: crate::Handler
77
8-
use std::convert::Infallible;
98
use std::marker::PhantomData;
109
use std::task::{Context, Poll};
1110

@@ -42,8 +41,8 @@ impl Sink {
4241

4342
impl<T> Service<T> for Sink {
4443
type Response = ();
45-
type Error = Infallible;
46-
type Future = ReadyFuture<Result<(), Infallible>>;
44+
type Error = Error;
45+
type Future = ReadyFuture<Result<(), Error>>;
4746

4847
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
4948
Poll::Ready(Ok(()))
@@ -223,9 +222,8 @@ all_events! {all_handler_on_events}
223222

224223
impl<Srv, Body> Service<Request<Body>> for Handler<Srv>
225224
where
226-
Srv: Service<Event, Response = ()>,
225+
Srv: Service<Event, Response = (), Error = Error>,
227226
Srv: Clone,
228-
Srv::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
229227
Body: http_body::Body,
230228
Body::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
231229
{

src/handler/future.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,16 @@ pin_project! {
105105
}
106106
}
107107

108-
impl<F, E> Future for HandlerCallServiceCall<F>
108+
impl<F> Future for HandlerCallServiceCall<F>
109109
where
110-
F: Future<Output = Result<(), E>>,
111-
E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
110+
F: Future<Output = Result<(), Error>>,
112111
{
113112
type Output = Result<Response<String>>;
114113

115114
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
116115
let s = self.project();
117116
if let Err(e) = ready!(s.inner.poll(cx)) {
118-
return Poll::Ready(Err(Error::handler(e)));
117+
return Poll::Ready(Err(e));
119118
}
120119
let res = Response::builder()
121120
.status(StatusCode::NO_CONTENT)
@@ -149,8 +148,7 @@ impl<B, S> Future for HandlerCallInner<B, S>
149148
where
150149
B: Body,
151150
B::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
152-
S: Service<Event, Response = ()>,
153-
S::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
151+
S: Service<Event, Response = (), Error = Error>,
154152
{
155153
type Output = Result<Response<String>>;
156154

@@ -214,8 +212,7 @@ impl<B, S> Future for HandlerCall<B, S>
214212
where
215213
B: Body,
216214
B::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
217-
S: Service<Event, Response = ()>,
218-
S::Error: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
215+
S: Service<Event, Response = (), Error = Error>,
219216
{
220217
type Output = Result<Response<String>>;
221218

src/macros.rs

+14-41
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ macro_rules! event_service_types {
381381
type Error = $crate::Error;
382382
type Future = ::futures::future::Either<
383383
$crate::handler::WrapErrorFuture<Service::Future, Service::Error>,
384-
$crate::handler::WrapErrorFuture<Fallback::Future, Fallback::Error>,
384+
Fallback::Future,
385385
>;
386386
};
387387
}
@@ -399,9 +399,7 @@ macro_rules! event_service_poll_ready {
399399
));
400400
}
401401
if let ::std::result::Result::Err(e) = ::futures::ready!(self.fallback.poll_ready(cx)) {
402-
return ::std::task::Poll::Ready(::std::result::Result::Err(
403-
$crate::Error::handler(e),
404-
));
402+
return ::std::task::Poll::Ready(::std::result::Result::Err(e));
405403
}
406404
::std::task::Poll::Ready(::std::result::Result::Ok(()))
407405
}
@@ -416,9 +414,7 @@ macro_rules! event_service_call {
416414
$crate::Event::$v($i) => ::futures::future::Either::Left(
417415
$crate::handler::WrapErrorFuture::new(self.inner.call($i)),
418416
),
419-
event => ::futures::future::Either::Right($crate::handler::WrapErrorFuture::new(
420-
self.fallback.call(event),
421-
)),
417+
event => ::futures::future::Either::Right(self.fallback.call(event)),
422418
}
423419
}
424420
};
@@ -432,9 +428,7 @@ macro_rules! event_service_call {
432428
$crate::Event::$v($i) => ::futures::future::Either::Left(
433429
$crate::handler::WrapErrorFuture::new(self.inner.call($e)),
434430
),
435-
event => ::futures::future::Either::Right($crate::handler::WrapErrorFuture::new(
436-
self.fallback.call(($s, event).into()),
437-
)),
431+
event => ::futures::future::Either::Right(self.fallback.call(($s, event))),
438432
}
439433
}
440434
};
@@ -462,10 +456,7 @@ macro_rules! event_service {
462456
Service::Error: ::std::convert::Into<::std::boxed::Box<
463457
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
464458
>>,
465-
Fallback: ::tower::Service<$crate::Event, Response = ()>,
466-
Fallback::Error: ::std::convert::Into<::std::boxed::Box<
467-
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
468-
>>,
459+
Fallback: ::tower::Service<$crate::Event, Response = (), Error = $crate::Error>,
469460
{
470461
$crate::macros::event_service_types! {}
471462
$crate::macros::event_service_poll_ready! {}
@@ -479,34 +470,15 @@ macro_rules! event_service {
479470
Service::Error: ::std::convert::Into<::std::boxed::Box<
480471
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
481472
>>,
482-
Fallback: ::tower::Service<(State, $crate::handler::Event), Response = ()>,
483-
Fallback::Error: ::std::convert::Into<::std::boxed::Box<
484-
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
485-
>>,
486-
{
487-
$crate::macros::event_service_types! {}
488-
$crate::macros::event_service_poll_ready! {}
489-
$crate::macros::event_service_call! { state; [< $e:camel >] (e) => e }
490-
}
491-
492-
impl<State, Service, Fallback> ::tower::Service<(State, $crate::handler::Event)>
493-
for [< On $e:camel >] <Service, Fallback, ($crate::payloads::[< $e:camel Payload >],) >
494-
where
495-
Service: ::tower::Service<
496-
($crate::payloads::[< $e:camel Payload >],),
473+
Fallback: ::tower::Service<
474+
(State, $crate::handler::Event),
497475
Response = (),
476+
Error = $crate::Error,
498477
>,
499-
Service::Error: ::std::convert::Into<::std::boxed::Box<
500-
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
501-
>>,
502-
Fallback: ::tower::Service<(State, $crate::handler::Event), Response = ()>,
503-
Fallback::Error: ::std::convert::Into<::std::boxed::Box<
504-
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
505-
>>,
506478
{
507479
$crate::macros::event_service_types! {}
508480
$crate::macros::event_service_poll_ready! {}
509-
$crate::macros::event_service_call! { state; [< $e:camel >] (e) => (e,) }
481+
$crate::macros::event_service_call! { state; [< $e:camel >] (e) => e }
510482
}
511483

512484
impl<State, Service, Fallback> ::tower::Service<(State, $crate::handler::Event)>
@@ -519,10 +491,11 @@ macro_rules! event_service {
519491
Service::Error: ::std::convert::Into<::std::boxed::Box<
520492
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
521493
>>,
522-
Fallback: ::tower::Service<(State, $crate::handler::Event), Response = ()>,
523-
Fallback::Error: ::std::convert::Into<::std::boxed::Box<
524-
dyn ::std::error::Error + ::std::marker::Send + ::std::marker::Sync + 'static,
525-
>>,
494+
Fallback: ::tower::Service<
495+
(State, $crate::handler::Event),
496+
Response = (),
497+
Error = $crate::Error
498+
>,
526499
{
527500
$crate::macros::event_service_types! {}
528501
$crate::macros::event_service_poll_ready! {}

0 commit comments

Comments
 (0)