New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Arguments & ownership #190
Comments
I think the issue here is that you want to return from the In #179 we're talking about switching the keys to be |
@vpzomtrrfrt can you check if the current master solved it? We now accept |
I don't think this is resolved, format_pattern still returns a value bound to the lifetime of the args |
I see. The challenge here is that we're trying to be really light on how we handle allocations. That means that in theory, your
in which case we want to take For numbers, we perform formatting so in theory maybe we could tailor the lifetimes to not span to other If you are ok with owning the result, you can just do: let text = if num == 0 {
let pattern = bundle.get_message("items-select").unwrap().value.unwrap();
bundle.format_pattern(&pattern, None, &mut errors).into_owned()
} else {
let pattern = bundle.get_message("items-selected").unwrap().value.unwrap();
let args = std::iter::once(("num", num.into())).collect();
bundle.format_pattern(&pattern, Some(&args), &mut errors).into_owned()
}; or, you could do: let mut s = String::new();
let text = if num == 0 {
let pattern = bundle.get_message("items-select").unwrap().value.unwrap();
bundle.write_pattern(&mut s, &pattern, None, &mut errors)
} else {
let pattern = bundle.get_message("items-selected").unwrap().value.unwrap();
let args = std::iter::once(("num", num.into())).collect();
bundle.write_pattern(&mut s, &pattern, Some(&args), &mut errors)
};
println!("{}", s) would that resolve it for you? |
I guess this isn't actually a good example, since it always needs allocation. The real case I'm looking at is one with no arguments, and ideally I would expect that to return with the lifetime of the resource, while currently it requires the lifetime of the bundle Or maybe I'm just using bundles wrong? |
That sounds reasonable. Can you construct an example that you'd expect to work? I believe that if you pass no arguments and request a simple message that doesn't require any resolution then yes, it should return it with the lifetime of the resource that holds it.
I think you're not wrong. It's just tricky to get all lifetimes correctly :) |
let mut errors = Vec::new();
let text = {
let bundle: FluentBundle<&'static FluentResource> = get_bundle_somehow();
let pattern = bundle.get_message("hello").unwrap().value.unwrap();
bundle.format_pattern(&pattern, None, &mut errors)
};
println!("{}", text); I would hope to get the message with 'static in this case |
Ah, that's really tricky. Because if you have two messages, one that is a simple message from The way we resolve it is that we return with a lifetime of a bundle and the bundle lifetime is that of resource and args. I think the best way for such scenarios is to use |
into_owned is what I'm using currently, but I'd rather not. I guess this would need something like a three-valued Cow? |
that wouldn't even help, since it would still need the shortest of the lifetimes |
No, it would need to be flexible with which lifetime it needs because it depends on what the pattern needs. |
or actually I think a multi-cow could help, since it could have a method to convert it to a Cow of the larger lifetime by cloning if necessary |
Hmm, it might work. If you want to take a stab at PR, I'm happy to review. |
Here's a simplified version of what I'm looking at, based on the example from the Good Practices page:
This doesn't compile, because the lifetime requirement on args is returned to the block:
Is there something I'm missing here? How could this be made to work?
The text was updated successfully, but these errors were encountered: