Search code examples
rustrust-rocket

from_request mismatched types


I am learning rust and I am trying to run the cookies/session example from rocket repo (https://github.com/rwf2/Rocket/blob/master/examples/cookies/src/session.rs)

use rocket::http::Status;
use rocket::outcome::IntoOutcome;
use rocket::request::{self, FromRequest, Request};

#[derive(Debug)]
pub struct User(usize);

#[rocket::async_trait]
impl<'r> FromRequest<'r> for User {
    type Error = std::convert::Infallible;

    async fn from_request(request: &'r Request<'_>) -> request::Outcome<User, Self::Error> {
        request
            .cookies()
            .get_private("user_id")
            .and_then(|cookie| cookie.value().parse().ok())
            .map(User)
            .or_forward(Status::Unauthorized)
    }
}

but I am getting mismatched types error

|             .or_forward(Status::Unauthorized)
|              ---------- ^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Status`
|              |
|              arguments to this method are incorrect

If I replace .or_forward(Status::Unauthorized) with .or_forward(()) then it compiles, but I loose the status.

Can someone help?


Solution

  • The problem is you're compiling code for rocket v0.5.0-rc4 and up with an old version (<=0.5.0-rc3) of it as dependency, in your Cargo.toml replace the line resembling this:

    [dependencies]
    rocket = { version = "0.4" }
    

    with this:

    [dependencies]
    rocket = { version = "0.5", features = ["secrets"] }
    

    and maybe cargo update as well.

    rustexplorer