Ogeon / rustful

[OUTDATED] A light HTTP framework for Rust
https://docs.rs/rustful
Apache License 2.0
862 stars 51 forks source link

Unable to add multiple routes #116

Closed hjr3 closed 8 years ago

hjr3 commented 8 years ago

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 8 years ago

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 8 years ago

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 8 years ago

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