Giter Club home page Giter Club logo

Comments (5)

ChurchTao avatar ChurchTao commented on August 22, 2024

@StringKe
tauri 中,应该可以保存到状态对象中吧,我刚刚写了个 demo 测试了一下,你可以看一下,不过我会思考能不能实现 copy ,目前无法保证这个对象在所有平台都线程安全。

https://github.com/ChurchTao/clipboard-rs-tauri-demo

from clipboard-rs.

StringKe avatar StringKe commented on August 22, 2024

我是这样写的 tauri 2 beta,还是无法移动它

https://github.com/stringke-org/qingmu/blob/main/src-tauri/src/commands.rs#L26

from clipboard-rs.

ChurchTao avatar ChurchTao commented on August 22, 2024

我是这样写的 tauri 2 beta,还是无法移动它

https://github.com/stringke-org/qingmu/blob/main/src-tauri/src/commands.rs#L26

@StringKe 我知道你的困惑了,你的代码中:
let clipboard_context = &state.0.lock().unwrap().clipboard_context;
这样做是试图把 对象从 state 中 move 出来,这是移动所有权的做法,这里 move 出来了,state 就会丢失所有权,这在 rust 中是不允许的,你可以改为,使用 & 创建一个不可变引用,在函数 get_clipboard_text 中这个不可变引用足以完成所有工作,并且不会额外内存开销,state 也不会因此丢失所有权。

示例代码如下:

#[tauri::command]
pub fn get_clipboard_text(state: tauri::State<'_, AppState>) -> Result<String> {
    // 无法移动
    let clipboard_context = &state.0.lock().unwrap().clipboard_context;
    match clipboard_context.get_text() {
        Ok(str) => {
            if str.is_empty() {
                Err(Error::from(anyhow::anyhow!("Clipboard text is empty")))
            } else {
                Ok(str)
            }
        }
        Err(_) => Err(Error::from(anyhow::anyhow!("Failed to get clipboard text"))),
    }
}

from clipboard-rs.

ChurchTao avatar ChurchTao commented on August 22, 2024

具体看下这个
https://kaisery.github.io/trpl-zh-cn/ch04-02-references-and-borrowing.html

from clipboard-rs.

ChurchTao avatar ChurchTao commented on August 22, 2024

应该没问题了吧,有问题重新打开这个issue即可

from clipboard-rs.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.