mirror of
https://github.com/tokio-rs/axum.git
synced 2024-11-25 00:28:07 +01:00
Seal Handler
trait
This commit is contained in:
parent
2e16842431
commit
f08b73f7b2
1 changed files with 19 additions and 1 deletions
20
src/lib.rs
20
src/lib.rs
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue