pub trait UnwrapThrowExt<T>: Sized {
// Required method
fn expect_throw(self, message: &str) -> T;
// Provided method
fn unwrap_throw(self) -> T { ... }
}
Expand description
An extension trait for Option<T>
and Result<T, E>
for unwrapping the T
value, or throwing a JS error if it is not available.
These methods should have a smaller code size footprint than the normal
Option::unwrap
and Option::expect
methods, but they are specific to
working with Wasm and JS.
On non-wasm32 targets, defaults to the normal unwrap/expect calls.
§Example
use wasm_bindgen::prelude::*;
// If the value is `Option::Some` or `Result::Ok`, then we just get the
// contained `T` value.
let x = Some(42);
assert_eq!(x.unwrap_throw(), 42);
let y: Option<i32> = None;
// This call would throw an error to JS!
//
// y.unwrap_throw()
//
// And this call would throw an error to JS with a custom error message!
//
// y.expect_throw("woopsie daisy!")
Required Methods§
sourcefn expect_throw(self, message: &str) -> T
fn expect_throw(self, message: &str) -> T
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<T>
is
None
).
Provided Methods§
sourcefn unwrap_throw(self) -> T
fn unwrap_throw(self) -> T
Unwrap this Option
or Result
, but instead of panicking on failure,
throw an exception to JavaScript.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.