Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot register function as middleware due to Debug trait requirements #496

Closed
jhorstmann opened this issue May 11, 2020 · 4 comments
Closed

Comments

@jhorstmann
Copy link

Looking at the middleware documentation, there is a implementation for Fn(Request<State>, Next<'a, State>) -> BoxFuture<'a, Result>, which should allow me to register an async function as a middleware without implementing a struct and trait. Unfortunately the Server::middleware function additionally has a requirement that the middleware implements the Debug trait. So the following example fails

async fn handle_error<State: Send + Sync + 'static>(
    ctx: Request<State>,
    next: Next<State>,
) -> Result {
    ...
}

...

let mut app = tide::new();
app.middleware(handle_error);


error[E0277]: `fn(tide::request::Request<_>, tide::middleware::Next<'static, _>) -> impl std::future::Future {handle_error::<_>}` doesn't implement `std::fmt::Debug`
  --> src/main.rs:97:20
   |
97 |     app.middleware(handle_error);
   |                    ^^^^^^^^^^^^ `fn(tide::request::Request<_>, tide::middleware::Next<'static, _>) -> impl std::future::Future {handle_error::<_>}` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
   |
   = help: the trait `std::fmt::Debug` is not implemented for `fn(tide::request::Request<_>, tide::middleware::Next<'static, _>) -> impl std::future::Future {handle_error::<_>}`

This makes implementing a middleware much more complex, especially for rust beginners.

@yoshuawuyts
Copy link
Member

Yeah that's fair — maybe we should get rid of the debug requirement, but add a default method on the middleware trait that contains the middleware name.

@jhorstmann
Copy link
Author

It seems part of this was fixed in #545, to me as a rust beginner the signature there still looks overly complex:

fn user_loader<'a>(
    mut request: Request<UserDatabase>,
    next: Next<'a, UserDatabase>,
) -> Pin<Box<dyn Future<Output = Result> + Send + 'a>> {

I'm still wondering if it might be possible to register a simple async function that returns a Result. An explanation why this might not be possible would also be very appreciated.

@yoshuawuyts
Copy link
Member

#556 makes this easier by exposing Before and After middleware constructors. We currently cannot implement the middleware trait for async fns, so this seems to be about the best we can do until the language catches up.

@jhorstmann
Copy link
Author

Sounds reasonable. If you want you can close this issue (or I can close), unless you want to keep it open as a reminder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants