Ogeon/rustful

Unable to add multiple routes

Closed this issue · 3 comments

hjr3 commented

Error:

src/main.rs:138 router.insert(Get, "/health", health_check);
                                              ^~~~~~~~~~~~
src/main.rs:138:31: 138:43 help: run `rustc --explain E0308` to see a detailed explanation
src/main.rs:138:31: 138:43 note: expected type `fn(rustful::Context<'_, '_, '_>, rustful::Response<'_, '_>) {resize}`
src/main.rs:138:31: 138:43 note:    found type `fn(rustful::Context<'_, '_, '_>, rustful::Response<'_, '_>) {health_check}`
error: aborting due to previous error
error: Could not compile `halo-img-resizer`.

Code sample:

    fn health_check(_context: Context, mut response: Response) {
        response.set_status(StatusCode::Ok);
    }

    fn resize(context: Context, mut response: Response) {
        response.set_status(StatusCode::Ok);
    }

    let mut router = TreeRouter::new();

    router.insert(Get, "/resizer", resize);
    router.insert(Get, "/health", health_check);

    Server {
        host: 6767.into(),
        handlers: router,
        ..Server::default()
    }.run().expect("Could not start server");

I am stumped as I am not sure what the error is complaining about?

Ogeon commented

it's the good old function item vs function pointer problem. Each function has its own type that you can't mix just like that, so you have to cast them has function pointers. I think it should be enough to rewrite you first insertion as router.insert(Get, "/resizer", resize as fn(Context, Response)); and the rest should be coerced to the same type.

hjr3 commented

Huzzah! Thank you. I was aware this concept, but I never ran into it in practice. The error message makes slightly more sense now.

Ogeon commented

Yeah, the error message doesn't really make sense if you are used to all functions having the same type.