mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-15 12:44:31 +03:00
0e11e4a3bd
An extension trait for `Option<T>` and `Result<T, E>` for unwraping 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.
24 lines
427 B
Rust
24 lines
427 B
Rust
extern crate wasm_bindgen;
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
#[test]
|
|
fn unwrap_throw_ok() {
|
|
assert_eq!(Some(42).unwrap_throw(), 42);
|
|
let x: Result<i32, ()> = Ok(42);
|
|
assert_eq!(x.unwrap_throw(), 42);
|
|
}
|
|
|
|
#[test]
|
|
#[should_panic]
|
|
fn unwrap_throw_none() {
|
|
let x: Option<i32> = None;
|
|
x.unwrap_throw();
|
|
}
|
|
|
|
#[test]
|
|
#[should_panic]
|
|
fn unwrap_throw_err() {
|
|
let x: Result<i32, ()> = Err(());
|
|
x.unwrap_throw();
|
|
}
|