Trait wasm_bindgen::UnwrapThrowExt[][src]

pub trait UnwrapThrowExt<T>: Sized {
    fn expect_throw(self, message: &str) -> T;

    fn unwrap_throw(self) -> T { ... }
Expand description

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.


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

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

Unwrap this Option or Result, but instead of panicking on failure, throw an exception to JavaScript.

Implementations on Foreign Types