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

add feature for clients that do not implement send #579

Merged
merged 3 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ wasm-bindgen-futures = "0.4"
[features]
default = ["reqwest"]
reqwest = ["dep:reqwest", "pin-project-lite", "bytes"]
futures-unsend = []

[dev-dependencies]
futures-await-test = "0.3"
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ Json output:
By default, the SDK uses [`reqwest`](https://docs.rs/reqwest/latest/reqwest/) to make http calls.
The SDK lets you customize the http client by implementing the `HttpClient` trait yourself and
initializing the `Client` with the `new_with_client` method.
You may be interested by the `futures-unsend` feature which lets you specify a non-Send http client.

#### Wasm support <!-- omit in TOC -->

The SDK supports wasm through reqwest. You'll need to enable the `futures-unsend` feature while importing it, though.

## 🌐 Running in the Browser with WASM <!-- omit in TOC -->

Expand Down
2 changes: 1 addition & 1 deletion examples/cli-app-with-awc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
meilisearch-sdk = { path = "../..", default-features = false }
meilisearch-sdk = { path = "../..", default-features = false, features = ["futures-unsend"] }
futures = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/web_app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ serde_json = "1.0"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4.18"
yew = {version="0.21", features = ["csr"]}
meilisearch-sdk = {path="../.."}
meilisearch-sdk = { path="../..", features = ["futures-unsend"] }
lazy_static = "1.4"
serde = {version="1.0", features=["derive"]}
web-sys = "0.3"
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@
//! By default, the SDK uses [`reqwest`](https://docs.rs/reqwest/latest/reqwest/) to make http calls.
//! The SDK lets you customize the http client by implementing the `HttpClient` trait yourself and
//! initializing the `Client` with the `new_with_client` method.
//! You may be interested by the `futures-unsend` feature which lets you specify a non-Send http client.
//!
//! ### Wasm support <!-- omit in TOC -->
//!
//! The SDK supports wasm through reqwest. You'll need to enable the `futures-unsend` feature while importing it, though.
#![warn(clippy::all)]
#![allow(clippy::needless_doctest_main)]

Expand Down
6 changes: 4 additions & 2 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ impl<Q, B> Method<Q, B> {
}
}

#[async_trait(?Send)]
#[cfg_attr(feature = "futures-unsend", async_trait(?Send))]
#[cfg_attr(not(feature = "futures-unsend"), async_trait)]
pub trait HttpClient: Clone + Send + Sync {
async fn request<Query, Body, Output>(
&self,
Expand Down Expand Up @@ -143,7 +144,8 @@ pub fn parse_response<Output: DeserializeOwned>(
}
}

#[async_trait(?Send)]
#[cfg_attr(feature = "futures-unsend", async_trait(?Send))]
#[cfg_attr(not(feature = "futures-unsend"), async_trait)]
impl HttpClient for Infallible {
async fn request<Query, Body, Output>(
&self,
Expand Down
3 changes: 2 additions & 1 deletion src/reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ impl ReqwestClient {
}
}

#[async_trait(?Send)]
#[cfg_attr(feature = "futures-unsend", async_trait(?Send))]
#[cfg_attr(not(feature = "futures-unsend"), async_trait)]
impl HttpClient for ReqwestClient {
async fn stream_request<
Query: Serialize + Send + Sync,
Expand Down
Loading