如何从Rust模块重新导出Tauri命令函数?

huangapple go评论68阅读模式
英文:

How to re-export Tauri command function from Rust module?

问题

Sure, here's the translated content without the code parts:

Questions
我是Rust和Tauri的新手。我不确定为什么我无法正确地重新导出函数(pub use)并在tauri::generate_handler中使用它。

I'm not sure why pub use operation::add; not work as expect and cause

tauri::generate_handler!中出现了failed to resolve: could not find __cmd__addinutilcould not find__cmd__addinutil rustc。我错在哪里?

If you need any further assistance or have more specific questions, feel free to ask.

英文:

Questions
<br>
I'm new to Rust and Tauri. I'm not sure why I can't properly re-export function (pub use) and use it in tauri::generate_handler.

Code
<br>
src/main.rs

mod util;

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![util::add])
        .run(tauri::generate_context!())
        .expect(&quot;error while running tauri application&quot;);
}

src/util/mod.rs

mod operation;

// pub use operation::add; // This line not work as expected
pub use operation::*; // This line no problem

src/util/operation.rs

#[tauri::command]
pub fn add(a: i32, b: i32) -&gt; i32 {
    a + b
}

I'm not sure why pub use operation::add; not work as expect and cause

failed to resolve: could not find `__cmd__add` in `util`
could not find `__cmd__add` in `util` rustc

in tauri::generate_handler!. Where am I wrong?

答案1

得分: 1

问题在于 tauri::command 创建了一个名为 __cmd__add 的新符号,并且 tauri::generate_handler 使用该符号而不是(或者除了) add 本身。 当你 use operation::* 时,你会从 operation 导入所有内容,包括 __cmd__add。 但是当你 use operation::add 时,你只导入了 add

一个不太规范的解决方法是 use operation::{ add, __cmd__add },但是这并不被推荐,因为 __cmd__add 是一个私有的实现细节,不打算直接使用。

一个完整的修复需要对 Tauri 进行更改,但对你来说可能的一个解决方法是将 add 封装在一个模块中,这样 tauri::generate_handler 就会在该模块内找到 __cmd__add

  • src/util/operation.rs

    pub mod add {
        #[tauri::command]
        pub fn add(a: i32, b: i32) -> i32 {
            a + b
        }
    }
    pub use add::add;
    
  • src/util/mod.rs

    mod operation;
    pub use operation::add;
    
  • src/main.rs

    mod util;
    
    fn main() {
        tauri::Builder::default()
            // 请注意这里的双重 `add`:
            .invoke_handler(tauri::generate_handler![util::add::add])
            .run(tauri::generate_context!())
            .expect("运行 tauri 应用程序时出错");
    }
    

请注意,在调用 tauri::generate_handler 时,你需要显式引用位于 add 模块内的 add 函数,因此你需要使用 add::add。 但是当你想从你的 Rust 代码中调用该函数时,你可以直接这样做:let four = util::add(2, 2) 将正常工作。

英文:

The problem is that tauri::command creates a new symbol named __cmd__add and that tauri::generate_handler uses that symbol instead of (or in addition to) add itself. When you use operation::* you import everything from operation, including __cmd__add. But when you use operation::add you only import add.

A dirty workaround would be to use operation::{ add, __cmd__add }, but this is discouraged because __cmd__add is a private implementation detail, not intended to be used directly.

A full fix would require changes to Tauri, but a possible workaround for you is to wrap add in a module so that tauri::generate_handler will find __cmd__add inside that module:

  • In src/util/operation.rs:

    pub mod add {
        #[tauri::command]
        pub fn add(a: i32, b: i32) -&gt; i32 {
            a + b
        }
    }
    pub use add::add;
    
  • In src/util/mod.rs:

    mod operation;
    pub use operation::add;
    
  • In src/main.rs:

    mod util;
    
    fn main() {
        tauri::Builder::default()
            // Note the double `add` here:
            .invoke_handler(tauri::generate_handler![util::add::add])
            .run(tauri::generate_context!())
            .expect(&quot;error while running tauri application&quot;);
    }
    

Note that when calling tauri::generate_handler you need to refer explicitly to the add function that's inside the add module, so you need to use add::add. However when you want to call the function from your Rust code you can do it directly: let four = util::add (2, 2) will work fine.

huangapple
  • 本文由 发表于 2023年6月29日 11:50:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577953.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定