mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-22 00:00:17 +02:00
Move axum-debug into axum-macros (#724)
* Move axum-debug into axum-macros * fix ref to axum-macros in changelog * Apply suggestions from code review Co-authored-by: Jonas Platte <[email protected]> Co-authored-by: Jonas Platte <[email protected]>
This commit is contained in:
co-authored by
Jonas Platte
parent
b1283e9708
commit
f6fc5ed80c
@@ -0,0 +1,6 @@
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler(foo: bool) {}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,7 @@
|
||||
error[E0277]: the trait bound `bool: FromRequest<Body>` is not satisfied
|
||||
--> tests/debug_handler/fail/argument_not_extractor.rs:4:23
|
||||
|
|
||||
4 | async fn handler(foo: bool) {}
|
||||
| ^^^^ the trait `FromRequest<Body>` is not implemented for `bool`
|
||||
|
|
||||
= help: see issue #48214
|
||||
@@ -0,0 +1,26 @@
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::{FromRequest, RequestParts},
|
||||
};
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
struct A;
|
||||
|
||||
#[async_trait]
|
||||
impl<B> FromRequest<B> for A
|
||||
where
|
||||
B: Send + 'static,
|
||||
{
|
||||
type Rejection = ();
|
||||
|
||||
async fn from_request(_req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl A {
|
||||
#[debug_handler]
|
||||
async fn handler(&mut self) {}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: Handlers must only take owned values
|
||||
--> tests/debug_handler/fail/extract_self_mut.rs:23:22
|
||||
|
|
||||
23 | async fn handler(&mut self) {}
|
||||
| ^^^^^^^^^
|
||||
@@ -0,0 +1,26 @@
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::{FromRequest, RequestParts},
|
||||
};
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
struct A;
|
||||
|
||||
#[async_trait]
|
||||
impl<B> FromRequest<B> for A
|
||||
where
|
||||
B: Send + 'static,
|
||||
{
|
||||
type Rejection = ();
|
||||
|
||||
async fn from_request(_req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl A {
|
||||
#[debug_handler]
|
||||
async fn handler(&self) {}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: Handlers must only take owned values
|
||||
--> tests/debug_handler/fail/extract_self_ref.rs:23:22
|
||||
|
|
||||
23 | async fn handler(&self) {}
|
||||
| ^^^^^
|
||||
@@ -0,0 +1,6 @@
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler<T>() {}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,13 @@
|
||||
error: `#[axum_macros::debug_handler]` doesn't support generic functions
|
||||
--> tests/debug_handler/fail/generics.rs:4:17
|
||||
|
|
||||
4 | async fn handler<T>() {}
|
||||
| ^^^
|
||||
|
||||
error[E0282]: type annotations needed
|
||||
--> tests/debug_handler/fail/generics.rs:4:10
|
||||
|
|
||||
4 | async fn handler<T>() {}
|
||||
| ----- ^^^^^^^ cannot infer type for type parameter `T` declared on the function `handler`
|
||||
| |
|
||||
| consider giving `future` a type
|
||||
@@ -0,0 +1,6 @@
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
#[debug_handler(foo)]
|
||||
async fn handler() {}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: unknown argument
|
||||
--> tests/debug_handler/fail/invalid_attrs.rs:3:17
|
||||
|
|
||||
3 | #[debug_handler(foo)]
|
||||
| ^^^
|
||||
@@ -0,0 +1,6 @@
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
#[debug_handler]
|
||||
struct A;
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: expected `fn`
|
||||
--> tests/debug_handler/fail/not_a_function.rs:4:1
|
||||
|
|
||||
4 | struct A;
|
||||
| ^^^^^^
|
||||
@@ -0,0 +1,6 @@
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
#[debug_handler]
|
||||
fn handler() {}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: Handlers must be `async fn`s
|
||||
--> tests/debug_handler/fail/not_async.rs:4:1
|
||||
|
|
||||
4 | fn handler() {}
|
||||
| ^^
|
||||
@@ -0,0 +1,9 @@
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler() {
|
||||
let rc = std::rc::Rc::new(());
|
||||
async {}.await;
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,21 @@
|
||||
error: future cannot be sent between threads safely
|
||||
--> tests/debug_handler/fail/not_send.rs:4:1
|
||||
|
|
||||
4 | async fn handler() {
|
||||
| ^^^^^ future returned by `handler` is not `Send`
|
||||
|
|
||||
= help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `Rc<()>`
|
||||
note: future is not `Send` as this value is used across an await
|
||||
--> tests/debug_handler/fail/not_send.rs:6:5
|
||||
|
|
||||
5 | let rc = std::rc::Rc::new(());
|
||||
| -- has type `Rc<()>` which is not `Send`
|
||||
6 | async {}.await;
|
||||
| ^^^^^^^^^^^^^^ await occurs here, with `rc` maybe used later
|
||||
7 | }
|
||||
| - `rc` is later dropped here
|
||||
note: required by a bound in `check`
|
||||
--> tests/debug_handler/fail/not_send.rs:4:1
|
||||
|
|
||||
4 | async fn handler() {
|
||||
| ^^^^^ required by this bound in `check`
|
||||
@@ -0,0 +1,24 @@
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler(
|
||||
e1: String,
|
||||
e2: String,
|
||||
e3: String,
|
||||
e4: String,
|
||||
e5: String,
|
||||
e6: String,
|
||||
e7: String,
|
||||
e8: String,
|
||||
e9: String,
|
||||
e10: String,
|
||||
e11: String,
|
||||
e12: String,
|
||||
e13: String,
|
||||
e14: String,
|
||||
e15: String,
|
||||
e16: String,
|
||||
e17: String,
|
||||
) {}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,11 @@
|
||||
error: Handlers cannot take more than 16 arguments. Use `(a, b): (ExtractorA, ExtractorA)` to further nest extractors
|
||||
--> tests/debug_handler/fail/too_many_extractors.rs:5:5
|
||||
|
|
||||
5 | / e1: String,
|
||||
6 | | e2: String,
|
||||
7 | | e3: String,
|
||||
8 | | e4: String,
|
||||
... |
|
||||
20 | | e16: String,
|
||||
21 | | e17: String,
|
||||
| |________________^
|
||||
@@ -0,0 +1,8 @@
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,11 @@
|
||||
error[E0277]: the trait bound `bool: IntoResponse` is not satisfied
|
||||
--> tests/debug_handler/fail/wrong_return_type.rs:4:23
|
||||
|
|
||||
4 | async fn handler() -> bool {
|
||||
| ^^^^ the trait `IntoResponse` is not implemented for `bool`
|
||||
|
|
||||
note: required by a bound in `__axum_macros_check_handler_into_response::{closure#0}::check`
|
||||
--> tests/debug_handler/fail/wrong_return_type.rs:4:23
|
||||
|
|
||||
4 | async fn handler() -> bool {
|
||||
| ^^^^ required by this bound in `__axum_macros_check_handler_into_response::{closure#0}::check`
|
||||
@@ -0,0 +1,10 @@
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
struct A;
|
||||
|
||||
impl A {
|
||||
#[debug_handler]
|
||||
async fn handler() {}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,10 @@
|
||||
use axum::{body::BoxBody, http::Request};
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
#[debug_handler(body = BoxBody)]
|
||||
async fn handler(_: Request<BoxBody>) {}
|
||||
|
||||
#[debug_handler(body = axum::body::BoxBody,)]
|
||||
async fn handler_with_trailing_comma_and_type_path(_: Request<axum::body::BoxBody>) {}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,9 @@
|
||||
use axum_macros::debug_handler;
|
||||
use std::future::Future;
|
||||
|
||||
#[debug_handler]
|
||||
fn handler() -> impl Future<Output = ()> {
|
||||
async {}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,9 @@
|
||||
use axum_macros::debug_handler;
|
||||
use axum::response::IntoResponse;
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler() -> impl IntoResponse {
|
||||
"hi!"
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,6 @@
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler(_one: String, _two: String, _three: String) {}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,9 @@
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler(mut foo: String) -> String {
|
||||
foo += "bar";
|
||||
foo
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,9 @@
|
||||
use axum_macros::debug_handler;
|
||||
use std::future::{Ready, ready};
|
||||
|
||||
#[debug_handler]
|
||||
fn handler() -> Ready<()> {
|
||||
ready(())
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,132 @@
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::{FromRequest, RequestParts},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
fn main() {}
|
||||
|
||||
#[debug_handler]
|
||||
fn concrete_future() -> std::future::Ready<Result<impl IntoResponse, ()>> {
|
||||
std::future::ready(Ok(()))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
fn impl_future() -> impl std::future::Future<Output = Result<impl IntoResponse, ()>> {
|
||||
std::future::ready(Ok(()))
|
||||
}
|
||||
|
||||
// === no args ===
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_no_arg_one() -> Result<impl IntoResponse, ()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_no_arg_two() -> Result<(), impl IntoResponse> {
|
||||
Err(())
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_no_arg_three() -> Result<impl IntoResponse, impl IntoResponse> {
|
||||
Ok::<_, ()>(())
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_no_arg_four() -> Result<impl IntoResponse, impl IntoResponse> {
|
||||
Err::<(), _>(())
|
||||
}
|
||||
|
||||
// === args ===
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_one(foo: String) -> Result<impl IntoResponse, ()> {
|
||||
dbg!(foo);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_two(foo: String) -> Result<(), impl IntoResponse> {
|
||||
dbg!(foo);
|
||||
Err(())
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_three(foo: String) -> Result<impl IntoResponse, impl IntoResponse> {
|
||||
dbg!(foo);
|
||||
Ok::<_, ()>(())
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_four(foo: String) -> Result<impl IntoResponse, impl IntoResponse> {
|
||||
dbg!(foo);
|
||||
Err::<(), _>(())
|
||||
}
|
||||
|
||||
// === no args with receiver ===
|
||||
|
||||
struct A;
|
||||
|
||||
impl A {
|
||||
#[debug_handler]
|
||||
async fn handler_no_arg_one(self) -> Result<impl IntoResponse, ()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_no_arg_two(self) -> Result<(), impl IntoResponse> {
|
||||
Err(())
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_no_arg_three(self) -> Result<impl IntoResponse, impl IntoResponse> {
|
||||
Ok::<_, ()>(())
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_no_arg_four(self) -> Result<impl IntoResponse, impl IntoResponse> {
|
||||
Err::<(), _>(())
|
||||
}
|
||||
}
|
||||
|
||||
// === args with receiver ===
|
||||
|
||||
impl A {
|
||||
#[debug_handler]
|
||||
async fn handler_one(self, foo: String) -> Result<impl IntoResponse, ()> {
|
||||
dbg!(foo);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_two(self, foo: String) -> Result<(), impl IntoResponse> {
|
||||
dbg!(foo);
|
||||
Err(())
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_three(self, foo: String) -> Result<impl IntoResponse, impl IntoResponse> {
|
||||
dbg!(foo);
|
||||
Ok::<_, ()>(())
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler_four(self, foo: String) -> Result<impl IntoResponse, impl IntoResponse> {
|
||||
dbg!(foo);
|
||||
Err::<(), _>(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<B> FromRequest<B> for A
|
||||
where
|
||||
B: Send + 'static,
|
||||
{
|
||||
type Rejection = ();
|
||||
|
||||
async fn from_request(_req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
use axum::response::{IntoResponse, Response};
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
struct A;
|
||||
|
||||
impl A {
|
||||
#[debug_handler]
|
||||
async fn handler() -> Self {
|
||||
A
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for A {
|
||||
fn into_response(self) -> Response {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,26 @@
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::{FromRequest, RequestParts},
|
||||
};
|
||||
use axum_macros::debug_handler;
|
||||
|
||||
struct A;
|
||||
|
||||
#[async_trait]
|
||||
impl<B> FromRequest<B> for A
|
||||
where
|
||||
B: Send + 'static,
|
||||
{
|
||||
type Rejection = ();
|
||||
|
||||
async fn from_request(_req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl A {
|
||||
#[debug_handler]
|
||||
async fn handler(self) {}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
Reference in New Issue
Block a user