projectfluent / fluent-rs

Rust implementation of Project Fluent
https://projectfluent.org
Apache License 2.0
1.08k stars 97 forks source link

Use of &str in FluentArgs #171

Closed JohnnyJayJay closed 4 years ago

JohnnyJayJay commented 4 years ago

As mentioned in #170 , I am currently writing Java bindings for this project.

There is another issue I encountered: the use of &str as a key type in FluentArgs makes insertion of strings from other scopes impossible without leaking memory. For example, this is not possible, because param is dropped:

fn put(args: &mut FluentArgs, param: String, value: FluentValue) {
    args.insert(&param, value);
}

This results in the fact that you can essentially only use literals as arguments for that method, unless you leak memory to make a &'static str.

My suggestion would be to use a different key type, such as Cow<str> or String. This would certainly be quite a breaking change, so I'm open to ideas how to fix this on the spot.

This issue may not only be relevant for my specific use case, I think.

JohnnyJayJay commented 4 years ago

I was just made aware of #123 and its corresponding PR. While that doesn't fix it for me, the discussion there gave me the idea to reduce the lifetime of FluentArgs only to the scope where they are needed. It still requires me to go through a fair amount of boiler plate to obtain a HashMap<&str, FluentValue>, but it works now.