diff --git a/Cargo.toml b/Cargo.toml index c06934617d1c..451ca86b97f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ test = false default = ["spans", "std"] spans = ["wasm-bindgen-macro/spans"] std = [] +std-unwrap = [] serde-serialize = ["serde", "serde_json", "std"] enable-interning = ["std"] diff --git a/src/lib.rs b/src/lib.rs index fdef0e8d39ab..373ec57ae51a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1327,7 +1327,7 @@ pub fn anyref_heap_live_count() -> u32 { pub trait UnwrapThrowExt: Sized { /// Unwrap this `Option` or `Result`, but instead of panicking on failure, /// throw an exception to JavaScript. - #[cfg_attr(debug_assertions, track_caller)] + #[cfg_attr(any(debug_assertions, feature = "std-unwrap"), track_caller)] fn unwrap_throw(self) -> T { if cfg!(all(debug_assertions, feature = "std")) { let loc = core::panic::Location::caller(); @@ -1346,16 +1346,26 @@ pub trait UnwrapThrowExt: Sized { /// Unwrap this container's `T` value, or throw an error to JS with the /// given message if the `T` value is unavailable (e.g. an `Option` is /// `None`). - #[cfg_attr(debug_assertions, track_caller)] + #[cfg_attr(any(debug_assertions, feature = "std-unwrap"), track_caller)] fn expect_throw(self, message: &str) -> T; } impl UnwrapThrowExt for Option { - #[cfg_attr(debug_assertions, track_caller)] + #[cfg(feature = "std-unwrap")] + #[track_caller] + fn unwrap_throw(self) -> T { + self.unwrap() + } + + #[cfg_attr(any(debug_assertions, feature = "std-unwrap"), track_caller)] fn expect_throw(self, message: &str) -> T { if cfg!(all( target_arch = "wasm32", - not(any(target_os = "emscripten", target_os = "wasi")) + not(any( + feature = "std-unwrap", + target_os = "emscripten", + target_os = "wasi" + )) )) { match self { Some(val) => val, @@ -1371,11 +1381,15 @@ impl UnwrapThrowExt for Result where E: core::fmt::Debug, { - #[cfg_attr(debug_assertions, track_caller)] + #[cfg_attr(any(debug_assertions, feature = "std-unwrap"), track_caller)] fn expect_throw(self, message: &str) -> T { if cfg!(all( target_arch = "wasm32", - not(any(target_os = "emscripten", target_os = "wasi")) + not(any( + feature = "std-unwrap", + target_os = "emscripten", + target_os = "wasi" + )) )) { match self { Ok(val) => val,