Rust Rocket - Change State Element

Hi there!

I’m new to Rust, and trying to build a little WebServer, with the Rocket crate.

I’m able to access a Object from the route handlers, but I’m not able to modify them. Could somebody help me? Is it possible to directly “copy” the complete struct out of the JSON into the “stored_configs” object (Would be nice for multiple values)?

This is my Code so far:

#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;
#[macro_use] extern crate serde_derive;

use rocket::response::Redirect;
use rocket_contrib::json::{Json, JsonValue};
use rocket::State;


#[derive(Serialize, Deserialize)]
struct Configs {
    first_value: String
}

#[post("/configs", format = "json", data = "<message>")]
fn configs(message: Json<Configs>, stored_configs: State<Configs>) -> JsonValue {
    println!("The new value should be: {}", message.0.first_value);
    println!("The old value is: {}", stored_configs.first_value);

    stored_configs = message.0; //what I want but doesn't work

    json!({ "status": "ok" })
}

fn main() {
    let stored_configs = Configs {
        first_value: "initial_value".to_string(),
    };

    rocket::ignite().manage(stored_configs)
        .mount("/", routes![configs])
        .launch();
}

Thanks in advance,
trembel

You’ll need to mutate through something that provides synchronization, like a Mutex. So have rocket manage a Mutex<Configs>, and then use the mutex to set the value in the handler.