Skip to content
This repository was archived by the owner on Apr 16, 2020. It is now read-only.

Commit fd65278

Browse files
committed
Decouple server::Error from MakeService
When server::Error is used in a generic way, but with a particular specialization of its MakeService type parameter, the type parameters and trait bounds of the specialization are included into the signature of the specialized server::Error, even though they may have no bearing on the actual variant error types. Redefine server::Error with two generic type parameters that receive associated types Error and MakeError of a MakeService impl, sans the explicit trait bound. To reduce verbosity in the common case when both parameters get the same type, the second parameter defaults to the type of the first.
1 parent 53d4249 commit fd65278

File tree

1 file changed

+18
-48
lines changed

1 file changed

+18
-48
lines changed

src/server/mod.rs

Lines changed: 18 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ where T: AsyncRead + AsyncWrite,
6161
/// The service has closed, so poll until connection is closed.
6262
GoAway {
6363
connection: Accept<T, SendBuf<<B::Data as IntoBuf>::Buf>>,
64-
error: Error<S>,
64+
error: Error<S::Error, S::MakeError>,
6565
},
6666

6767
/// Everything is closed up.
@@ -94,20 +94,19 @@ where B: Body,
9494
}
9595

9696
/// Error produced by a `Connection`.
97-
pub enum Error<S>
98-
where S: MakeService<(), Request<RecvBody>>,
99-
{
97+
#[derive(Debug)]
98+
pub enum Error<E, ME = E> {
10099
/// Error produced during the HTTP/2.0 handshake.
101100
Handshake(h2::Error),
102101

103102
/// Error produced by the HTTP/2.0 stream
104103
Protocol(h2::Error),
105104

106105
/// Error produced when obtaining the service
107-
NewService(S::MakeError),
106+
NewService(ME),
108107

109108
/// Error produced by the service
110-
Service(S::Error),
109+
Service(E),
111110

112111
/// Error produced when attempting to spawn a task
113112
Execute,
@@ -200,7 +199,7 @@ where T: AsyncRead + AsyncWrite,
200199
F: Modify,
201200
{
202201
type Item = ();
203-
type Error = Error<S>;
202+
type Error = Error<S::Error, S::MakeError>;
204203

205204
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
206205
// Code is in poll2 to make sure any Err returned
@@ -238,7 +237,7 @@ where T: AsyncRead + AsyncWrite,
238237
self.state = State::Done;
239238
}
240239

241-
fn poll2(&mut self) -> Poll<(), Error<S>> {
240+
fn poll2(&mut self) -> Poll<(), Error<S::Error, S::MakeError>> {
242241
loop {
243242
match self.state {
244243
State::Init(..) => try_ready!(self.poll_init()),
@@ -257,7 +256,7 @@ where T: AsyncRead + AsyncWrite,
257256
}
258257
}
259258

260-
fn poll_init(&mut self) -> Poll<(), Error<S>> {
259+
fn poll_init(&mut self) -> Poll<(), Error<S::Error, S::MakeError>> {
261260
use self::State::*;
262261

263262
let (connection, service) = match self.state {
@@ -270,7 +269,7 @@ where T: AsyncRead + AsyncWrite,
270269
Ok(().into())
271270
}
272271

273-
fn poll_main(&mut self) -> Poll<PollMain, Error<S>> {
272+
fn poll_main(&mut self) -> Poll<PollMain, Error<S::Error, S::MakeError>> {
274273
let error = match self.state {
275274
State::Ready { ref mut connection, ref mut service } => loop {
276275
// Make sure the service is ready
@@ -344,7 +343,7 @@ where T: AsyncRead + AsyncWrite,
344343
}
345344
}
346345

347-
fn poll_goaway(&mut self) -> Poll<(), Error<S>> {
346+
fn poll_goaway(&mut self) -> Poll<(), Error<S::Error, S::MakeError>> {
348347
match self.state {
349348
State::GoAway { ref mut connection, .. } => {
350349
try_ready!(connection.poll_close().map_err(Error::Protocol));
@@ -470,47 +469,19 @@ where T: Future<Item = Response<B>>,
470469

471470
// ===== impl Error =====
472471

473-
impl<S> Error<S>
474-
where S: MakeService<(), Request<RecvBody>>,
475-
{
476-
fn from_init(err: Either<h2::Error, S::MakeError>) -> Self {
472+
impl<E, ME> Error<E, ME> {
473+
fn from_init(err: Either<h2::Error, ME>) -> Self {
477474
match err {
478475
Either::A(err) => Error::Handshake(err),
479476
Either::B(err) => Error::NewService(err),
480477
}
481478
}
482479
}
483480

484-
impl<S> fmt::Debug for Error<S>
485-
where
486-
S: MakeService<(), Request<RecvBody>>,
487-
S::MakeError: fmt::Debug,
488-
S::Error: fmt::Debug,
489-
{
490-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
491-
match *self {
492-
Error::Handshake(ref why) => f.debug_tuple("Handshake")
493-
.field(why)
494-
.finish(),
495-
Error::Protocol(ref why) => f.debug_tuple("Protocol")
496-
.field(why)
497-
.finish(),
498-
Error::NewService(ref why) => f.debug_tuple("NewService")
499-
.field(why)
500-
.finish(),
501-
Error::Service(ref why) => f.debug_tuple("Service")
502-
.field(why)
503-
.finish(),
504-
Error::Execute => f.debug_tuple("Execute").finish(),
505-
}
506-
}
507-
}
508-
509-
impl<S> fmt::Display for Error<S>
481+
impl<E, ME> fmt::Display for Error<E, ME>
510482
where
511-
S: MakeService<(), Request<RecvBody>>,
512-
S::MakeError: fmt::Display,
513-
S::Error: fmt::Display,
483+
E: fmt::Display,
484+
ME: fmt::Display,
514485
{
515486
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
516487
match *self {
@@ -528,11 +499,10 @@ where
528499
}
529500
}
530501

531-
impl<S> error::Error for Error<S>
502+
impl<E, ME> error::Error for Error<E, ME>
532503
where
533-
S: MakeService<(), Request<RecvBody>>,
534-
S::MakeError: error::Error,
535-
S::Error: error::Error,
504+
E: error::Error,
505+
ME: error::Error,
536506
{
537507
fn cause(&self) -> Option<&error::Error> {
538508
match *self {

0 commit comments

Comments
 (0)