ajrcarey / pdfium-render

A high-level idiomatic Rust wrapper around Pdfium, the C++ PDF library used by the Google Chromium project.
https://crates.io/crates/pdfium-render
Other
364 stars 59 forks source link

Question: How to use in another thread #161

Closed smw-wagnerma closed 2 months ago

smw-wagnerma commented 2 months ago

I can't figure out, how to use this library with the tokio runtime. I have to put this library in another thread (for a tauri app). But I always get these kind of errros: (dyn PdfiumLibraryBindings + 'static)` cannot be shared between threads safely the trait `Sync` is not implemented for `(dyn PdfiumLibraryBindings + 'static)`, which is required by `LazyLock<pdfium_render::prelude::Pdfium>: Sync` required for `Unique<(dyn PdfiumLibraryBindings + 'static)>` to implement `Sync` required for `LazyLock<pdfium_render::prelude::Pdfium>` to implement `Sync` shared static variables must have a type that implements `Sync In the thread_safe exampe, there seems no problems, but it uses the rayon runtime. With the tokio runtime it seems not possible. Is this right?

smw-wagnerma commented 2 months ago

Sorry, I figuerd it out, what was wrong... it was my fault, not your nice crate, sorry!

ajrcarey commented 2 months ago

Hi @smw-wagnerma , no problem, glad you were able to figure it out. What was the solution? I was going to ask if you had enabled the sync feature.

smw-wagnerma commented 1 month ago

Hi @ajrcarey , well the sync feature helps a little bit, but I am struggling with the correct lifetime handling in my environment. I have a Tauri app, where the pdfium lib should be loaded once, and there is one active PDF that is opend and worked with. I try to manage this with the state handling of Tauri https://v2.tauri.app/develop/state-management/ , so I can simply access the active PDF from UI commands. But I struggle to open a PDF document and store it in the state mangement of the Tauri App. You can look at https://github.com/smarwag/pdfium_test.git

ajrcarey commented 1 month ago

Hi @smw-wagnerma , I cannot help you with general application development. But I would say that if you are having trouble using Tauri's state management, you can simply ignore it and use something like OnceCell directly. Take a look at https://github.com/ajrcarey/pdfium-render/issues/94. Other people have gotten this to work, so it's definitely possible.