Skip to content

Commit a522c31

Browse files
committed
feat(service): export hyper::service::MakeServiceRef
It's sealed, and has a blanket implementation, and so should only be used as bounds. Even still, its hidden from the docs.
1 parent 01f6498 commit a522c31

File tree

5 files changed

+79
-41
lines changed

5 files changed

+79
-41
lines changed

src/server/conn.rs

+1-36
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ use common::exec::{Exec, H2Exec, NewSvcExec};
2525
use common::io::Rewind;
2626
use error::{Kind, Parse};
2727
use proto;
28-
use service::Service;
28+
use service::{MakeServiceRef, Service};
2929
use upgrade::Upgraded;
3030

31-
pub(super) use self::make_service::MakeServiceRef;
3231
pub(super) use self::spawn_all::NoopWatcher;
3332
use self::spawn_all::NewSvcTask;
3433
pub(super) use self::spawn_all::Watcher;
@@ -910,37 +909,3 @@ mod upgrades {
910909
}
911910
}
912911

913-
pub(crate) mod make_service {
914-
use std::error::Error as StdError;
915-
916-
pub trait MakeServiceRef<Ctx> {
917-
type Error: Into<Box<StdError + Send + Sync>>;
918-
type ReqBody: ::body::Payload;
919-
type ResBody: ::body::Payload;
920-
type Service: ::service::Service<ReqBody=Self::ReqBody, ResBody=Self::ResBody, Error=Self::Error>;
921-
type Future: ::futures::Future<Item=Self::Service>;
922-
923-
fn make_service_ref(&mut self, ctx: &Ctx) -> Self::Future;
924-
}
925-
926-
impl<T, Ctx, E, ME, S, F, IB, OB> MakeServiceRef<Ctx> for T
927-
where
928-
T: for<'a> ::service::MakeService<&'a Ctx, Error=E, MakeError=ME, Service=S, Future=F, ReqBody=IB, ResBody=OB>,
929-
E: Into<Box<StdError + Send + Sync>>,
930-
ME: Into<Box<StdError + Send + Sync>>,
931-
S: ::service::Service<ReqBody=IB, ResBody=OB, Error=E>,
932-
F: ::futures::Future<Item=S, Error=ME>,
933-
IB: ::body::Payload,
934-
OB: ::body::Payload,
935-
{
936-
type Error = E;
937-
type Service = S;
938-
type ReqBody = IB;
939-
type ResBody = OB;
940-
type Future = F;
941-
942-
fn make_service_ref(&mut self, ctx: &Ctx) -> Self::Future {
943-
self.make_service(ctx)
944-
}
945-
}
946-
}

src/server/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ use tokio_io::{AsyncRead, AsyncWrite};
6565

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

src/server/shutdown.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use tokio_io::{AsyncRead, AsyncWrite};
44
use body::{Body, Payload};
55
use common::drain::{self, Draining, Signal, Watch, Watching};
66
use common::exec::{H2Exec, NewSvcExec};
7-
use service::Service;
8-
use super::conn::{MakeServiceRef, SpawnAll, UpgradeableConnection, Watcher};
7+
use service::{MakeServiceRef, Service};
8+
use super::conn::{SpawnAll, UpgradeableConnection, Watcher};
99

1010
#[allow(missing_debug_implementations)]
1111
pub struct Graceful<I, S, F, E> {

src/service/make_service.rs

+72
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,68 @@ pub trait MakeService<Ctx> {
3434
fn make_service(&mut self, ctx: Ctx) -> Self::Future;
3535
}
3636

37+
// Just a sort-of "trait alias" of `MakeService`, not to be implemented
38+
// by anyone, only used as bounds.
39+
#[doc(hidden)]
40+
pub trait MakeServiceRef<Ctx>: self::sealed::Sealed<Ctx> {
41+
type ReqBody: Payload;
42+
type ResBody: Payload;
43+
type Error: Into<Box<StdError + Send + Sync>>;
44+
type Service: Service<
45+
ReqBody=Self::ReqBody,
46+
ResBody=Self::ResBody,
47+
Error=Self::Error,
48+
>;
49+
type Future: Future<Item=Self::Service>;
50+
51+
// Acting like a #[non_exhaustive] for associated types of this trait.
52+
//
53+
// Basically, no one outside of hyper should be able to set this type
54+
// or declare bounds on it, so it should prevent people from creating
55+
// trait objects or otherwise writing code that requires using *all*
56+
// of the associated types.
57+
//
58+
// Why? So we can add new associated types to this alias in the future,
59+
// if necessary.
60+
type __DontNameMe: self::sealed::CantImpl;
61+
62+
fn make_service_ref(&mut self, ctx: &Ctx) -> Self::Future;
63+
}
64+
65+
impl<T, Ctx, E, ME, S, F, IB, OB> MakeServiceRef<Ctx> for T
66+
where
67+
T: for<'a> MakeService<&'a Ctx, Error=E, MakeError=ME, Service=S, Future=F, ReqBody=IB, ResBody=OB>,
68+
E: Into<Box<StdError + Send + Sync>>,
69+
ME: Into<Box<StdError + Send + Sync>>,
70+
S: Service<ReqBody=IB, ResBody=OB, Error=E>,
71+
F: Future<Item=S, Error=ME>,
72+
IB: Payload,
73+
OB: Payload,
74+
{
75+
type Error = E;
76+
type Service = S;
77+
type ReqBody = IB;
78+
type ResBody = OB;
79+
type Future = F;
80+
81+
type __DontNameMe = self::sealed::CantName;
82+
83+
fn make_service_ref(&mut self, ctx: &Ctx) -> Self::Future {
84+
self.make_service(ctx)
85+
}
86+
}
87+
88+
impl<T, Ctx, E, ME, S, F, IB, OB> self::sealed::Sealed<Ctx> for T
89+
where
90+
T: for<'a> MakeService<&'a Ctx, Error=E, MakeError=ME, Service=S, Future=F, ReqBody=IB, ResBody=OB>,
91+
E: Into<Box<StdError + Send + Sync>>,
92+
ME: Into<Box<StdError + Send + Sync>>,
93+
S: Service<ReqBody=IB, ResBody=OB, Error=E>,
94+
F: Future<Item=S, Error=ME>,
95+
IB: Payload,
96+
OB: Payload,
97+
{}
98+
3799

38100
/// Create a `MakeService` from a function.
39101
///
@@ -94,3 +156,13 @@ impl<F> fmt::Debug for MakeServiceFn<F> {
94156
}
95157
}
96158

159+
mod sealed {
160+
pub trait Sealed<T> {}
161+
162+
pub trait CantImpl {}
163+
164+
#[allow(missing_debug_implementations)]
165+
pub enum CantName {}
166+
167+
impl CantImpl for CantName {}
168+
}

src/service/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ mod make_service;
3333
mod new_service;
3434
mod service;
3535

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

0 commit comments

Comments
 (0)