'Making a variable accessible to handler in salvo

How can I make the current game available to my request handler in Salvo?

The current game needs to be able to change, as it contains the current game state inside it. I don't need any support for multiple games running concurrently.

I would be excited about a solution where I can pass it into the handler somehow from the main function, but am open to anything.

My current attempt at this involves all of this code:

struct Config {
    pub current_game: Option<game::Game>,
}

impl Config {
    pub fn current() -> Arc<Config> {
        CURRENT_CONFIG.with(|c| c.read().unwrap().clone())
    }
    pub fn make_current(self) {
        CURRENT_CONFIG.with(|c| *c.write().unwrap() = Arc::new(self))
    }
}

impl Default for Config {
    fn default() -> Config {
        Config {current_game: None}
    }
}

thread_local! {
    static CURRENT_CONFIG: RwLock<Arc<Config>> = RwLock::new(Default::default());
}

This works, but it's a lot of overhead and boilerplate code. Is there any easier way to do this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source