xu-cheng / katex-rs

Rust bindings to KaTeX
https://docs.rs/katex
Apache License 2.0
110 stars 11 forks source link

Unable to use katex Opts when trait Sync is required #7

Closed deep110 closed 3 years ago

deep110 commented 3 years ago

First of all, thanks for this great crate.

I am encountering an error when using this where Sync is also required.

Detailed stacktrace:

| impl ParseBlock for EquationBlock {
|      ^^^^^^^^^^ `(dyn for<'r> std::ops::Fn(katex::TrustContext<'r>) -> bool + std::panic::RefUnwindSafe + 'static)` cannot be sent between threads safely
| 
::: ~/.cargo/registry/src/github.com-1ecc6299db9ec823/liquid-core-0.21.2/src/parser/block.rs:32:30
|
32 | pub trait ParseBlock: Send + Sync + ParseBlockClone {
|                              ---- required by this bound in `liquid_core::parser::block::ParseBlock`
|
= help: the trait `std::marker::Send` is not implemented for `(dyn for<'r> std::ops::Fn(katex::TrustContext<'r>) -> bool + std::panic::RefUnwindSafe + 'static)`
= note: required because of the requirements on the impl of `std::marker::Sync` for `std::sync::Arc<(dyn for<'r> std::ops::Fn(katex::TrustContext<'r>) -> bool + std::panic::RefUnwindSafe + 'static)>`
= note: required because it appears within the type `katex::TrustCallback`
= note: required because it appears within the type `std::option::Option<katex::TrustCallback>`
= note: required because it appears within the type `katex::Opts`
= note: required because it appears within the type `equation_block::EquationBlock`

error[E0277]: `(dyn for<'r> std::ops::Fn(katex::TrustContext<'r>) -> bool + std::panic::RefUnwindSafe + 'static)` cannot be shared between threads safely
--> src/equation_block.rs:56:6
|
56 | impl ParseBlock for EquationBlock {
|      ^^^^^^^^^^ `(dyn for<'r> std::ops::Fn(katex::TrustContext<'r>) -> bool + std::panic::RefUnwindSafe + 'static)` cannot be shared between threads safely
|
::: ~/.cargo/registry/src/github.com-1ecc6299db9ec823/liquid-core-0.21.2/src/parser/block.rs:32:30
|
32 | pub trait ParseBlock: Send + Sync + ParseBlockClone {
|                              ---- required by this bound in `liquid_core::parser::block::ParseBlock`
|
= help: the trait `std::marker::Sync` is not implemented for `(dyn for<'r> std::ops::Fn(katex::TrustContext<'r>) -> bool + std::panic::RefUnwindSafe + 'static)`
= note: required because of the requirements on the impl of `std::marker::Sync` for `std::sync::Arc<(dyn for<'r> std::ops::Fn(katex::TrustContext<'r>) -> bool + std::panic::RefUnwindSafe + 'static)>`
= note: required because it appears within the type `katex::TrustCallback`
= note: required because it appears within the type `std::option::Option<katex::TrustCallback>`
xu-cheng commented 3 years ago

Please show a minimum example to reproduce your issue. Thanks.

deep110 commented 3 years ago

@xu-cheng Sure,

You can try to create a katex::Opts struct in lazy_static

lazy_static! {
    static ref katex_opts: katex::Opts = katex::Opts::builder()
            .display_mode(true)
            .output_type(katex::OutputType::Html)
            .throw_on_error(false)
            .build()
            .unwrap();
}

You get the same stacktrace, since lazy_static also requires Sync trait to be implemented.

error[E0277]: `(dyn for<'r> std::ops::Fn(katex::TrustContext<'r>) -> bool + std::panic::RefUnwindSafe + 'static)` cannot be sent between threads safely
|
11 | / lazy_static! {
12 | |     static ref katex_opts: katex::Opts = katex::Opts::builder()
13 | |             .display_mode(true)
14 | |             .output_type(katex::OutputType::Html)
...  |
18 | |
19 | | }
| |_^ `(dyn for<'r> std::ops::Fn(katex::TrustContext<'r>) -> bool + std::panic::RefUnwindSafe + 'static)` cannot be sent between threads safely
pub struct Lazy<T: Sync>(Cell<Option<T>>, Once);
|                      ---- required by this bound in `lazy_static::lazy::Lazy`
|
= help: the trait `std::marker::Send` is not implemented for `(dyn for<'r> std::ops::Fn(katex::TrustContext<'r>) -> bool + std::panic::RefUnwindSafe + 'static)`