Skip to content

Commit af3b363

Browse files
bors[bot]ppamorim
andauthored
Merge #132
132: Remove lifetimes r=curquiza a=ppamorim In this large PR, I removed the requirement to use explicit lifetimes. This sorts the issue reported #127. Please let me know if this PR removed any recent modifications in the code. Co-authored-by: Pedro Paulo de Amorim <[email protected]>
2 parents 0603945 + 8c9fa49 commit af3b363

File tree

13 files changed

+206
-180
lines changed

13 files changed

+206
-180
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ jobs:
5555
- name: Build
5656
run: |
5757
rustup target add wasm32-unknown-unknown
58-
cargo check --example web_app --target wasm32-unknown-unknown
58+
cargo check --example web_app --target wasm32-unknown-unknown --features=sync

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ wasm-bindgen-futures = "0.4"
2626
[features]
2727
default = ["isahc-static-curl"]
2828
isahc-static-curl = ["isahc/static-curl"]
29+
sync = []
2930

3031
[dev-dependencies]
3132
env_logger = "0.8"
@@ -36,10 +37,12 @@ futures = "0.3"
3637
wasm-bindgen = "0.2"
3738
wasm-bindgen-futures = "0.4"
3839
yew = "0.18"
40+
lazy_static = "1.4"
3941
web-sys = "0.3"
4042
console_error_panic_hook = "0.1"
4143

4244
[[example]]
4345
name = "web_app"
4446
crate-type = ["cdylib", "rlib"]
47+
required-features = ["sync"]
4548
path = "examples/web_app/src/lib.rs"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ serde = { version = "1.0", features = ["derive"] }
6060
```
6161

6262
This crate is `async` but you can choose to use an async runtime like [tokio](https://crates.io/crates/tokio) or just [block on futures](https://docs.rs/futures/latest/futures/executor/fn.block_on.html).
63+
You can enable the `sync` feature to make most structs `Sync`. It may be a bit slower.
6364

6465
Using this crate is possible without [serde](https://crates.io/crates/serde), but a lot of features require serde.
6566

examples/web_app/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ wasm-bindgen = "0.2"
1212
wasm-bindgen-futures = "0.4.18"
1313
yew = "0.18"
1414
meilisearch-sdk = {path="../.."}
15+
lazy_static = "1.4"
1516
serde = {version="1.0", features=["derive"]}
1617
web-sys = "0.3"
1718
console_error_panic_hook = "0.1"

examples/web_app/src/document.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ impl Document for Crate {
2323
}
2424

2525
impl Crate {
26+
2627
pub fn get_readable_download_count(&self) -> String {
2728
if let Some(downloads) = self.downloads {
2829
if downloads < 1000 {

examples/web_app/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ use std::rc::Rc;
88
use wasm_bindgen::prelude::*;
99
use wasm_bindgen_futures::spawn_local;
1010
use yew::prelude::*;
11+
use lazy_static::lazy_static;
1112

1213
mod document;
1314
use crate::document::Crate;
1415

15-
// We need a static client because yew's component trait does not allow lifetimes shorter than static
16-
pub static CLIENT: Client = Client::new(
17-
"https://finding-demos.meilisearch.com",
18-
"2b902cce4f868214987a9f3c7af51a69fa660d74a785bed258178b96e3480bb3",
19-
);
16+
lazy_static! {
17+
static ref CLIENT: Client = Client::new(
18+
"https://finding-demos.meilisearch.com",
19+
"2b902cce4f868214987a9f3c7af51a69fa660d74a785bed258178b96e3480bb3",
20+
);
21+
}
2022

2123
struct Model {
2224
link: Rc<ComponentLink<Self>>,
23-
index: Rc<Index<'static>>, // The lifetime of Index is the lifetime of the client
25+
index: Rc<Index>,
2426
results: Vec<Crate>,
2527
processing_time_ms: usize,
2628

src/client.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use crate::{errors::*, indexes::*, request::*};
1+
use crate::{errors::*, indexes::*, request::*, Rc};
22
use serde_json::{json, Value};
33
use serde::{Deserialize};
44
use std::collections::HashMap;
55

66
/// The top-level struct of the SDK, representing a client containing [indexes](../indexes/struct.Index.html).
77
#[derive(Debug)]
8-
pub struct Client<'a> {
9-
pub(crate) host: &'a str,
10-
pub(crate) apikey: &'a str,
8+
pub struct Client {
9+
pub(crate) host: Rc<String>,
10+
pub(crate) api_key: Rc<String>,
1111
}
1212

13-
impl<'a> Client<'a> {
13+
impl Client {
1414
/// Create a client using the specified server.
1515
/// Don't put a '/' at the end of the host.
1616
/// In production mode, see [the documentation about authentication](https://docs.meilisearch.com/reference/features/authentication.html#authentication).
@@ -22,8 +22,11 @@ impl<'a> Client<'a> {
2222
/// // create the client
2323
/// let client = Client::new("http://localhost:7700", "masterKey");
2424
/// ```
25-
pub const fn new(host: &'a str, apikey: &'a str) -> Client<'a> {
26-
Client { host, apikey }
25+
pub fn new(host: impl Into<String>, api_key: impl Into<String>) -> Client {
26+
Client {
27+
host: Rc::new(host.into()),
28+
api_key: Rc::new(api_key.into())
29+
}
2730
}
2831

2932
/// List all [indexes](../indexes/struct.Index.html).
@@ -40,10 +43,10 @@ impl<'a> Client<'a> {
4043
/// println!("{:?}", indexes);
4144
/// # });
4245
/// ```
43-
pub async fn list_all_indexes(&'a self) -> Result<Vec<Index<'a>>, Error> {
46+
pub async fn list_all_indexes(&self) -> Result<Vec<Index>, Error> {
4447
let json_indexes = request::<(), Vec<JsonIndex>>(
4548
&format!("{}/indexes", self.host),
46-
self.apikey,
49+
&self.api_key,
4750
Method::Get,
4851
200,
4952
).await?;
@@ -72,21 +75,22 @@ impl<'a> Client<'a> {
7275
/// let movies = client.get_index("movies").await.unwrap();
7376
/// # });
7477
/// ```
75-
pub async fn get_index(&'a self, uid: &'a str) -> Result<Index<'a>, Error> {
78+
pub async fn get_index(&self, uid: impl AsRef<str>) -> Result<Index, Error> {
7679
Ok(request::<(), JsonIndex>(
77-
&format!("{}/indexes/{}", self.host, uid),
78-
self.apikey,
80+
&format!("{}/indexes/{}", self.host, uid.as_ref()),
81+
&self.api_key,
7982
Method::Get,
8083
200,
8184
).await?
8285
.into_index(self))
8386
}
8487

8588
/// Assume that an [index](../indexes/struct.Index.html) exist and create a corresponding object without any check.
86-
pub fn assume_index(&'a self, uid: &'a str) -> Index<'a> {
89+
pub fn assume_index(&self, uid: impl Into<String>) -> Index {
8790
Index {
88-
client: &self,
89-
uid: uid.to_string()
91+
uid: Rc::new(uid.into()),
92+
host: Rc::clone(&self.host),
93+
api_key: Rc::clone(&self.api_key)
9094
}
9195
}
9296

@@ -109,15 +113,15 @@ impl<'a> Client<'a> {
109113
/// # });
110114
/// ```
111115
pub async fn create_index(
112-
&'a self,
113-
uid: &'a str,
116+
&self,
117+
uid: impl AsRef<str>,
114118
primary_key: Option<&str>,
115-
) -> Result<Index<'a>, Error> {
119+
) -> Result<Index, Error> {
116120
Ok(request::<Value, JsonIndex>(
117121
&format!("{}/indexes", self.host),
118-
self.apikey,
122+
&self.api_key,
119123
Method::Post(json!({
120-
"uid": uid,
124+
"uid": uid.as_ref(),
121125
"primaryKey": primary_key,
122126
})),
123127
201,
@@ -142,26 +146,26 @@ impl<'a> Client<'a> {
142146

143147
/// Delete an index from its UID.
144148
/// To delete an index from the [index object](../indexes/struct.Index.html), use [the delete method](../indexes/struct.Index.html#method.delete).
145-
pub async fn delete_index(&self, uid: &str) -> Result<(), Error> {
149+
pub async fn delete_index(&self, uid: impl AsRef<str>) -> Result<(), Error> {
146150
Ok(request::<(), ()>(
147-
&format!("{}/indexes/{}", self.host, uid),
148-
self.apikey,
151+
&format!("{}/indexes/{}", self.host, uid.as_ref()),
152+
&self.api_key,
149153
Method::Delete,
150154
204,
151155
).await?)
152156
}
153157

154158
/// This will try to get an index and create the index if it does not exist.
155-
pub async fn get_or_create(&'a self, uid: &'a str) -> Result<Index<'a>, Error> {
156-
if let Ok(index) = self.get_index(uid).await {
159+
pub async fn get_or_create(&self, uid: impl AsRef<str>) -> Result<Index, Error> {
160+
if let Ok(index) = self.get_index(uid.as_ref()).await {
157161
Ok(index)
158162
} else {
159163
self.create_index(uid, None).await
160164
}
161165
}
162166

163167
/// Alias for [list_all_indexes](#method.list_all_indexes).
164-
pub async fn get_indexes(&'a self) -> Result<Vec<Index<'a>>, Error> {
168+
pub async fn get_indexes(&self) -> Result<Vec<Index>, Error> {
165169
self.list_all_indexes().await
166170
}
167171

@@ -180,7 +184,7 @@ impl<'a> Client<'a> {
180184
pub async fn get_stats(&self) -> Result<ClientStats, Error> {
181185
request::<serde_json::Value, ClientStats>(
182186
&format!("{}/stats", self.host),
183-
self.apikey,
187+
&self.api_key,
184188
Method::Get,
185189
200,
186190
).await
@@ -201,7 +205,7 @@ impl<'a> Client<'a> {
201205
pub async fn health(&self) -> Result<Health, Error> {
202206
request::<serde_json::Value, Health>(
203207
&format!("{}/health", self.host),
204-
self.apikey,
208+
&self.api_key,
205209
Method::Get,
206210
200,
207211
)
@@ -244,7 +248,7 @@ impl<'a> Client<'a> {
244248
pub async fn get_keys(&self) -> Result<Keys, Error> {
245249
request::<(), Keys>(
246250
&format!("{}/keys", self.host),
247-
self.apikey,
251+
&self.api_key,
248252
Method::Get,
249253
200,
250254
).await
@@ -265,7 +269,7 @@ impl<'a> Client<'a> {
265269
pub async fn get_version(&self) -> Result<Version, Error> {
266270
request::<(), Version>(
267271
&format!("{}/version", self.host),
268-
self.apikey,
272+
&self.api_key,
269273
Method::Get,
270274
200,
271275
).await

src/dumps.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub struct DumpInfo {
6363

6464
/// Dump related methods.\
6565
/// See the [dumps](crate::dumps) module.
66-
impl<'a> Client<'a> {
66+
impl Client {
6767
/// Triggers a dump creation process.
6868
/// Once the process is complete, a dump is created in the [dumps directory](https://docs.meilisearch.com/reference/features/configuration.html#dumps-destination).
6969
/// If the dumps directory does not exist yet, it will be created.
@@ -85,7 +85,7 @@ impl<'a> Client<'a> {
8585
pub async fn create_dump(&self) -> Result<DumpInfo, Error> {
8686
request::<(), DumpInfo>(
8787
&format!("{}/dumps", self.host),
88-
self.apikey,
88+
&self.api_key,
8989
Method::Post(()),
9090
202,
9191
)
@@ -109,10 +109,10 @@ impl<'a> Client<'a> {
109109
/// let dump_info = client.get_dump_status(&dump_info.uid).await.unwrap();
110110
/// # });
111111
/// ```
112-
pub async fn get_dump_status(&self, dump_uid: &str) -> Result<DumpInfo, Error> {
112+
pub async fn get_dump_status(&self, dump_uid: impl AsRef<str>) -> Result<DumpInfo, Error> {
113113
request::<(), DumpInfo>(
114-
&format!("{}/dumps/{}/status", self.host, dump_uid),
115-
self.apikey,
114+
&format!("{}/dumps/{}/status", self.host, dump_uid.as_ref()),
115+
&self.api_key,
116116
Method::Get,
117117
200,
118118
)
@@ -121,14 +121,14 @@ impl<'a> Client<'a> {
121121
}
122122

123123
/// Alias for [create_dump](Client::create_dump).
124-
pub async fn create_dump<'a>(client: &'a Client<'a>) -> Result<DumpInfo, Error> {
124+
pub async fn create_dump(client: &Client) -> Result<DumpInfo, Error> {
125125
client.create_dump().await
126126
}
127127

128128
/// Alias for [get_dump_status](Client::get_dump_status).
129-
pub async fn get_dump_status<'a>(
130-
client: &'a Client<'a>,
131-
dump_uid: &str,
129+
pub async fn get_dump_status(
130+
client: &Client,
131+
dump_uid: impl AsRef<str>,
132132
) -> Result<DumpInfo, Error> {
133133
client.get_dump_status(dump_uid).await
134134
}

0 commit comments

Comments
 (0)