-
Notifications
You must be signed in to change notification settings - Fork 11
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
Use AsRef as bounds in negotiation #15
Conversation
Hmm, I like the approach! |
And I think this will work for impl AsRef<LanguageIdentifier> for Locale {
fn as_ref(&self) -> &LanguageIdentifier {
&self.langid
}
} |
Already there! https://github.com/zbraniecki/unic-locale/blob/master/unic-locale-impl/src/lib.rs#L136 I think this model works quite well for negotiation and other cases where you get something, operate on it as LanguageIdentifier, and then return filtered results etc. (negotiation use case). But for cases like struct FluentBundle {
langid: LanguageIdentifier
}
impl FluentBundle {
pub fn new(langid: AsRef<LanguageIdentifier>) -> Self {
LanguageIdentifier {
langid: langid.as_ref().to_owned()
}
}
}
struct Localization {
langids: Vec<LanguageIdentifier>
}
impl Localization {
pub fn new(langids: &[AsRef<LanguageIdentifier>]) -> Self {
LanguageIdentifier {
langids: langid.iter().map(|langid| langid.as_ref().to_owned()).collect()
}
}
} Is that how it would work? Then:
|
I think the idiomatic API for this would be to use Something to think about when designing APIs in Rust is to make allocations explicit. This is informally specified in a bunch of naming conventions: Anyway what I mean to say here: Similarly in general I don't think API should take a slice as an argument if it's going to immediately allocate a vec and copy all the items. In this particular situation, though, |
Another little thing I noticed looking at locale-impl; you don't need to implement edit: it's possible I misread your code and this is an external type, in which case carry on 😎 In case you end up using this approach I've fixed the signatures so you can now pass in a slice of owned values and everything just works. |
that looks great! Thank you! |
Looking through #14, I was left wondering if something like this wouldn't be a simpler way to get where you want to be?
(This is just intended as an illustration, not as a real patch.)