#How to pass a &'a App in a closure ?

3 messages · Page 1 of 1 (latest)

velvet vine
#

Im a noob in rust and i try to pass the app param to my event_handler function, but i have the following error and i don't know how to solve it..

(dyn for<'a> FnOnce(&'a mut tauri::App) -> Result<(), Box<(dyn std::error::Error + 'static)>> + Send + 'static)` cannot be shared between threads safely
the trait `Sync` is not implemented for `(dyn for<'a> FnOnce(&'a mut tauri::App) -> Result<(), Box<(dyn std::error::Error + 'static)>> + Send + 'static)`, which is required by `{closure@src\app\src-tauri\src\file_watcher.rs:23:41: 23:48}: EventHandler
impl<'a> ExtensionWatcher<'a> {
    pub fn new(app: &'a App) -> notify::Result<Self> {
        let mut watcher =
            notify::recommended_watcher(|event| Self::event_handler(event, app)).unwrap();

        // Start to watch the extension directory
        Self::watch(&mut watcher, EXTENSION_DIRECTORY)?;

        Ok(Self { app, watcher })
    }

    fn event_handler(event: notify::Result<notify::Event>, app: &'a App) {
        match event {
            Ok(event) => {
                if event.kind == EventKind::Modify(ModifyKind::Any) {
                    for path in &event.paths {
                        if path.extension() == Some(OsStr::new("dll")) {
                        }
                    }
                }

                println!("{:?}", event)
            }
            Err(e) => {
                println!("{:?}", e)
            }
        }
    }

    pub fn watch(watcher: &mut notify::RecommendedWatcher, path: &str) -> notify::Result<()> {
        watcher.watch(Path::new(path), notify::RecursiveMode::Recursive)?;
        Ok(())
    }
}
cold flume
#

you rarely want to try to pass around an instance of App. Try using the AppHandle instead. Assuming you're in the setup hook you can get one via app.handle().

#

This way you'll also have to worry less about lifetimes/borrows