mirror of
https://github.com/tokio-rs/axum.git
synced 2025-10-02 15:24:54 +00:00
generic body almost kinda works
This commit is contained in:
parent
10b38b5421
commit
bf3d4f4a40
92
src/lib.rs
92
src/lib.rs
@ -226,14 +226,14 @@ mod sealed {
|
|||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait Handler<In>: Sized {
|
pub trait Handler<In>: Sized {
|
||||||
type ResponseBody;
|
type Response: IntoResponse;
|
||||||
|
|
||||||
// This seals the trait. We cannot use the regular "sealed super trait" approach
|
// This seals the trait. We cannot use the regular "sealed super trait" approach
|
||||||
// due to coherence.
|
// due to coherence.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
type Sealed: sealed::HiddentTrait;
|
type Sealed: sealed::HiddentTrait;
|
||||||
|
|
||||||
async fn call(self, req: Request<Body>) -> Result<Response<Self::ResponseBody>, Error>;
|
async fn call(self, req: Request<Body>) -> Result<Self::Response, Error>;
|
||||||
|
|
||||||
fn layer<L>(self, layer: L) -> Layered<L::Service, In>
|
fn layer<L>(self, layer: L) -> Layered<L::Service, In>
|
||||||
where
|
where
|
||||||
@ -243,17 +243,37 @@ pub trait Handler<In>: Sized {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait IntoResponse {
|
||||||
|
fn into_response(self) -> Response<Body>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<B> IntoResponse for Response<B>
|
||||||
|
where
|
||||||
|
B: Into<Body>,
|
||||||
|
{
|
||||||
|
fn into_response(self) -> Response<Body> {
|
||||||
|
self.map(Into::into)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoResponse for String {
|
||||||
|
fn into_response(self) -> Response<Body> {
|
||||||
|
Response::new(Body::from(self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<F, Fut, B> Handler<()> for F
|
impl<F, Fut, Res> Handler<()> for F
|
||||||
where
|
where
|
||||||
F: Fn(Request<Body>) -> Fut + Send + Sync,
|
F: Fn(Request<Body>) -> Fut + Send + Sync,
|
||||||
Fut: Future<Output = Result<Response<B>, Error>> + Send,
|
Fut: Future<Output = Result<Res, Error>> + Send,
|
||||||
|
Res: IntoResponse,
|
||||||
{
|
{
|
||||||
type ResponseBody = B;
|
type Response = Res;
|
||||||
|
|
||||||
type Sealed = sealed::Hidden;
|
type Sealed = sealed::Hidden;
|
||||||
|
|
||||||
async fn call(self, req: Request<Body>) -> Result<Response<Self::ResponseBody>, Error> {
|
async fn call(self, req: Request<Body>) -> Result<Self::Response, Error> {
|
||||||
self(req).await
|
self(req).await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -262,17 +282,18 @@ macro_rules! impl_handler {
|
|||||||
( $head:ident $(,)? ) => {
|
( $head:ident $(,)? ) => {
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
impl<F, Fut, B, $head> Handler<($head,)> for F
|
impl<F, Fut, Res, $head> Handler<($head,)> for F
|
||||||
where
|
where
|
||||||
F: Fn(Request<Body>, $head) -> Fut + Send + Sync,
|
F: Fn(Request<Body>, $head) -> Fut + Send + Sync,
|
||||||
Fut: Future<Output = Result<Response<B>, Error>> + Send,
|
Fut: Future<Output = Result<Res, Error>> + Send,
|
||||||
|
Res: IntoResponse,
|
||||||
$head: FromRequest + Send,
|
$head: FromRequest + Send,
|
||||||
{
|
{
|
||||||
type ResponseBody = B;
|
type Response = Res;
|
||||||
|
|
||||||
type Sealed = sealed::Hidden;
|
type Sealed = sealed::Hidden;
|
||||||
|
|
||||||
async fn call(self, mut req: Request<Body>) -> Result<Response<Self::ResponseBody>, Error> {
|
async fn call(self, mut req: Request<Body>) -> Result<Self::Response, Error> {
|
||||||
let $head = $head::from_request(&mut req).await?;
|
let $head = $head::from_request(&mut req).await?;
|
||||||
let res = self(req, $head).await?;
|
let res = self(req, $head).await?;
|
||||||
Ok(res)
|
Ok(res)
|
||||||
@ -283,18 +304,19 @@ macro_rules! impl_handler {
|
|||||||
( $head:ident, $($tail:ident),* $(,)? ) => {
|
( $head:ident, $($tail:ident),* $(,)? ) => {
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
impl<F, Fut, B, $head, $($tail,)*> Handler<($head, $($tail,)*)> for F
|
impl<F, Fut, Res, $head, $($tail,)*> Handler<($head, $($tail,)*)> for F
|
||||||
where
|
where
|
||||||
F: Fn(Request<Body>, $head, $($tail,)*) -> Fut + Send + Sync,
|
F: Fn(Request<Body>, $head, $($tail,)*) -> Fut + Send + Sync,
|
||||||
Fut: Future<Output = Result<Response<B>, Error>> + Send,
|
Fut: Future<Output = Result<Res, Error>> + Send,
|
||||||
|
Res: IntoResponse,
|
||||||
$head: FromRequest + Send,
|
$head: FromRequest + Send,
|
||||||
$( $tail: FromRequest + Send, )*
|
$( $tail: FromRequest + Send, )*
|
||||||
{
|
{
|
||||||
type ResponseBody = B;
|
type Response = Res;
|
||||||
|
|
||||||
type Sealed = sealed::Hidden;
|
type Sealed = sealed::Hidden;
|
||||||
|
|
||||||
async fn call(self, mut req: Request<Body>) -> Result<Response<Self::ResponseBody>, Error> {
|
async fn call(self, mut req: Request<Body>) -> Result<Self::Response, Error> {
|
||||||
let $head = $head::from_request(&mut req).await?;
|
let $head = $head::from_request(&mut req).await?;
|
||||||
$(
|
$(
|
||||||
let $tail = $tail::from_request(&mut req).await?;
|
let $tail = $tail::from_request(&mut req).await?;
|
||||||
@ -325,17 +347,18 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<S, T, B> Handler<T> for Layered<S, T>
|
impl<S, T> Handler<T> for Layered<S, T>
|
||||||
where
|
where
|
||||||
S: Service<Request<Body>, Response = Response<B>> + Send,
|
S: Service<Request<Body>> + Send,
|
||||||
|
S::Response: IntoResponse,
|
||||||
S::Error: Into<BoxError>,
|
S::Error: Into<BoxError>,
|
||||||
S::Future: Send,
|
S::Future: Send,
|
||||||
{
|
{
|
||||||
type ResponseBody = B;
|
type Response = S::Response;
|
||||||
|
|
||||||
type Sealed = sealed::Hidden;
|
type Sealed = sealed::Hidden;
|
||||||
|
|
||||||
async fn call(self, req: Request<Body>) -> Result<Response<Self::ResponseBody>, Error> {
|
async fn call(self, req: Request<Body>) -> Result<Self::Response, Error> {
|
||||||
self.svc
|
self.svc
|
||||||
.oneshot(req)
|
.oneshot(req)
|
||||||
.await
|
.await
|
||||||
@ -380,10 +403,10 @@ where
|
|||||||
|
|
||||||
impl<H, T> Service<Request<Body>> for HandlerSvc<H, T>
|
impl<H, T> Service<Request<Body>> for HandlerSvc<H, T>
|
||||||
where
|
where
|
||||||
H: Handler<T> + Clone + 'static,
|
H: Handler<T> + Clone + Send + 'static,
|
||||||
H::ResponseBody: 'static,
|
H::Response: 'static,
|
||||||
{
|
{
|
||||||
type Response = Response<H::ResponseBody>;
|
type Response = Response<Body>;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type Future = future::BoxFuture<'static, Result<Self::Response, Self::Error>>;
|
type Future = future::BoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||||
|
|
||||||
@ -394,7 +417,10 @@ where
|
|||||||
|
|
||||||
fn call(&mut self, req: Request<Body>) -> Self::Future {
|
fn call(&mut self, req: Request<Body>) -> Self::Future {
|
||||||
let handler = self.handler.clone();
|
let handler = self.handler.clone();
|
||||||
Box::pin(Handler::call(handler, req))
|
Box::pin(async move {
|
||||||
|
let res = Handler::call(handler, req).await?;
|
||||||
|
Ok(res.into_response())
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -794,27 +820,29 @@ mod tests {
|
|||||||
Ok(Response::new(Body::empty()))
|
Ok(Response::new(Body::empty()))
|
||||||
}
|
}
|
||||||
|
|
||||||
let app =
|
async fn users_index(
|
||||||
app()
|
_: Request<Body>,
|
||||||
|
pagination: Query<Pagination>,
|
||||||
|
) -> Result<String, Error> {
|
||||||
|
let pagination = pagination.into_inner();
|
||||||
|
assert_eq!(pagination.page, 1);
|
||||||
|
assert_eq!(pagination.per_page, 30);
|
||||||
|
Ok::<_, Error>("users#index".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
let app = app()
|
||||||
// routes with functions
|
// routes with functions
|
||||||
.at("/")
|
.at("/")
|
||||||
.get(root)
|
.get(root)
|
||||||
// routes with closures
|
// routes with closures
|
||||||
.at("/users")
|
.at("/users")
|
||||||
.get(|_: Request<Body>, pagination: Query<Pagination>| async {
|
.get(users_index)
|
||||||
let pagination = pagination.into_inner();
|
|
||||||
assert_eq!(pagination.page, 1);
|
|
||||||
assert_eq!(pagination.per_page, 30);
|
|
||||||
|
|
||||||
Ok::<_, Error>(Response::new(Body::from("users#index")))
|
|
||||||
})
|
|
||||||
.post(
|
.post(
|
||||||
|_: Request<Body>,
|
|_: Request<Body>,
|
||||||
payload: Json<UsersCreate>,
|
payload: Json<UsersCreate>,
|
||||||
_state: Extension<Arc<State>>| async {
|
_state: Extension<Arc<State>>| async {
|
||||||
let payload = payload.into_inner();
|
let payload = payload.into_inner();
|
||||||
assert_eq!(payload.username, "bob");
|
assert_eq!(payload.username, "bob");
|
||||||
|
|
||||||
Ok::<_, Error>(Response::new(Body::from("users#create")))
|
Ok::<_, Error>(Response::new(Body::from("users#create")))
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user