XAMPPRocky / fluent-templates

Easily add Fluent to your Rust project.
Apache License 2.0
136 stars 28 forks source link

Loader cannot be made into an object #39

Open tmpfs opened 1 year ago

tmpfs commented 1 year ago

Hi,

I am trying to update this dependency from 0.5.16 to 0.8 and I reference the Loader trait in a struct field:

/// Lookup a language string in the underlying loader.
pub struct FluentHelper {
    loader: Box<dyn Loader + Send + Sync>,
    /// Escape messages, default is `true`.
    pub escape: bool,
}

Now I am unable to use the trait boxed due to this compiler error:

error[E0038]: the trait `Loader` cannot be made into an object
  --> src/lib.rs:95:17
   |
95 |     loader: Box<dyn Loader + Send + Sync>,
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Loader` cannot be made into an object
   |
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
  --> /Users/muji/.cargo/registry/src/github.com-1ecc6299db9ec823/fluent-templates-0.8.0/src/loader.rs:35:8
   |
35 |     fn lookup_with_args<T: AsRef<str>>(
   |        ^^^^^^^^^^^^^^^^ the trait cannot be made into an object because method `lookup_with_args` has generic type parameters
...
45 |     fn lookup_complete<T: AsRef<str>>(
   |        ^^^^^^^^^^^^^^^ the trait cannot be made into an object because method `lookup_complete` has generic type parameters

I think it may be ok for Loader to just accept &str, I wonder what you think?

XAMPPRocky commented 1 year ago

Thank you for your issue! That it is an unfortunate regression. I think we can just replace it with &dyn AsRef<str>.