Skip to content

Commit aa19d97

Browse files
committed
feat: change to generics
Remove existential type and also removes need for box
1 parent 29f0946 commit aa19d97

File tree

3 files changed

+14
-20
lines changed

3 files changed

+14
-20
lines changed

src/native.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
compile_error!("Must chose a native runtime by enabling a feature flag. Right now only tokio is supported. If you have a different runtime that you want please create an issue on github.");
55

66
#[cfg(feature = "native-tokio")]
7-
pub(crate) fn fetch(
8-
request: reqwest::RequestBuilder,
9-
on_done: impl 'static + Send + FnOnce(Result<reqwest::Response, reqwest::Error>),
10-
) {
7+
pub fn fetch<F>(request: reqwest::RequestBuilder, on_done: F)
8+
where
9+
F: 'static + Send + FnOnce(Result<reqwest::Response, reqwest::Error>),
10+
{
1111
tokio::spawn(async move {
1212
let result = request.send().await;
1313
on_done(result)

src/wasm.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
//! Stores the code specific to wasm compilations
22
3-
use reqwest::RequestBuilder;
4-
5-
pub(crate) fn fetch(
6-
request: RequestBuilder,
7-
on_done: impl 'static + Send + FnOnce(Result<reqwest::Response, reqwest::Error>),
8-
) {
3+
pub fn fetch<F>(request: reqwest::RequestBuilder, on_done: F)
4+
where
5+
F: 'static + Send + FnOnce(reqwest::Result<reqwest::Response>),
6+
{
97
wasm_bindgen_futures::spawn_local(async move {
108
let result = request.send().await;
119
on_done(result)

src/wrappers.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! Stores the wrapper functions that can be called from either native or wasm
22
//! code
33
4-
use reqwest::{Error, RequestBuilder, Response};
5-
64
/// Performs a HTTP requests and calls the given callback when done. NB: Needs
75
/// to use a callback to prevent blocking on the thread that initiates the
86
/// fetch. Note: Instead of calling get like in the example you can use post,
@@ -34,17 +32,15 @@ use reqwest::{Error, RequestBuilder, Response};
3432
/// # #[cfg(target_arch = "wasm32")]
3533
/// # fn main(){}
3634
/// ```
37-
pub fn fetch(
38-
request: RequestBuilder,
39-
on_done: impl 'static + Send + FnOnce(Result<Response, Error>),
40-
) {
35+
pub fn fetch<F>(request: reqwest::RequestBuilder, on_done: F)
36+
where
37+
F: 'static + Send + FnOnce(reqwest::Result<reqwest::Response>),
38+
{
4139
#[cfg(not(target_arch = "wasm32"))]
42-
crate::native::fetch(request, Box::new(on_done));
40+
crate::native::fetch(request, on_done);
4341

4442
#[cfg(target_arch = "wasm32")]
45-
crate::wasm::fetch(request, Box::new(on_done));
43+
crate::wasm::fetch(request, on_done);
4644
}
4745

48-
// TODO 3: Change to generics for `on_done`
49-
5046
// TODO 3: Test link in documentation after pushing to main

0 commit comments

Comments
 (0)