Seal Handler trait

This commit is contained in:
David Pedersen 2021-05-30 11:19:36 +02:00
parent 2e16842431
commit f08b73f7b2

View file

@ -216,11 +216,21 @@ impl From<Infallible> for Error {
}
}
// TODO(david): make this trait sealed
mod sealed {
pub trait HiddentTrait {}
pub struct Hidden;
impl HiddentTrait for Hidden {}
}
#[async_trait]
pub trait Handler<In>: Sized {
type ResponseBody;
// This seals the trait. We cannot use the regular "sealed super trait" approach
// due to coherence.
#[doc(hidden)]
type Sealed: sealed::HiddentTrait;
async fn call(self, req: Request<Body>) -> Result<Response<Self::ResponseBody>, Error>;
fn layer<L>(self, layer: L) -> Layered<L::Service, In>
@ -239,6 +249,8 @@ where
{
type ResponseBody = B;
type Sealed = sealed::Hidden;
async fn call(self, req: Request<Body>) -> Result<Response<Self::ResponseBody>, Error> {
self(req).await
}
@ -256,6 +268,8 @@ macro_rules! impl_handler {
{
type ResponseBody = B;
type Sealed = sealed::Hidden;
async fn call(self, mut req: Request<Body>) -> Result<Response<Self::ResponseBody>, Error> {
let $head = $head::from_request(&mut req).await?;
let res = self(req, $head).await?;
@ -276,6 +290,8 @@ macro_rules! impl_handler {
{
type ResponseBody = B;
type Sealed = sealed::Hidden;
async fn call(self, mut req: Request<Body>) -> Result<Response<Self::ResponseBody>, Error> {
let $head = $head::from_request(&mut req).await?;
$(
@ -315,6 +331,8 @@ where
{
type ResponseBody = B;
type Sealed = sealed::Hidden;
async fn call(self, req: Request<Body>) -> Result<Response<Self::ResponseBody>, Error> {
self.svc
.oneshot(req)