首页 > 解决方案 > 如何从文件更改中热重载结构

问题描述

我正在尝试在文件更改时进行热重新加载,但出现此错误:期望一个实现该Fn特征的闭包,但此闭包仅实现FnMut 此闭包 implements FnMut,而不是Fn

似乎对我从这个库传递给 new_immediate 函数的闭包不满意:

notify = { version = "5.0.0-pre.4", features = ["serde"] }

我的代码:

use announcer::messages::{load_config, save_config, Config, Message};
use notify::{
    event::ModifyKind, Error, Event, EventFn, EventKind, RecommendedWatcher, RecursiveMode, Watcher,
};
use tide::{http, Body, Response};

const CONFIG_PATH: &str = "config.json";

#[async_std::main]
async fn main() -> tide::Result<()> {
    let mut config = load_config(CONFIG_PATH).unwrap();

    let mut watcher: RecommendedWatcher =
        Watcher::new_immediate(|result: Result<Event, Error>| {
            let event = result.unwrap();
            
            if event.kind == EventKind::Modify(ModifyKind::Any) {
                config = load_config(CONFIG_PATH).unwrap();
            }
        })?;

    watcher.watch(CONFIG_PATH, RecursiveMode::Recursive)?;

    let mut app = tide::with_state(config);
    app.listen("127.0.0.1:8080").await?;

    Ok(())
}

我在 Rust Discord 初学者聊天中询问,17cupsofcoffee 说我应该使用互斥锁,但我不知道该怎么做。

标签: filerustwatchnotify

解决方案


这里的问题是您在另一个线程中生成了这个监视函数,并且您可能在一个线程中写入它,同时在另一个线程中读取它,从而导致竞争条件。您应该使用Mutex, 和lock它来获得一个保护,让您从中读取/写入它。由于tide的全局状态也需要Clone,因此您还应该将其包装在Arc一个线程安全的引用计数指针中:

use announcer::messages::{load_config, save_config, Config, Message};
use notify::{
    event::ModifyKind, Error, Event, EventFn, EventKind, RecommendedWatcher, RecursiveMode, Watcher,
};
use tide::{http, Body, Response};
// necessary imports
use std::sync::{Arc, Mutex};

const CONFIG_PATH: &str = "config.json";

#[async_std::main]
async fn main() -> tide::Result<()> {
    // we store it in an Arc<Mutex<T>>
    let config = Arc::new(Mutex::new(load_config(CONFIG_PATH).unwrap()));
    let cloned_config = Arc::clone(&config);

    let mut watcher: RecommendedWatcher =
        Watcher::new_immediate(move |result: Result<Event, Error>| {
            let event = result.unwrap();
            
            if event.kind == EventKind::Modify(ModifyKind::Any) {
                // we lock the mutex to acquire a mutable guard and write to that
                match load_config(CONFIG_PATH) {
                    Ok(new_config) => *cloned_config.lock().unwrap() = new_config,
                    Err(error) => println!("Error reloading config: {:?}", error),
                }
        })?;

    watcher.watch(CONFIG_PATH, RecursiveMode::Recursive)?;
    
    let mut app = tide::with_state(config);
    app.listen("127.0.0.1:8080").await?;

    Ok(())
}

然后,每当您需要从状态中读取时,您只需锁定互斥锁:

let config_guard = req.state().lock().unwrap();
println!("{:?}", config_guard.foo);

更多参考:


推荐阅读