Struct JsOption

#[repr(transparent)]
pub struct JsOption<T = JsValue> { /* private fields */ }

An optional JS value of type T.

Unlike Option<T>, which is a Rust-side construct, JsOption<T> represents a JS value that may be T or undefined, where the presence status is not yet known in Rust. The value remains in JS until inspected via methods like is_empty, as_option, or into_option.

Only undefined is treated as absent, matching TypeScript's T | undefined. JavaScript null is a distinct present value.

T must implement JsGeneric, meaning it is any type that can be represented as a JsValue (e.g., JsString, Number, Object, etc.). JsOption<T> itself implements JsGeneric, so it can be used in all generic positions that accept JS types.

Implementations

impl<T: JsGeneric> JsOption<T>

fn new() -> Self

Creates an empty JsOption<T> representing undefined.

fn wrap(val: T) -> Self

Wraps a value in a JsOption<T>.

fn from_option(opt: Option<T>) -> Self

Creates a JsOption<T> from an Option<T>.

Returns JsOption::wrap(val) if Some(val), otherwise JsOption::new().

fn is_empty(&self) -> bool

Tests whether this JsOption<T> is empty (undefined).

fn as_option(&self) -> Option<T>

Converts this JsOption<T> to an Option<T> by cloning the inner value.

Returns None if the value is undefined, otherwise returns Some(T) with a clone of the contained value.

fn into_option(self) -> Option<T>

Converts this JsOption<T> into an Option<T>, consuming self.

Returns None if the value is undefined, otherwise returns Some(T) with the contained value.

fn unwrap(self) -> T

Returns the contained value, consuming self.

Panics

Panics if the value is undefined.

fn expect(self, msg: &str) -> T

Returns the contained value, consuming self.

Panics

Panics if the value is undefined, with a panic message including the passed message.

fn unwrap_or_default(self) -> T
where
    T: Default,

Returns the contained value or a default.

Returns the contained value if not undefined, otherwise returns the default value of T.

fn unwrap_or_else<F>(self, f: F) -> T
where
    F: FnOnce() -> T,

Returns the contained value or computes it from a closure.

Returns the contained value if not undefined, otherwise calls f and returns the result.

Trait Implementations

impl From<JsValue> for JsOption

fn from(obj: JsValue) -> Self

impl UpcastFrom<JsError> for JsOption<JsValue>

impl UpcastFrom<JsValue> for JsOption<JsValue>

impl UpcastFrom<bool> for JsOption<JsValue>

impl UpcastFrom<char> for JsOption<JsValue>

impl UpcastFrom<f32> for JsOption<JsValue>

impl UpcastFrom<f32> for JsOption<f64>

impl UpcastFrom<f64> for JsOption<JsValue>

impl UpcastFrom<i128> for JsOption<JsValue>

impl UpcastFrom<i16> for JsOption<JsValue>

impl UpcastFrom<i16> for JsOption<i128>

impl UpcastFrom<i16> for JsOption<i32>

impl UpcastFrom<i16> for JsOption<i64>

impl UpcastFrom<i32> for JsOption<JsValue>

impl UpcastFrom<i32> for JsOption<i128>

impl UpcastFrom<i32> for JsOption<i64>

impl UpcastFrom<i32> for JsOption<isize>

impl UpcastFrom<i64> for JsOption<JsValue>

impl UpcastFrom<i64> for JsOption<i128>

impl UpcastFrom<i8> for JsOption<JsValue>

impl UpcastFrom<i8> for JsOption<i128>

impl UpcastFrom<i8> for JsOption<i16>

impl UpcastFrom<i8> for JsOption<i32>

impl UpcastFrom<i8> for JsOption<i64>

impl UpcastFrom<isize> for JsOption<JsValue>

impl UpcastFrom<isize> for JsOption<i128>

impl UpcastFrom<isize> for JsOption<i64>

impl UpcastFrom<u128> for JsOption<JsValue>

impl UpcastFrom<u16> for JsOption<JsValue>

impl UpcastFrom<u16> for JsOption<u128>

impl UpcastFrom<u16> for JsOption<u32>

impl UpcastFrom<u16> for JsOption<u64>

impl UpcastFrom<u32> for JsOption<JsValue>

impl UpcastFrom<u32> for JsOption<u128>

impl UpcastFrom<u32> for JsOption<u64>

impl UpcastFrom<u32> for JsOption<usize>

impl UpcastFrom<u64> for JsOption<JsValue>

impl UpcastFrom<u64> for JsOption<u128>

impl UpcastFrom<u8> for JsOption<JsValue>

impl UpcastFrom<u8> for JsOption<u128>

impl UpcastFrom<u8> for JsOption<u16>

impl UpcastFrom<u8> for JsOption<u32>

impl UpcastFrom<u8> for JsOption<u64>

impl UpcastFrom<usize> for JsOption<JsValue>

impl UpcastFrom<usize> for JsOption<u128>

impl UpcastFrom<usize> for JsOption<u64>

impl<T> AsRef<JsOption<T>> for JsOption<T>

fn as_ref(&self) -> &JsOption<T>

impl<T> AsRef<JsValue> for JsOption<T>

fn as_ref(&self) -> &JsValue

impl<T> Deref for JsOption<T>

type Target = JsValue;
fn deref(&self) -> &JsValue

impl<T> FromWasmAbi for JsOption<T>

type Abi = <JsValue as FromWasmAbi>::Abi;
unsafe fn from_abi(js: Self::Abi) -> Self

impl<T> IntoJsGeneric for JsOption<T> where JsOption<T>: JsGeneric,

type JsCanon = JsOption<T>;
fn to_js(self) -> JsOption<T>

impl<T> IntoWasmAbi for JsOption<T>

type Abi = <JsValue as IntoWasmAbi>::Abi;
fn into_abi(self) -> Self::Abi

impl<T> JsCast for JsOption<T>

fn instanceof(val: &JsValue) -> bool
fn unchecked_from_js(val: JsValue) -> Self
fn unchecked_from_js_ref(val: &JsValue) -> &Self

impl<T> LongRefFromWasmAbi for JsOption<T>

type Abi = <JsValue as LongRefFromWasmAbi>::Abi;
type Anchor = JsOption<T>;
unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor

impl<T> OptionFromWasmAbi for JsOption<T>

fn is_none(abi: &Self::Abi) -> bool

impl<T> OptionIntoWasmAbi for JsOption<T>

fn none() -> Self::Abi

impl<T> Promising for JsOption<T>

type Resolution = JsOption<T>;

impl<T> RefFromWasmAbi for JsOption<T>

type Abi = <JsValue as RefFromWasmAbi>::Abi;
type Anchor = ManuallyDrop<JsOption<T>>;
unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor

impl<T> UpcastFrom<()> for JsOption<T>

impl<T> UpcastFrom<Undefined> for JsOption<T>

impl<T, E, TargetT, TargetE> UpcastFrom<Result<T, E>> for JsOption<Result<TargetT, TargetE>> where TargetT: UpcastFrom<T>, TargetE: UpcastFrom<E>,

impl<T, Target> UpcastFrom<*const T> for JsOption<*const Target> where Target: UpcastFrom<T>,

impl<T, Target> UpcastFrom<Option<T>> for JsOption<Option<Target>> where Target: UpcastFrom<T>,

impl<T, U> UpcastFrom<JsOption<U>> for JsOption<T> where T: UpcastFrom<U>,

impl<T1, T2, T3, T4, T5, T6, T7, T8, Target1, Target2, Target3, Target4, Target5, Target6, Target7, Target8> UpcastFrom<(T1, T2, T3, T4, T5, T6, T7, T8)> for JsOption<(Target1, Target2, Target3, Target4, Target5, Target6, Target7, Target8)> where Target1: JsGeneric + UpcastFrom<T1>, Target2: JsGeneric + UpcastFrom<T2>, Target3: JsGeneric + UpcastFrom<T3>, Target4: JsGeneric + UpcastFrom<T4>, Target5: JsGeneric + UpcastFrom<T5>, Target6: JsGeneric + UpcastFrom<T6>, Target7: JsGeneric + UpcastFrom<T7>, Target8: JsGeneric + UpcastFrom<T8>, T1: JsGeneric, T2: JsGeneric, T3: JsGeneric, T4: JsGeneric, T5: JsGeneric, T6: JsGeneric, T7: JsGeneric, T8: JsGeneric,

impl<T1, T2, T3, T4, T5, T6, T7, Target1, Target2, Target3, Target4, Target5, Target6, Target7> UpcastFrom<(T1, T2, T3, T4, T5, T6, T7)> for JsOption<(Target1, Target2, Target3, Target4, Target5, Target6, Target7)> where Target1: JsGeneric + UpcastFrom<T1>, Target2: JsGeneric + UpcastFrom<T2>, Target3: JsGeneric + UpcastFrom<T3>, Target4: JsGeneric + UpcastFrom<T4>, Target5: JsGeneric + UpcastFrom<T5>, Target6: JsGeneric + UpcastFrom<T6>, Target7: JsGeneric + UpcastFrom<T7>, T1: JsGeneric, T2: JsGeneric, T3: JsGeneric, T4: JsGeneric, T5: JsGeneric, T6: JsGeneric, T7: JsGeneric,

impl<T1, T2, T3, T4, T5, T6, Target1, Target2, Target3, Target4, Target5, Target6> UpcastFrom<(T1, T2, T3, T4, T5, T6)> for JsOption<(Target1, Target2, Target3, Target4, Target5, Target6)> where Target1: JsGeneric + UpcastFrom<T1>, Target2: JsGeneric + UpcastFrom<T2>, Target3: JsGeneric + UpcastFrom<T3>, Target4: JsGeneric + UpcastFrom<T4>, Target5: JsGeneric + UpcastFrom<T5>, Target6: JsGeneric + UpcastFrom<T6>, T1: JsGeneric, T2: JsGeneric, T3: JsGeneric, T4: JsGeneric, T5: JsGeneric, T6: JsGeneric,

impl<T1, T2, T3, T4, T5, Target1, Target2, Target3, Target4, Target5> UpcastFrom<(T1, T2, T3, T4, T5)> for JsOption<(Target1, Target2, Target3, Target4, Target5)> where Target1: JsGeneric + UpcastFrom<T1>, Target2: JsGeneric + UpcastFrom<T2>, Target3: JsGeneric + UpcastFrom<T3>, Target4: JsGeneric + UpcastFrom<T4>, Target5: JsGeneric + UpcastFrom<T5>, T1: JsGeneric, T2: JsGeneric, T3: JsGeneric, T4: JsGeneric, T5: JsGeneric,

impl<T1, T2, T3, T4, Target1, Target2, Target3, Target4> UpcastFrom<(T1, T2, T3, T4)> for JsOption<(Target1, Target2, Target3, Target4)> where Target1: JsGeneric + UpcastFrom<T1>, Target2: JsGeneric + UpcastFrom<T2>, Target3: JsGeneric + UpcastFrom<T3>, Target4: JsGeneric + UpcastFrom<T4>, T1: JsGeneric, T2: JsGeneric, T3: JsGeneric, T4: JsGeneric,

impl<T1, T2, T3, Target1, Target2, Target3> UpcastFrom<(T1, T2, T3)> for JsOption<(Target1, Target2, Target3)> where Target1: JsGeneric + UpcastFrom<T1>, Target2: JsGeneric + UpcastFrom<T2>, Target3: JsGeneric + UpcastFrom<T3>, T1: JsGeneric, T2: JsGeneric, T3: JsGeneric,

impl<T1, T2, Target1, Target2> UpcastFrom<(T1, T2)> for JsOption<(Target1, Target2)> where Target1: JsGeneric + UpcastFrom<T1>, Target2: JsGeneric + UpcastFrom<T2>, T1: JsGeneric, T2: JsGeneric,

impl<T1, Target1> UpcastFrom<(T1,)> for JsOption<(Target1,)> where Target1: JsGeneric + UpcastFrom<T1>, T1: JsGeneric,

impl<T: Clone> Clone for JsOption<T>

fn clone(&self) -> Self

impl<T: JsGeneric + Debug> Debug for JsOption<T>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<T: JsGeneric + Display> Display for JsOption<T>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<T: JsGeneric> Default for JsOption<T>

fn default() -> Self

impl<T: PartialEq> PartialEq for JsOption<T>

fn eq(&self, other: &Self) -> bool

impl<T: PartialEq> StructuralPartialEq for JsOption<T>

Auto Trait Implementations

impl<T> Freeze for JsOption<T> where PhantomData<(T,)>: Freeze,

impl<T> RefUnwindSafe for JsOption<T> where PhantomData<(T,)>: RefUnwindSafe,

impl<T> Send for JsOption<T> where PhantomData<(T,)>: Send,

impl<T> Sync for JsOption<T> where PhantomData<(T,)>: Sync,

impl<T> Unpin for JsOption<T> where PhantomData<(T,)>: Unpin,

impl<T> UnsafeUnpin for JsOption<T> where PhantomData<(T,)>: UnsafeUnpin,

impl<T> UnwindSafe for JsOption<T> where PhantomData<(T,)>: UnwindSafe,

Blanket Implementations

impl<P, T> Receiver for JsOption<T> where P: Deref<Target = T> + ?Sized, T: ?Sized,

impl<S, T> Upcast<T> for JsOption<T> where T: UpcastFrom<S> + ?Sized, S: ?Sized,

impl<T> Any for JsOption<T> where T: 'static + ?Sized,

impl<T> Borrow<T> for JsOption<T> where T: ?Sized,

impl<T> BorrowMut<T> for JsOption<T> where T: ?Sized,

impl<T> CloneToUninit for JsOption<T> where T: Clone,

impl<T> From<T> for JsOption<T>

impl<T> JsGeneric for JsOption<T> where T: ErasableGeneric<Repr = JsValue> + UpcastFrom<T> + Upcast<JsValue> + JsCast + 'static,

impl<T> ReturnWasmAbi for JsOption<T> where T: IntoWasmAbi,

impl<T> ToOwned for JsOption<T> where T: Clone,

impl<T> ToString for JsOption<T> where T: Display + ?Sized,

impl<T> TryFromJsValue for JsOption<T> where T: JsCast,

impl<T> VectorFromWasmAbi for JsOption<T> where T: ErasableGeneric<Repr = JsValue> + WasmDescribe,

impl<T> VectorIntoWasmAbi for JsOption<T> where T: ErasableGeneric<Repr = JsValue> + WasmDescribe,

impl<T> VectorRefIntoWasmAbi for JsOption<T> where &'a T: for<'a> Into<JsValue>,

impl<T, U> Into<U> for JsOption<T> where U: From<T>,

impl<T, U> TryFrom<U> for JsOption<T> where U: Into<T>,

impl<T, U> TryInto<U> for JsOption<T> where U: TryFrom<T>,