Accessing state from an async command (implicit elided lifetime not allowed here) #4317
Answered
by
FabianLars
alex-brook
asked this question in
Q&A
-
Hi guys. I'm trying to manage a sqlite connection in my app, but I get a lifetime error when I try accessing it in the same way I would a non-async command. tauri::Builder::default()
.manage(app_dir)
.manage(Mutex::new(connection)) // Mutex<SqliteConnection>
.invoke_handler(tauri::generate_handler!(create_todo))
.run(tauri::generate_context!())
.expect("error while running tauri application"); #[tauri::command]
async fn create_todo(state: tauri::State<Mutex<SqliteConnection>>) {
println!("Creating a todo");
// Run insert query
}
// implicit elided lifetime not allowed here
// note: assuming a `'static` lifetime...
// help: indicate the anonymous lifetime: `tauri::State<'_, Mutex<SqliteConnection>>`rustc(E0726) When I try and follow the compiler's suggestion I get a new error: #[tauri::command]
async fn create_todo(state: tauri::State<'_, Mutex<SqliteConnection>>) {
println!("Creating a todo");
// Run insert query
}
// `__tauri_message__` does not live long enough
// borrowed value does not live long enoughrustc(E0597)
// main.rs(61, 1): borrowed value does not live long enough
// main.rs(61, 17): `__tauri_message__` dropped here while still borrowed
// `__tauri_message__` does not live long enough
// argument requires that `__tauri_message__` is borrowed for `'static`rustc(E0597)
// main.rs(61, 1): argument requires that `__tauri_message__` is borrowed for `'static`
// main.rs(61, 17): `__tauri_message__` dropped here while still borrowed The first version compiles fine without the |
Beta Was this translation helpful? Give feedback.
Answered by
FabianLars
Jun 10, 2022
Replies: 1 comment 2 replies
-
This is a known issue/limitation. You need to return a Result from async stateful commands. Something as simple as |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
alex-brook
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#2533
This is a known issue/limitation. You need to return a Result from async stateful commands. Something as simple as
Result<(), ()>
should be enough.