tauri icon indicating copy to clipboard operation
tauri copied to clipboard

Compile error for async stateful commands that don't return a `Result`

Open probablykasper opened this issue 4 years ago • 3 comments

Describe the bug

Stateful async commands that don't return a Result give a compiler error.

To Reproduce

Example:

#[command]
pub async fn get_info(app: State<'_, Data>) -> bool {
  true
}

There's no error if you remove async, remove the app argument, or change the return type to a Result works.

Platform and Versions

Details
Operating System - Mac OS, version 10.15.7 X64

Node.js environment
  Node.js - 14.16.0
  @tauri-apps/cli - 1.0.0-beta.10
  @tauri-apps/api - 1.0.0-beta.8

Global packages
  npm - 6.14.11
  yarn - 1.22.10

Rust environment
  rustc - 1.54.0
  cargo - 1.54.0

App directory structure
/node_modules
/src-tauri
/build
/.git
/src

App
  tauri.rs - 1.0.0-beta.8
  build-type - bundle
  CSP - default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self'
  distDir - ../build
  devPath - http://localhost:8000
  framework - Svelte

Stack Trace

error[E0597]: `__tauri_message__` does not live long enough
  --> src/cmd.rs:93:1
   |
93 |   #[command]
   |   ^^^^^^^^^-
   |   |        |
   |   |        `__tauri_message__` dropped here while still borrowed
   |   borrowed value does not live long enough
   |   argument requires that `__tauri_message__` is borrowed for `'static`

probablykasper avatar Aug 25 '21 21:08 probablykasper

same problem

zsmatrix62 avatar Nov 07 '21 15:11 zsmatrix62

Is there any interim solution?

ricardomatias avatar Sep 19 '22 10:09 ricardomatias

For now you just have to return a Result. If you don't want to return anything from the command it can be as simple as Result<(), ()> or in case of the OP's example it'd be Result<bool, ()>.

This doesn't change what you'll receive on the JavaScript side. Ok will resolve the Promise and Err will reject it.

FabianLars avatar Sep 19 '22 10:09 FabianLars