Skip to content

Commit

Permalink
feat(service): export hyper::service::MakeServiceRef
Browse files Browse the repository at this point in the history
It's sealed, and has a blanket implementation, and so should only be
used as bounds. Even still, its hidden from the docs.
  • Loading branch information
seanmonstar committed Dec 3, 2018
1 parent 01f6498 commit a522c31
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 41 deletions.
37 changes: 1 addition & 36 deletions src/server/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ use common::exec::{Exec, H2Exec, NewSvcExec};
use common::io::Rewind;
use error::{Kind, Parse};
use proto;
use service::Service;
use service::{MakeServiceRef, Service};
use upgrade::Upgraded;

pub(super) use self::make_service::MakeServiceRef;
pub(super) use self::spawn_all::NoopWatcher;
use self::spawn_all::NewSvcTask;
pub(super) use self::spawn_all::Watcher;
Expand Down Expand Up @@ -910,37 +909,3 @@ mod upgrades {
}
}

pub(crate) mod make_service {
use std::error::Error as StdError;

pub trait MakeServiceRef<Ctx> {
type Error: Into<Box<StdError + Send + Sync>>;
type ReqBody: ::body::Payload;
type ResBody: ::body::Payload;
type Service: ::service::Service<ReqBody=Self::ReqBody, ResBody=Self::ResBody, Error=Self::Error>;
type Future: ::futures::Future<Item=Self::Service>;

fn make_service_ref(&mut self, ctx: &Ctx) -> Self::Future;
}

impl<T, Ctx, E, ME, S, F, IB, OB> MakeServiceRef<Ctx> for T
where
T: for<'a> ::service::MakeService<&'a Ctx, Error=E, MakeError=ME, Service=S, Future=F, ReqBody=IB, ResBody=OB>,
E: Into<Box<StdError + Send + Sync>>,
ME: Into<Box<StdError + Send + Sync>>,
S: ::service::Service<ReqBody=IB, ResBody=OB, Error=E>,
F: ::futures::Future<Item=S, Error=ME>,
IB: ::body::Payload,
OB: ::body::Payload,
{
type Error = E;
type Service = S;
type ReqBody = IB;
type ResBody = OB;
type Future = F;

fn make_service_ref(&mut self, ctx: &Ctx) -> Self::Future {
self.make_service(ctx)
}
}
}
4 changes: 2 additions & 2 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ use tokio_io::{AsyncRead, AsyncWrite};

use body::{Body, Payload};
use common::exec::{Exec, H2Exec, NewSvcExec};
use service::Service;
use service::{MakeServiceRef, Service};
// Renamed `Http` as `Http_` for now so that people upgrading don't see an
// error that `hyper::server::Http` is private...
use self::conn::{Http as Http_, MakeServiceRef, NoopWatcher, SpawnAll};
use self::conn::{Http as Http_, NoopWatcher, SpawnAll};
use self::shutdown::{Graceful, GracefulWatcher};
#[cfg(feature = "runtime")] use self::tcp::AddrIncoming;

Expand Down
4 changes: 2 additions & 2 deletions src/server/shutdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use tokio_io::{AsyncRead, AsyncWrite};
use body::{Body, Payload};
use common::drain::{self, Draining, Signal, Watch, Watching};
use common::exec::{H2Exec, NewSvcExec};
use service::Service;
use super::conn::{MakeServiceRef, SpawnAll, UpgradeableConnection, Watcher};
use service::{MakeServiceRef, Service};
use super::conn::{SpawnAll, UpgradeableConnection, Watcher};

#[allow(missing_debug_implementations)]
pub struct Graceful<I, S, F, E> {
Expand Down
72 changes: 72 additions & 0 deletions src/service/make_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,68 @@ pub trait MakeService<Ctx> {
fn make_service(&mut self, ctx: Ctx) -> Self::Future;
}

// Just a sort-of "trait alias" of `MakeService`, not to be implemented
// by anyone, only used as bounds.
#[doc(hidden)]
pub trait MakeServiceRef<Ctx>: self::sealed::Sealed<Ctx> {
type ReqBody: Payload;
type ResBody: Payload;
type Error: Into<Box<StdError + Send + Sync>>;
type Service: Service<
ReqBody=Self::ReqBody,
ResBody=Self::ResBody,
Error=Self::Error,
>;
type Future: Future<Item=Self::Service>;

// Acting like a #[non_exhaustive] for associated types of this trait.
//
// Basically, no one outside of hyper should be able to set this type
// or declare bounds on it, so it should prevent people from creating
// trait objects or otherwise writing code that requires using *all*
// of the associated types.
//
// Why? So we can add new associated types to this alias in the future,
// if necessary.
type __DontNameMe: self::sealed::CantImpl;

fn make_service_ref(&mut self, ctx: &Ctx) -> Self::Future;
}

impl<T, Ctx, E, ME, S, F, IB, OB> MakeServiceRef<Ctx> for T
where
T: for<'a> MakeService<&'a Ctx, Error=E, MakeError=ME, Service=S, Future=F, ReqBody=IB, ResBody=OB>,
E: Into<Box<StdError + Send + Sync>>,
ME: Into<Box<StdError + Send + Sync>>,
S: Service<ReqBody=IB, ResBody=OB, Error=E>,
F: Future<Item=S, Error=ME>,
IB: Payload,
OB: Payload,
{
type Error = E;
type Service = S;
type ReqBody = IB;
type ResBody = OB;
type Future = F;

type __DontNameMe = self::sealed::CantName;

fn make_service_ref(&mut self, ctx: &Ctx) -> Self::Future {
self.make_service(ctx)
}
}

impl<T, Ctx, E, ME, S, F, IB, OB> self::sealed::Sealed<Ctx> for T
where
T: for<'a> MakeService<&'a Ctx, Error=E, MakeError=ME, Service=S, Future=F, ReqBody=IB, ResBody=OB>,
E: Into<Box<StdError + Send + Sync>>,
ME: Into<Box<StdError + Send + Sync>>,
S: Service<ReqBody=IB, ResBody=OB, Error=E>,
F: Future<Item=S, Error=ME>,
IB: Payload,
OB: Payload,
{}


/// Create a `MakeService` from a function.
///
Expand Down Expand Up @@ -94,3 +156,13 @@ impl<F> fmt::Debug for MakeServiceFn<F> {
}
}

mod sealed {
pub trait Sealed<T> {}

pub trait CantImpl {}

#[allow(missing_debug_implementations)]
pub enum CantName {}

impl CantImpl for CantName {}
}
3 changes: 2 additions & 1 deletion src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ mod make_service;
mod new_service;
mod service;

pub use self::make_service::{make_service_fn, MakeService};
pub use self::make_service::{make_service_fn, MakeService, MakeServiceRef};
// NewService is soft-deprecated.
#[doc(hidden)]
pub use self::new_service::NewService;
pub use self::service::{service_fn, service_fn_ok, Service};

0 comments on commit a522c31

Please sign in to comment.