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 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§

source

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§

source

fn unwrap_throw(self) -> T

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

Implementations on Foreign Types§

source§

impl<T> UnwrapThrowExt<T> for Option<T>

source§

fn expect_throw(self, message: &str) -> T

source§

impl<T, E> UnwrapThrowExt<T> for Result<T, E>where
    E: Debug,

source§

fn expect_throw(self, message: &str) -> T

Implementors§