Skip to content

Commit 8236a27

Browse files
cmyrzbraniecki
authored andcommitted
Use AsRef as bounds in negotiation (#15)
1 parent ed1309e commit 8236a27

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

src/negotiate/mod.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -130,27 +130,21 @@ pub enum NegotiationStrategy {
130130
Lookup,
131131
}
132132

133-
pub fn filter_matches<
134-
'a,
135-
R: 'a + Into<LanguageIdentifier> + Clone,
136-
A: 'a + Into<LanguageIdentifier> + Clone,
137-
>(
138-
requested: impl IntoIterator<Item = &'a R>,
139-
available: impl IntoIterator<Item = &'a A>,
133+
pub fn filter_matches<'a, R: 'a + AsRef<LanguageIdentifier>, A: 'a + AsRef<LanguageIdentifier>>(
134+
requested: &[R],
135+
available: &'a [A],
140136
strategy: NegotiationStrategy,
141137
) -> Vec<&'a A> {
142138
let mut supported_locales = vec![];
143139

144-
let mut av_map: HashMap<LanguageIdentifier, &'a A> = HashMap::new();
140+
let mut av_map: HashMap<&'a LanguageIdentifier, &'a A> = HashMap::new();
145141

146142
for av in available.into_iter() {
147-
av_map.insert(av.clone().into(), av);
143+
av_map.insert(av.as_ref(), av);
148144
}
149145

150-
let req_langids: Vec<LanguageIdentifier> =
151-
requested.into_iter().map(|a| a.clone().into()).collect();
152-
153-
for req in req_langids {
146+
for req in requested {
147+
let req = req.as_ref();
154148
if req.get_language() == "und" {
155149
continue;
156150
}
@@ -206,7 +200,7 @@ pub fn filter_matches<
206200
match_found = false;
207201

208202
// 3) Try to match against a maximized version of the requested locale
209-
let mut req = if let Some(req) = likely_subtags::add(&req) {
203+
let mut req = if let Some(req) = likely_subtags::add(req) {
210204
av_map.retain(|key, value| {
211205
if strategy != NegotiationStrategy::Filtering && match_found {
212206
return true;
@@ -231,7 +225,7 @@ pub fn filter_matches<
231225
match_found = false;
232226
req
233227
} else {
234-
req
228+
req.to_owned()
235229
};
236230

237231
// 4) Try to match against a variant as a range
@@ -315,22 +309,22 @@ pub fn filter_matches<
315309

316310
pub fn negotiate_languages<
317311
'a,
318-
R: 'a + Into<LanguageIdentifier> + Clone,
319-
A: 'a + Into<LanguageIdentifier> + PartialEq + Clone,
312+
R: 'a + AsRef<LanguageIdentifier>,
313+
A: 'a + AsRef<LanguageIdentifier>,
320314
>(
321-
requested: impl IntoIterator<Item = &'a R>,
322-
available: impl IntoIterator<Item = &'a A>,
315+
requested: &[R],
316+
available: &'a [A],
323317
default: Option<&'a A>,
324318
strategy: NegotiationStrategy,
325-
) -> Vec<&A> {
319+
) -> Vec<&'a A> {
326320
let mut supported = filter_matches(requested, available, strategy);
327321

328322
if let Some(default) = default {
329323
if strategy == NegotiationStrategy::Lookup {
330324
if supported.is_empty() {
331325
supported.push(default);
332326
}
333-
} else if !supported.contains(&default) {
327+
} else if !supported.iter().any(|s| s.as_ref() == default.as_ref()) {
334328
supported.push(default);
335329
}
336330
}

tests/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ fn locale_matching() {
138138

139139
assert_eq!(
140140
negotiate_languages(
141-
vec![&loc_en_us, &loc_de_at],
142-
vec![&loc_pl, &loc_de, &loc_en],
141+
&[loc_en_us, loc_de_at],
142+
&[loc_pl, loc_de.clone(), loc_en.clone()],
143143
None,
144144
NegotiationStrategy::Matching
145145
),

0 commit comments

Comments
 (0)