bpetit/rs-docker-sync

Error when using Rocket "cannot borrow as mutable"

williamdes opened this issue · 2 comments

error[E0596]: cannot borrow data in a dereference of `rocket::State<Docker>` as mutable
  --> src/routes/api/index.rs:35:41
   |
35 |     let version: Option<String> = match docker.get_version() {
   |                                         ^^^^^^ cannot borrow as mutable
   |
   = help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `rocket::State<Docker>`
#[launch]
fn rocket() -> _ {
    rocket::build()
        .manage(docker_conn)
        .mount(
            "/",
            routes_with_openapi![
                routes::api::index::docker_version
            ],
        )
}
#[get("/docker/version")]
pub fn docker_version(docker: &State<Docker>) -> Result<Json<Option<String>>, String> {
    let version: Option<String> = match docker.get_version() {
        Ok(v) => Some(v.Version),
        Err(_) => None,
    };
    Ok(Json(version))
}

Hi,

I didn't try the crate using Rocket so I may lack a bit of context. I used docker.get_version on my side like this:

docker.get_version().unwrap().Version.as_str()

which works fine. Reading the error message I wonder: is there something to check about how rocket::State is used ?

Reading the error message I wonder: is there something to check about how rocket::State is used ?

Probably, could you try to reproduce this on your side ?
I am quite new to Rust