Skip to content
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

No way to own the result of negotiate_languages() #21

Open
alerque opened this issue Apr 30, 2020 · 1 comment
Open

No way to own the result of negotiate_languages() #21

alerque opened this issue Apr 30, 2020 · 1 comment

Comments

@alerque
Copy link
Collaborator

alerque commented Apr 30, 2020

Pardon me if this is a Rust newbie question (I am one), but I'm struggling to implement this library in my app.

Very roughly I want to do some negotiation up front and decide on a language fallback stack, then retain that in an immutable struct for the lifetime of the app (a CLI tool). I don't have any problem with this config struct otherwise, it's working and I can even put some language information in it. For example I can use this libraries accepted_languages::parse() and get an owned result back (Vec<LanguageIdentifier>) which I can easily keep in my struct. The issue I have is there seems to be no way to use negotiate_languages() and get back something owned by the calling function. It always returns Vec<&LanguageIdentifier> (which I can't retain in my struct).

Shouldn't there be a built in method that returns something that can be owned by the parent scope?

@alerque
Copy link
Collaborator Author

alerque commented Apr 30, 2020

After nearly a day of monkeying with it right after asking this I've finally managed to hack my way around this problem by rebuilding everything in the returned vector with clones:

let negotiated = negotiate_languages(
      &requested,
      &available,
      Some(&default),
      NegotiationStrategy::Filtering,
).iter().map(|x| *x).cloned().collect();

I can now stuff that into my struct and return it:

Locales {
    negotiated
}

That .iter().map(|x| *x).cloned().collect() business seems like it should be unnecessary, but the negotiate_languages() function does not allow passing ownership of any of the inputs, nor does it generate copies on the output. There is probably an idiomatic Rust way of handling this that I'm missing, but the docs are sparse and it seems to kind of railroad a specific usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant