rusttauri

The method blocking_king is not unsatfied with #[tauri::command]?


I'm having big problems with this error, I don't understand what it wants exactly. The error says: method cannot be called on &option<person> due to unsatisfied trait bounds. When I paste my person.rs code into lib.rs code, I get the same error and I don't understand what exactly is meant by &Option<Person>? What have I overlooked?

Code example:

person.rs

#[derive(Clone)]
pub struct Person {
    pub firstname: String,
    pub lastname: String,
    pub age: u8
}

impl Person {
    pub fn new(firstname: String, lastname: String, age: u8) -> Person {
        Person{firstname, lastname, age}
    }
}

lib.rs

use std::sync::Mutex;

mod person;

static CUSTOMER: Mutex<Option<person::Person>> = Mutex::new(None);

#[tauri::command]
fn add_customer() -> () {
    *CUSTOMER.lock().unwrap() = Some(person::Person {
        firstname: String::from("Firstname"),
        lastname: String::from("Lastname"),
        age: 72
    });
}


#[tauri::command]  // Error: method cannot be called on `&Option<Person>` due to unsatisfied trait bounds
fn get_customer() -> Option<person::Person> {
    return CUSTOMER.lock().unwrap().clone();
}


#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_shell::init())
        .invoke_handler(tauri::generate_handler![
            add_customer
            get_customer,
         ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Error:

error[E0308]: mismatched types
  --> src\lib.rs:19:12
   |
18 | fn get_customer() -> Option<person::Person> {
   |                      ---------------------- expected `Option<Person>` because of return type
19 |     return CUSTOMER.lock().unwrap();
   |            ^^^^^^^^^^^^^^^^^^^^^^^^ expected `Option<Person>`, found `MutexGuard<'_, Option<Person>>`
   |
   = note: expected enum `Option<_>`
            found struct `MutexGuard<'_, Option<_>, >`

error[E0599]: the method `blocking_kind` exists for reference `&Option<Person>`, but its trait bounds were not satisfied
   --> src\lib.rs:17:1
    |
17  |   #[tauri::command]
    |   ^^^^^^^^^^^^^^^^^ method cannot be called on `&Option<Person>` due to unsatisfied trait bounds
...
28  |           .invoke_handler(tauri::generate_handler![
    |  _________________________-
29  | |             add_customer,
30  | |             get_customer
31  | |         ])
    | |_________- in this macro invocation
    |
   ::: C:\Users\user\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\option.rs:571:1
    |
571 |   pub enum Option<T> {
    |   ------------------ doesn't satisfy `Option<Person>: IpcResponse`
    |
    = note: the following trait bounds were not satisfied:
            `Option<Person>: IpcResponse`
            which is required by `&Option<Person>: tauri::ipc::private::ResponseKind`
    = note: this error originates in the macro `__cmd__get_customer` which comes from the expansion of the macro `tauri::generate_handler` (in Nightly builds, run with -Z macro-backtrace for more info)

Some errors have detailed explanations: E0308, E0599.
For more information about an error, try `rustc --explain E0308`.
error: could not compile `operation-robot` (lib) due to 2 previous errors

Solution

  • #[derive(Clone, serde::Serialize, serde::Deserialize)]
    pub struct Person {
        pub firstname: String,
        pub lastname: String,
        pub age: u8
    }
    

    It works, thank you very much!

    For those who are also struggle like me.