Skip to content

Commit b68c166

Browse files
Remove futures-util and apply notes from code review
Co-authored-by: Yoshua Wuyts <[email protected]>
1 parent f642dde commit b68c166

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ isahc = { version = "0.9", optional = true, default-features = false, features =
4242
js-sys = { version = "0.3.25", optional = true }
4343
wasm-bindgen = { version = "0.2.48", optional = true }
4444
wasm-bindgen-futures = { version = "0.4.5", optional = true }
45-
futures-util = "0.3.1"
4645

4746
[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
4847
version = "0.3.25"

src/wasm.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ impl HttpClient for WasmClient {
4343
response.set_body(Body::from(body));
4444
for (name, value) in res.headers() {
4545
let name: http_types::headers::HeaderName = name.parse().unwrap();
46-
response.insert_header(
47-
&name,
48-
value.parse::<http_types::headers::HeaderValue>().unwrap(),
49-
);
46+
response.insert_header(&name, value);
5047
}
5148

5249
Ok(response)
@@ -76,7 +73,6 @@ impl Future for InnerFuture {
7673
}
7774

7875
mod fetch {
79-
use futures_util::io::AsyncReadExt;
8076
use js_sys::{Array, ArrayBuffer, Reflect, Uint8Array};
8177
use wasm_bindgen::JsCast;
8278
use wasm_bindgen_futures::JsFuture;
@@ -91,35 +87,34 @@ mod fetch {
9187
/// An HTTP Fetch Request.
9288
pub(crate) struct Request {
9389
request: web_sys::Request,
94-
_body_buf: Pin<Vec<u8>>,
90+
/// This field stores the body of the request to ensure it stays allocated as long as the request needs it.
91+
#[allow(dead_code)]
92+
body_buf: Pin<Vec<u8>>,
9593
}
9694

9795
impl Request {
9896
/// Create a new instance.
9997
pub(crate) async fn new(mut req: super::Request) -> Result<Self, io::Error> {
100-
//create a fetch request initaliser
98+
// create a fetch request initaliser
10199
let mut init = RequestInit::new();
102100

103-
//set the fetch method
101+
// set the fetch method
104102
init.method(req.method().as_ref());
105103

106104
let uri = req.url().to_string();
107-
let mut body = req.take_body();
105+
let body = req.take_body();
108106

109-
//convert the body into a uint8 array
107+
// convert the body into a uint8 array
110108
// needs to be pinned and retained inside the Request because the Uint8Array passed to
111109
// js is just a portal into WASM linear memory, and if the underlying data is moved the
112110
// js ref will become silently invalid
113-
let mut body_buf = Vec::with_capacity(1024);
114-
body.read_to_end(&mut body_buf).await.map_err(|_| {
111+
let body_buf = body.into_bytes().await.map_err(|_| {
115112
io::Error::new(io::ErrorKind::Other, "could not read body into a buffer")
116113
})?;
117114
let body_pinned = Pin::new(body_buf);
118115
if body_pinned.len() > 0 {
119-
unsafe {
120-
let uint_8_array = js_sys::Uint8Array::view(&body_pinned);
121-
init.body(Some(&uint_8_array));
122-
}
116+
let uint_8_array = unsafe { js_sys::Uint8Array::view(&body_pinned) };
117+
init.body(Some(&uint_8_array));
123118
}
124119

125120
let request = web_sys::Request::new_with_str_and_init(&uri, &init).map_err(|e| {
@@ -129,7 +124,7 @@ mod fetch {
129124
)
130125
})?;
131126

132-
//add any fetch headers
127+
// add any fetch headers
133128
let headers: &mut super::Headers = req.as_mut();
134129
for (name, value) in headers.iter() {
135130
let name = name.as_str();
@@ -145,7 +140,7 @@ mod fetch {
145140

146141
Ok(Self {
147142
request,
148-
_body_buf: body_pinned,
143+
body_buf: body_pinned,
149144
})
150145
}
151146

0 commit comments

Comments
 (0)