Trait wasm_bindgen::UnwrapThrowExt

source ·
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.


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§


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§


fn unwrap_throw(self) -> T

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

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§


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


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


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


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