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

instruducing inner data for Client to avoid extra Allocations #641

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,21 @@ use crate::{
/// The top-level struct of the SDK, representing a client containing [indexes](../indexes/struct.Index.html).
#[derive(Debug, Clone)]
pub struct Client<Http: HttpClient = DefaultHttpClient> {
pub(crate) inner: std::sync::Arc<ClientInner<Http>>,
}

#[derive(Debug)]
pub struct ClientInner<Http: HttpClient = DefaultHttpClient> {
pub(crate) host: String,
pub(crate) api_key: Option<String>,
pub(crate) http_client: Http,
}

impl<Http: HttpClient> std::ops::Deref for Client<Http> {
type Target = ClientInner<Http>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SwapIndexes {
pub indexes: (String, String),
Expand Down Expand Up @@ -55,9 +65,11 @@ impl Client {
let http_client = crate::reqwest::ReqwestClient::new(api_key.as_deref())?;

Ok(Client {
host: host.into(),
api_key,
http_client,
inner: std::sync::Arc::new(ClientInner {
host: host.into(),
api_key,
http_client,
}),
})
}
}
Expand All @@ -70,9 +82,11 @@ impl<Http: HttpClient> Client<Http> {
http_client: Http,
) -> Client<Http> {
Client {
host: host.into(),
api_key: api_key.map(|key| key.into()),
http_client,
inner: std::sync::Arc::new(ClientInner {
host: host.into(),
api_key: api_key.map(|key| key.into()),
http_client,
}),
}
}

Expand Down Expand Up @@ -1353,7 +1367,7 @@ mod tests {
let master_key = client.api_key.clone();

// create a new client with no right
let client = Client::new(client.host, Some(key.key.clone())).unwrap();
let client = Client::new(client.host.clone(), Some(key.key.clone())).unwrap();
// with a wrong key
let error = client.delete_key("invalid_key").await.unwrap_err();
insta::assert_snapshot!(error, @"Meilisearch auth: invalid_api_key: The provided API key is invalid.. https://docs.meilisearch.com/errors#invalid_api_key");
Expand All @@ -1378,7 +1392,7 @@ mod tests {
));

// cleanup
let client = Client::new(client.host, master_key).unwrap();
let client = Client::new(client.host.clone(), master_key).unwrap();
client.delete_key(key).await.unwrap();
}

Expand Down Expand Up @@ -1446,7 +1460,7 @@ mod tests {

// cleanup
master_client
.delete_key(client.api_key.unwrap())
.delete_key(client.api_key.clone().unwrap())
.await
.unwrap();
}
Expand Down