axum icon indicating copy to clipboard operation
axum copied to clipboard

Add support for generic functions to `#[debug_handler]`

Open jplatte opened this issue 1 year ago • 3 comments

I've had an idea for a while that would make generic handlers pretty much fully checkable, that is:

#[debug_handler(with(T = String, T = u64; U = i8, U = i16; N = 0))]
fn handler<T, U, const N: usize>(extract_t: Path<T>, extract_u: Json<U>, extract_n: Foo<N>)
where
    T: Deserialize,
    U: Deserialize,
{
    // ...
}

could make debug_handler add FromRequest assertions for Path<String>, Path<u64>, Json<i8>, Json<i16> and Foo<0> while also adding Send assertions for the futures returned by

  • handler::<String, i8, 0>
  • handler::<String, i16, 0>
  • handler::<u64, i8, 0>
  • handler::<u64, i16, 0>

If we were to do that, we could just require users to add this with() annotation before any parameters are checked on a generic function (even ones that don't mention the generic types) as it should be easy enough.

Originally posted in https://github.com/tokio-rs/axum/issues/1251#issuecomment-1212942852

jplatte avatar Aug 12 '22 10:08 jplatte

For anybody who wants to work on this, here's some tips:

  • If you aren't familiar with the debug_handler macro yet, go read its documentation to get an understanding of what it does
  • We also have tests for it here, the most interesting ones are probably the ones in fail, where every .rs test case has a corresponding .stderr file showing the error generated by the macro
  • You can find the macro implementation here
  • If you have never worked with procedural macro code before and find the code hard to read, maybe my blog series on proc-macros over at blog.turbo.fish can help :)
  • The important parts for this this feature are the functions check_inputs_impl_from_request for the FromRequest implementation assertions and check_future_send for the Send assertions
  • I'm pretty sure there's nothing to be done for lifetime generics, those should just be an error
  • It's probably worthwhile to start with type generic support and add const generic support later, if at all
  • Use syn::visit_mut to substitute the concrete types for uses of a generic parameter, this ensures you find them all even in very complex cases like <<T as Trait>::Foo as some_crate::OtherTrait<U>>::Bar

jplatte avatar Aug 12 '22 10:08 jplatte

@jplatte I am interested in taking a crack at this. thanks for the extensive write-up!

slessans avatar Aug 12 '22 17:08 slessans

@jplatte here is a proof-of-concept that implements this for generic handler args for early feedback if you have some time.

https://github.com/tokio-rs/axum/pull/1265

will move onto return types next

slessans avatar Aug 16 '22 23:08 slessans

Is it still open for taking. I am new here, was looking for some simple issue to work on.

RoKu1 avatar Nov 21 '22 13:11 RoKu1

No, this is mostly done in #1265.

jplatte avatar Nov 21 '22 13:11 jplatte

Hi, Is there any simple task I can take up.

RoKu1 avatar Nov 21 '22 13:11 RoKu1

any updates here?

0xdeepmehta avatar Feb 07 '23 13:02 0xdeepmehta

@0xdeepmehta No. I still have to review the PR.

davidpdrsn avatar Feb 07 '23 18:02 davidpdrsn

As per latest comments in the PR, I'll close this since it's probably not worth the maintenance burden to support.

jplatte avatar Apr 14 '23 19:04 jplatte