Skip to content

Commit 303c081

Browse files
bors[bot]Mubelotix
andauthored
Merge #120
120: Type flexibility improvements r=curquiza a=Mubelotix [Rust 1.51 is stable!](https://blog.rust-lang.org/2021/03/25/Rust-1.51.0.html) That allows us to build much more powerful traits using const generics so that this library can handle a huge number of types! Co-authored-by: Mubelotix <[email protected]>
2 parents 287d44a + 2516485 commit 303c081

File tree

5 files changed

+62
-36
lines changed

5 files changed

+62
-36
lines changed

.code-samples.meilisearch.yaml

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ update_settings_1: |-
7373
synonyms.insert(String::from("logan"), vec!["wolverine"]);
7474
7575
let settings = Settings::new()
76-
.with_ranking_rules(&[
76+
.with_ranking_rules([
7777
"typo",
7878
"words",
7979
"proximity",
@@ -82,24 +82,24 @@ update_settings_1: |-
8282
"exactness",
8383
"desc(release_date)",
8484
"desc(rank)"
85-
][..])
85+
])
8686
.with_distinct_attribute("movie_id")
87-
.with_searchable_attributes(&[
87+
.with_searchable_attributes([
8888
"title",
8989
"description",
9090
"genre"
91-
][..])
92-
.with_displayed_attributes(&[
91+
])
92+
.with_displayed_attributes([
9393
"title",
9494
"description",
9595
"genre",
9696
"release_date"
97-
][..])
98-
.with_stop_words(&[
97+
])
98+
.with_stop_words([
9999
"the",
100100
"a",
101101
"an"
102-
][..])
102+
])
103103
.with_synonyms(synonyms);
104104
105105
let progress: Progress = movies.set_settings(&settings).await.unwrap();
@@ -120,7 +120,7 @@ get_stop_words_1: |-
120120
let stop_words: Vec<String> = movies.get_stop_words().await.unwrap();
121121
update_stop_words_1: |-
122122
let stop_words = ["of", "the", "to"];
123-
let progress: Progress = movies.set_stop_words(&stop_words[..]).await.unwrap();
123+
let progress: Progress = movies.set_stop_words(&stop_words).await.unwrap();
124124
reset_stop_words_1: |-
125125
let progress: Progress = movies.reset_stop_words().await.unwrap();
126126
get_ranking_rules_1: |-
@@ -137,7 +137,7 @@ update_ranking_rules_1: |-
137137
"desc(rank)",
138138
];
139139
140-
let progress: Progress = movies.set_ranking_rules(&ranking_rules[..]).await.unwrap();
140+
let progress: Progress = movies.set_ranking_rules(&ranking_rules).await.unwrap();
141141
reset_ranking_rules_1: |-
142142
let progress: Progress = movies.reset_ranking_rules().await.unwrap();
143143
get_distinct_attribute_1: |-
@@ -155,7 +155,7 @@ update_searchable_attributes_1: |-
155155
"genre"
156156
];
157157
158-
let progress: Progress = movies.set_searchable_attributes(&searchable_attributes[..]).await.unwrap();
158+
let progress: Progress = movies.set_searchable_attributes(&searchable_attributes).await.unwrap();
159159
reset_searchable_attributes_1: |-
160160
let progress: Progress = movies.reset_searchable_attributes().await.unwrap();
161161
get_attributes_for_faceting_1: |-
@@ -166,7 +166,7 @@ update_attributes_for_faceting_1: |-
166166
"director"
167167
];
168168
169-
let progress: Progress = movies.set_attributes_for_faceting(&attributes_for_faceting[..]).await.unwrap();
169+
let progress: Progress = movies.set_attributes_for_faceting(&attributes_for_faceting).await.unwrap();
170170
reset_attributes_for_faceting_1: |-
171171
let progress: Progress = movies.reset_attributes_for_faceting().await.unwrap();
172172
get_displayed_attributes_1: |-
@@ -179,7 +179,7 @@ update_displayed_attributes_1: |-
179179
"release_date"
180180
];
181181
182-
let progress: Progress = movies.set_displayed_attributes(&displayed_attributes[..]).await.unwrap();
182+
let progress: Progress = movies.set_displayed_attributes(&displayed_attributes).await.unwrap();
183183
reset_displayed_attributes_1: |-
184184
let progress: Progress = movies.reset_displayed_attributes().await.unwrap();
185185
get_index_stats_1: |-
@@ -201,7 +201,7 @@ field_properties_guide_searchable_1: |-
201201
"genre"
202202
];
203203
204-
let progress: Progress = movies.set_searchable_attributes(&searchable_attributes[..]).await.unwrap();
204+
let progress: Progress = movies.set_searchable_attributes(&searchable_attributes).await.unwrap();
205205
field_properties_guide_displayed_1: |-
206206
let displayed_attributes = [
207207
"title",
@@ -210,7 +210,7 @@ field_properties_guide_displayed_1: |-
210210
"release_date"
211211
];
212212
213-
let progress: Progress = movies.set_displayed_attributes(&displayed_attributes[..]).await.unwrap();
213+
let progress: Progress = movies.set_displayed_attributes(&displayed_attributes).await.unwrap();
214214
filtering_guide_1: |-
215215
let results: SearchResults<Movie> = movies.search()
216216
.with_query("Avengers")
@@ -321,9 +321,9 @@ settings_guide_synonyms_1: |-
321321
let tops: Index = client.get_index("tops").await.unwrap();
322322
let progress: Progress = tops.set_synonyms(&synonyms).await.unwrap();
323323
settings_guide_stop_words_1: |-
324-
let progress: Progress = movies.set_stop_words(&["the", "a", "an"][..]).await.unwrap();
324+
let progress: Progress = movies.set_stop_words(["the", "a", "an"]).await.unwrap();
325325
settings_guide_attributes_for_faceting_1: |-
326-
let progress: Progress = movies.set_attributes_for_faceting(&["director", "genres"][..]).await.unwrap();
326+
let progress: Progress = movies.set_attributes_for_faceting(["director", "genres"]).await.unwrap();
327327
settings_guide_ranking_rules_1: |-
328328
let ranking_rules = [
329329
"typo",
@@ -336,7 +336,7 @@ settings_guide_ranking_rules_1: |-
336336
"desc(rank)",
337337
];
338338
339-
let progress: Progress = movies.set_ranking_rules(&ranking_rules[..]).await.unwrap();
339+
let progress: Progress = movies.set_ranking_rules(&ranking_rules).await.unwrap();
340340
settings_guide_distinct_1: |-
341341
let jackets: Index = client.get_index("jackets").await.unwrap();
342342
let progress: Progress = jackets.set_distinct_attribute("product_id").await.unwrap();
@@ -347,7 +347,7 @@ settings_guide_searchable_1: |-
347347
"genre"
348348
];
349349
350-
let progress: Progress = movies.set_searchable_attributes(&searchable_attributes[..]).await.unwrap();
350+
let progress: Progress = movies.set_searchable_attributes(&searchable_attributes).await.unwrap();
351351
settings_guide_displayed_1: |-
352352
let displayed_attributes = [
353353
"title",
@@ -356,7 +356,7 @@ settings_guide_displayed_1: |-
356356
"release_date"
357357
];
358358
359-
let progress: Progress = movies.set_displayed_attributes(&displayed_attributes[..]).await.unwrap();
359+
let progress: Progress = movies.set_displayed_attributes(&displayed_attributes).await.unwrap();
360360
documents_guide_add_movie_1: |-
361361
// Define the type of our documents
362362
#[derive(Serialize, Deserialize, Debug)]
@@ -500,7 +500,7 @@ getting_started_search_md: |-
500500
501501
[About this SDK](https://github.com/meilisearch/meilisearch-rust/)
502502
faceted_search_update_settings_1: |-
503-
let progress: Progress = movies.set_attributes_for_faceting(&["director", "genres"][..]).await.unwrap();
503+
let progress: Progress = movies.set_attributes_for_faceting(["director", "genres"]).await.unwrap();
504504
faceted_search_facet_filters_1: |-
505505
let results: SearchResults<Movie> = movies.search()
506506
.with_query("thriller")
@@ -524,7 +524,7 @@ faceted_search_walkthrough_attributes_for_faceting_1: |-
524524
"production_companies"
525525
];
526526
527-
let progress: Progress = movies.set_attributes_for_faceting(&attributes_for_faceting[..]).await.unwrap();
527+
let progress: Progress = movies.set_attributes_for_faceting(&attributes_for_faceting).await.unwrap();
528528
faceted_search_walkthrough_facet_filters_1: |-
529529
let results: SearchResults<Movie> = movies.search()
530530
.with_query("thriller")

src/indexes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ impl<'a> Index<'a> {
661661
/// # sleep(Duration::from_secs(1));
662662
///
663663
/// let status = movies.get_all_updates().await.unwrap();
664-
/// assert_eq!(status.len(), 2);
664+
/// assert!(status.len() >= 2);
665665
/// # client.delete_index("movies_get_all_updates").await.unwrap();
666666
/// # });
667667
/// ```

src/progress.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ mod test {
262262
#[async_test]
263263
async fn test_wait_for_pending_updates_with_args() {
264264
let client = Client::new("http://localhost:7700", "masterKey");
265-
let movies = client.create_index("movies_wait_for_pending_args", None).await.unwrap();
265+
let movies = client.get_or_create("movies_wait_for_pending_args").await.unwrap();
266266
let progress = movies.add_documents(&[
267267
Document {
268268
id: 0,
@@ -286,7 +286,7 @@ mod test {
286286
#[async_test]
287287
async fn test_wait_for_pending_updates_time_out() {
288288
let client = Client::new("http://localhost:7700", "masterKey");
289-
let movies = client.create_index("movies_wait_for_pending_timeout", None).await.unwrap();
289+
let movies = client.get_or_create("movies_wait_for_pending_timeout").await.unwrap();
290290
let progress = movies.add_documents(&[
291291
Document {
292292
id: 0,

src/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ mod tests {
322322
Document { id: 8, kind: "title".into(), value: "Harry Potter and the Half-Blood Prince".to_string() },
323323
Document { id: 9, kind: "title".into(), value: "Harry Potter and the Deathly Hallows".to_string() },
324324
], None).await.unwrap();
325-
index.set_attributes_for_faceting(&["kind"][..]).await.unwrap();
325+
index.set_attributes_for_faceting(["kind"]).await.unwrap();
326326
sleep(Duration::from_secs(1));
327327
index
328328
}

src/settings.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{indexes::Index, errors::Error, request::{request, Method}, progress:
1010
/// ```
1111
/// # use meilisearch_sdk::settings::Settings;
1212
/// let settings = Settings::new()
13-
/// .with_stop_words(&["a", "the", "of"][..]);
13+
/// .with_stop_words(["a", "the", "of"]);
1414
///
1515
/// // OR
1616
///
@@ -107,9 +107,6 @@ impl IntoVecString for &[&String] {
107107
}
108108
}
109109

110-
/*
111-
TODO: Implement IntoVecString trought const generics as soon as they are stable.
112-
113110
impl<const N: usize> IntoVecString for &[String; N] {
114111
fn convert(self) -> Vec<String> {
115112
let mut vec = Vec::new();
@@ -119,7 +116,36 @@ impl<const N: usize> IntoVecString for &[String; N] {
119116
vec
120117
}
121118
}
122-
*/
119+
120+
impl<const N: usize> IntoVecString for &[&str; N] {
121+
fn convert(self) -> Vec<String> {
122+
let mut vec = Vec::new();
123+
for item in self {
124+
vec.push((*item).to_string())
125+
}
126+
vec
127+
}
128+
}
129+
130+
impl<const N: usize> IntoVecString for [String; N] {
131+
fn convert(self) -> Vec<String> {
132+
let mut vec = Vec::new();
133+
for item in self.iter() {
134+
vec.push((*item).clone())
135+
}
136+
vec
137+
}
138+
}
139+
140+
impl<const N: usize> IntoVecString for [&str; N] {
141+
fn convert(self) -> Vec<String> {
142+
let mut vec = Vec::new();
143+
for item in self.iter() {
144+
vec.push((*item).to_string())
145+
}
146+
vec
147+
}
148+
}
123149

124150
#[allow(missing_docs)]
125151
impl Settings {
@@ -411,7 +437,7 @@ impl<'a> Index<'a> {
411437
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
412438
///
413439
/// let stop_words = ["the", "of", "to"];
414-
/// let progress = movie_index.set_stop_words(&stop_words[..]).await.unwrap();
440+
/// let progress = movie_index.set_stop_words(&stop_words).await.unwrap();
415441
/// # std::thread::sleep(std::time::Duration::from_secs(2));
416442
/// # progress.get_status().await.unwrap();
417443
/// # });
@@ -446,7 +472,7 @@ impl<'a> Index<'a> {
446472
/// "asc(release_date)",
447473
/// "desc(rank)",
448474
/// ];
449-
/// let progress = movie_index.set_ranking_rules(&ranking_rules[..]).await.unwrap();
475+
/// let progress = movie_index.set_ranking_rules(ranking_rules).await.unwrap();
450476
/// # std::thread::sleep(std::time::Duration::from_secs(2));
451477
/// # progress.get_status().await.unwrap();
452478
/// # });
@@ -472,7 +498,7 @@ impl<'a> Index<'a> {
472498
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
473499
///
474500
/// let attributes_for_faceting = ["genre", "director"];
475-
/// let progress = movie_index.set_attributes_for_faceting(&attributes_for_faceting[..]).await.unwrap();
501+
/// let progress = movie_index.set_attributes_for_faceting(&attributes_for_faceting).await.unwrap();
476502
/// # std::thread::sleep(std::time::Duration::from_secs(2));
477503
/// # progress.get_status().await.unwrap();
478504
/// # });
@@ -522,7 +548,7 @@ impl<'a> Index<'a> {
522548
/// let client = Client::new("http://localhost:7700", "masterKey");
523549
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
524550
///
525-
/// let progress = movie_index.set_searchable_attributes(&["title", "description", "uid"][..]).await.unwrap();
551+
/// let progress = movie_index.set_searchable_attributes(["title", "description", "uid"]).await.unwrap();
526552
/// # std::thread::sleep(std::time::Duration::from_secs(2));
527553
/// # progress.get_status().await.unwrap();
528554
/// # });
@@ -547,7 +573,7 @@ impl<'a> Index<'a> {
547573
/// let client = Client::new("http://localhost:7700", "masterKey");
548574
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
549575
///
550-
/// let progress = movie_index.set_displayed_attributes(&["title", "description", "release_date", "rank", "poster"][..]).await.unwrap();
576+
/// let progress = movie_index.set_displayed_attributes(["title", "description", "release_date", "rank", "poster"]).await.unwrap();
551577
/// # std::thread::sleep(std::time::Duration::from_secs(2));
552578
/// # progress.get_status().await.unwrap();
553579
/// # });

0 commit comments

Comments
 (0)