Skip to content

Commit 11d835d

Browse files
authored
use wasm-bindgen ecosystem only for wasm32-unknown-* target (#3000)
1 parent 1f72916 commit 11d835d

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ serde_json = { version = "1.0", optional = true }
113113
## multipart
114114
mime_guess = { version = "2.0", default-features = false, optional = true }
115115

116-
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
116+
[target.'cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))'.dependencies]
117117
encoding_rs = { version = "0.8", optional = true }
118118
http-body = "1"
119119
http-body-util = "0.1.2"
@@ -160,7 +160,7 @@ h3-quinn = { version = "0.0.10", optional = true }
160160
quinn = { version = "0.11.1", default-features = false, features = ["runtime-tokio"], optional = true }
161161
futures-channel = { version = "0.3", optional = true }
162162

163-
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
163+
[target.'cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))'.dev-dependencies]
164164
env_logger = "0.10"
165165
hyper = { version = "1.1.0", default-features = false, features = ["http1", "http2", "client", "server"] }
166166
hyper-util = { version = "0.1.12", features = ["http1", "http2", "client", "client-legacy", "server-auto", "server-graceful", "tokio"] }
@@ -174,13 +174,13 @@ futures-util = { version = "0.3.28", default-features = false, features = ["std"
174174

175175
# wasm
176176

177-
[target.'cfg(target_arch = "wasm32")'.dependencies]
177+
[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dependencies]
178178
js-sys = "0.3.77"
179179
wasm-bindgen = "0.2.89"
180180
wasm-bindgen-futures = "0.4.18"
181181
wasm-streams = { version = "0.5", optional = true }
182182

183-
[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
183+
[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dependencies.web-sys]
184184
version = "0.3.28"
185185
features = [
186186
"AbortController",
@@ -201,7 +201,7 @@ features = [
201201
"RequestCache"
202202
]
203203

204-
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
204+
[target.'cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))'.dev-dependencies]
205205
wasm-bindgen = { version = "0.2.89", features = ["serde-serialize"] }
206206
wasm-bindgen-test = "0.3"
207207

src/error.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
#![cfg_attr(target_arch = "wasm32", allow(unused))]
1+
#![cfg_attr(
2+
all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")),
3+
allow(unused)
4+
)]
25
use std::error::Error as StdError;
36
use std::fmt;
47
use std::io;
@@ -102,11 +105,11 @@ impl Error {
102105

103106
/// Returns true if the error is from `Response::error_for_status`.
104107
pub fn is_status(&self) -> bool {
105-
#[cfg(not(target_arch = "wasm32"))]
108+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
106109
{
107110
matches!(self.inner.kind, Kind::Status(_, _))
108111
}
109-
#[cfg(target_arch = "wasm32")]
112+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
110113
{
111114
matches!(self.inner.kind, Kind::Status(_))
112115
}
@@ -120,7 +123,10 @@ impl Error {
120123
if err.is::<TimedOut>() {
121124
return true;
122125
}
123-
#[cfg(not(target_arch = "wasm32"))]
126+
#[cfg(not(all(
127+
target_arch = "wasm32",
128+
any(target_os = "unknown", target_os = "none")
129+
)))]
124130
if let Some(hyper_err) = err.downcast_ref::<hyper::Error>() {
125131
if hyper_err.is_timeout() {
126132
return true;
@@ -142,7 +148,7 @@ impl Error {
142148
matches!(self.inner.kind, Kind::Request)
143149
}
144150

145-
#[cfg(not(target_arch = "wasm32"))]
151+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
146152
/// Returns true if the error is related to connect
147153
pub fn is_connect(&self) -> bool {
148154
let mut source = self.source();
@@ -173,9 +179,12 @@ impl Error {
173179
/// Returns the status code, if the error was generated from a response.
174180
pub fn status(&self) -> Option<StatusCode> {
175181
match self.inner.kind {
176-
#[cfg(target_arch = "wasm32")]
182+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
177183
Kind::Status(code) => Some(code),
178-
#[cfg(not(target_arch = "wasm32"))]
184+
#[cfg(not(all(
185+
target_arch = "wasm32",
186+
any(target_os = "unknown", target_os = "none")
187+
)))]
179188
Kind::Status(code, _) => Some(code),
180189
_ => None,
181190
}
@@ -198,7 +207,7 @@ impl Error {
198207
/// internal equivalents.
199208
///
200209
/// Currently only is used for `tower::timeout::error::Elapsed`.
201-
#[cfg(not(target_arch = "wasm32"))]
210+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
202211
pub(crate) fn cast_to_internal_error(error: BoxError) -> BoxError {
203212
if error.is::<tower::timeout::error::Elapsed>() {
204213
Box::new(crate::error::TimedOut) as BoxError
@@ -233,7 +242,7 @@ impl fmt::Display for Error {
233242
Kind::Decode => f.write_str("error decoding response body")?,
234243
Kind::Redirect => f.write_str("error following redirect")?,
235244
Kind::Upgrade => f.write_str("error upgrading connection")?,
236-
#[cfg(target_arch = "wasm32")]
245+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
237246
Kind::Status(ref code) => {
238247
let prefix = if code.is_client_error() {
239248
"HTTP status client error"
@@ -243,7 +252,10 @@ impl fmt::Display for Error {
243252
};
244253
write!(f, "{prefix} ({code})")?;
245254
}
246-
#[cfg(not(target_arch = "wasm32"))]
255+
#[cfg(not(all(
256+
target_arch = "wasm32",
257+
any(target_os = "unknown", target_os = "none")
258+
)))]
247259
Kind::Status(ref code, ref reason) => {
248260
let prefix = if code.is_client_error() {
249261
"HTTP status client error"
@@ -278,14 +290,14 @@ impl StdError for Error {
278290
}
279291
}
280292

281-
#[cfg(target_arch = "wasm32")]
293+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
282294
impl From<crate::error::Error> for wasm_bindgen::JsValue {
283295
fn from(err: Error) -> wasm_bindgen::JsValue {
284296
js_sys::Error::from(err).into()
285297
}
286298
}
287299

288-
#[cfg(target_arch = "wasm32")]
300+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
289301
impl From<crate::error::Error> for js_sys::Error {
290302
fn from(err: Error) -> js_sys::Error {
291303
js_sys::Error::new(&format!("{err}"))
@@ -297,9 +309,9 @@ pub(crate) enum Kind {
297309
Builder,
298310
Request,
299311
Redirect,
300-
#[cfg(not(target_arch = "wasm32"))]
312+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
301313
Status(StatusCode, Option<hyper::ext::ReasonPhrase>),
302-
#[cfg(target_arch = "wasm32")]
314+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
303315
Status(StatusCode),
304316
Body,
305317
Decode,
@@ -331,12 +343,15 @@ pub(crate) fn redirect<E: Into<BoxError>>(e: E, url: Url) -> Error {
331343
pub(crate) fn status_code(
332344
url: Url,
333345
status: StatusCode,
334-
#[cfg(not(target_arch = "wasm32"))] reason: Option<hyper::ext::ReasonPhrase>,
346+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))] reason: Option<hyper::ext::ReasonPhrase>,
335347
) -> Error {
336348
Error::new(
337349
Kind::Status(
338350
status,
339-
#[cfg(not(target_arch = "wasm32"))]
351+
#[cfg(not(all(
352+
target_arch = "wasm32",
353+
any(target_os = "unknown", target_os = "none")
354+
)))]
340355
reason,
341356
),
342357
None::<Error>,

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,14 @@ use sync_wrapper as _;
264264

265265
macro_rules! if_wasm {
266266
($($item:item)*) => {$(
267-
#[cfg(target_arch = "wasm32")]
267+
#[cfg(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none")))]
268268
$item
269269
)*}
270270
}
271271

272272
macro_rules! if_hyper {
273273
($($item:item)*) => {$(
274-
#[cfg(not(target_arch = "wasm32"))]
274+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
275275
$item
276276
)*}
277277
}

src/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ where
2424
header
2525
}
2626

27-
#[cfg(not(target_arch = "wasm32"))]
27+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
2828
pub(crate) fn fast_random() -> u64 {
2929
use std::cell::Cell;
3030
use std::collections::hash_map::RandomState;
@@ -77,7 +77,7 @@ pub(crate) fn replace_headers(dst: &mut HeaderMap, src: HeaderMap) {
7777
}
7878

7979
#[cfg(feature = "cookies")]
80-
#[cfg(not(target_arch = "wasm32"))]
80+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
8181
pub(crate) fn add_cookie_header(
8282
headers: &mut HeaderMap,
8383
cookie_store: &dyn crate::cookie::CookieStore,
@@ -90,7 +90,7 @@ pub(crate) fn add_cookie_header(
9090

9191
pub(crate) struct Escape<'a>(&'a [u8]);
9292

93-
#[cfg(not(target_arch = "wasm32"))]
93+
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
9494
impl<'a> Escape<'a> {
9595
pub(crate) fn new(bytes: &'a [u8]) -> Self {
9696
Escape(bytes)

0 commit comments

Comments
 (0)