Skip to content

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

@jhorstmann

Description

@jhorstmann

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions