Does anybody tried to set **tauri::App** type to a global variable?

If it is possiable,there will easily call notification function by rust code, when program initialize error,we can tell user friendly.

static mut APP: Option<&mut tauri::App> = None;

pub fn set_app()-> Option<&'static mut tauri::App> {
    let app = tauri::Builder::default()
        .build(tauri::generate_context!())
        .expect("error while building tauri application");

    let c = Box::new(app);
    return Some(Box::leak(c));
}

fn main() {
    let A = set_app().unwrap();
    unsafe {
        //
        // This line wil cause error :
        // cannot move out of `*A` which is behind a mutable reference
        // move occurs because `*A` has type `tauri::App`, which does not implement the `Copy` trait"
        //
        // But I change A.run to *A.run,it not works
        //
        A.run(|_app_handle, event| match event {
            tauri::RunEvent::ExitRequested { api, .. } => {
                api.prevent_exit();
            }
            _ => {}
        });
    }
}

Thank you

first you're not using the static global you declared.

second why are you leaking the box (also why does set_app return an Option if it ever only returns Some)?

and third, the error you're getting is because A is of type &mut App, and App::run takes ownership, which can't be done through &mut.

1 Like