pub struct JsOption<T = JsValue> { /* private fields */ }Expand description
A nullable JS value of type T.
Unlike Option<T>, which is a Rust-side construct, JsOption<T> represents
a JS value that may be T, null, or undefined, where the null status is
not yet known in Rust. The value remains in JS until inspected via methods
like is_empty, as_option, or
into_option.
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§
Source§impl<T> JsOption<T>
impl<T> JsOption<T>
Sourcepub fn from_option(opt: Option<T>) -> Self
pub 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().
Sourcepub fn as_option(&self) -> Option<T>
pub 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 null or undefined, otherwise returns
Some(T) with a clone of the contained value.
Sourcepub fn into_option(self) -> Option<T>
pub fn into_option(self) -> Option<T>
Converts this JsOption<T> into an Option<T>, consuming self.
Returns None if the value is null or undefined, otherwise returns
Some(T) with the contained value.
Sourcepub fn expect(self, msg: &str) -> T
pub fn expect(self, msg: &str) -> T
Returns the contained value, consuming self.
§Panics
Panics if the value is null or undefined, with a panic message
including the passed message.
Sourcepub fn unwrap_or_default(self) -> Twhere
T: Default,
pub fn unwrap_or_default(self) -> Twhere
T: Default,
Returns the contained value or a default.
Returns the contained value if not null or undefined, otherwise
returns the default value of T.
Sourcepub fn unwrap_or_else<F>(self, f: F) -> Twhere
F: FnOnce() -> T,
pub fn unwrap_or_else<F>(self, f: F) -> Twhere
F: FnOnce() -> T,
Returns the contained value or computes it from a closure.
Returns the contained value if not null or undefined, otherwise
calls f and returns the result.
Methods from Deref<Target = JsValue>§
pub const NULL: JsValue
pub const UNDEFINED: JsValue
pub const TRUE: JsValue
pub const FALSE: JsValue
Sourcepub fn into_serde<T>(&self) -> Result<T>where
T: for<'a> Deserialize<'a>,
👎Deprecated: causes dependency cycles, use serde-wasm-bindgen or gloo_utils::format::JsValueSerdeExt instead
pub fn into_serde<T>(&self) -> Result<T>where
T: for<'a> Deserialize<'a>,
serde-wasm-bindgen or gloo_utils::format::JsValueSerdeExt insteadInvokes JSON.stringify on this value and then parses the resulting
JSON into an arbitrary Rust value.
This function is deprecated, due to creating a dependency cycle in
some circumstances. Use serde-wasm-bindgen or
gloo_utils::format::JsValueSerdeExt instead.
This function will first call JSON.stringify on the JsValue itself.
The resulting string is then passed into Rust which then parses it as
JSON into the resulting value.
Usage of this API requires activating the serde-serialize feature of
the wasm-bindgen crate.
§Errors
Returns any error encountered when parsing the JSON into a T.
Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
Returns the f64 value of this JS value if it’s an instance of a
number.
If this JS value is not an instance of a number then this returns
None.
Sourcepub fn as_string(&self) -> Option<String>
pub fn as_string(&self) -> Option<String>
If this JS value is a string value, this function copies the JS string
value into Wasm linear memory, encoded as UTF-8, and returns it as a
Rust String.
To avoid the copying and re-encoding, consider the
JsString::try_from() function from js-sys
instead.
If this JS value is not an instance of a string or if it’s not valid
utf-8 then this returns None.
§UTF-16 vs UTF-8
JavaScript strings in general are encoded as UTF-16, but Rust strings
are encoded as UTF-8. This can cause the Rust string to look a bit
different than the JS string sometimes. For more details see the
documentation about the str type which contains a few
caveats about the encodings.
Sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
Returns the bool value of this JS value if it’s an instance of a
boolean.
If this JS value is not an instance of a boolean then this returns
None.
Sourcepub fn is_undefined(&self) -> bool
pub fn is_undefined(&self) -> bool
Tests whether this JS value is undefined
Sourcepub fn is_null_or_undefined(&self) -> bool
pub fn is_null_or_undefined(&self) -> bool
Tests whether this JS value is null or undefined
Sourcepub fn is_function(&self) -> bool
pub fn is_function(&self) -> bool
Tests whether the type of this JS value is function.
Sourcepub fn js_in(&self, obj: &JsValue) -> bool
pub fn js_in(&self, obj: &JsValue) -> bool
Applies the binary in JS operator on the two JsValues.
Sourcepub fn loose_eq(&self, other: &Self) -> bool
pub fn loose_eq(&self, other: &Self) -> bool
Compare two JsValues for equality, using the == operator in JS.
Sourcepub fn unsigned_shr(&self, rhs: &Self) -> u32
pub fn unsigned_shr(&self, rhs: &Self) -> u32
Applies the binary >>> JS operator on the two JsValues.
Sourcepub fn checked_div(&self, rhs: &Self) -> Self
pub fn checked_div(&self, rhs: &Self) -> Self
Applies the binary / JS operator on two JsValues, catching and returning any RangeError thrown.
Sourcepub fn unchecked_into_f64(&self) -> f64
pub fn unchecked_into_f64(&self) -> f64
Applies the unary + JS operator on a JsValue. Can throw.
Trait Implementations§
Source§impl<T> ErasableGeneric for JsOption<T>
impl<T> ErasableGeneric for JsOption<T>
Source§impl<T> FromWasmAbi for JsOption<T>
impl<T> FromWasmAbi for JsOption<T>
Source§impl<'a, T> IntoWasmAbi for &'a JsOption<T>
impl<'a, T> IntoWasmAbi for &'a JsOption<T>
Source§impl<T> IntoWasmAbi for JsOption<T>
impl<T> IntoWasmAbi for JsOption<T>
Source§impl<T> JsCast for JsOption<T>
impl<T> JsCast for JsOption<T>
Source§fn instanceof(val: &JsValue) -> bool
fn instanceof(val: &JsValue) -> bool
instanceof check to see whether the JsValue
provided is an instance of this type. Read moreSource§fn unchecked_from_js(val: JsValue) -> Self
fn unchecked_from_js(val: JsValue) -> Self
Source§fn unchecked_from_js_ref(val: &JsValue) -> &Self
fn unchecked_from_js_ref(val: &JsValue) -> &Self
Source§fn has_type<T>(&self) -> boolwhere
T: JsCast,
fn has_type<T>(&self) -> boolwhere
T: JsCast,
T. Read moreSource§fn dyn_into<T>(self) -> Result<T, Self>where
T: JsCast,
fn dyn_into<T>(self) -> Result<T, Self>where
T: JsCast,
T. Read moreSource§fn dyn_ref<T>(&self) -> Option<&T>where
T: JsCast,
fn dyn_ref<T>(&self) -> Option<&T>where
T: JsCast,
T. Read moreSource§fn unchecked_into<T>(self) -> Twhere
T: JsCast,
fn unchecked_into<T>(self) -> Twhere
T: JsCast,
Source§fn unchecked_ref<T>(&self) -> &Twhere
T: JsCast,
fn unchecked_ref<T>(&self) -> &Twhere
T: JsCast,
Source§impl<T> LongRefFromWasmAbi for JsOption<T>
impl<T> LongRefFromWasmAbi for JsOption<T>
Source§impl<T> OptionFromWasmAbi for JsOption<T>
impl<T> OptionFromWasmAbi for JsOption<T>
Source§impl<'a, T> OptionIntoWasmAbi for &'a JsOption<T>
impl<'a, T> OptionIntoWasmAbi for &'a JsOption<T>
Source§impl<T> OptionIntoWasmAbi for JsOption<T>
impl<T> OptionIntoWasmAbi for JsOption<T>
Source§impl<T> Promising for JsOption<T>
impl<T> Promising for JsOption<T>
Source§type Resolution = JsOption<T>
type Resolution = JsOption<T>
Source§impl<T> RefFromWasmAbi for JsOption<T>
impl<T> RefFromWasmAbi for JsOption<T>
Source§type Abi = <JsValue as RefFromWasmAbi>::Abi
type Abi = <JsValue as RefFromWasmAbi>::Abi
Self are recovered from.Source§type Anchor = ManuallyDrop<JsOption<T>>
type Anchor = ManuallyDrop<JsOption<T>>
Self for the duration of the
invocation of the function that has an &Self parameter. This is
required to ensure that the lifetimes don’t persist beyond one function
call, and so that they remain anonymous.impl<T> StructuralPartialEq for JsOption<T>
impl<T> UpcastFrom<()> for JsOption<T>
impl<T1, Target1> UpcastFrom<(T1,)> for JsOption<(Target1,)>
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, 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, 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, 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, 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, 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, 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<T, Target> UpcastFrom<*const T> for JsOption<*const Target>where
Target: UpcastFrom<T>,
impl UpcastFrom<JsError> for JsOption<JsValue>
impl<T> UpcastFrom<JsOption<T>> for JsValue
impl<T, U> UpcastFrom<JsOption<U>> for JsOption<T>where
T: UpcastFrom<U>,
impl UpcastFrom<JsValue> for JsOption<JsValue>
impl<T> UpcastFrom<Null> for JsOption<T>
impl<T, Target> UpcastFrom<Option<T>> for JsOption<Option<Target>>where
Target: UpcastFrom<T>,
impl<T, E, TargetT, TargetE> UpcastFrom<Result<T, E>> for JsOption<Result<TargetT, TargetE>>where
TargetT: UpcastFrom<T>,
TargetE: UpcastFrom<E>,
impl<T> UpcastFrom<Undefined> for JsOption<T>
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>
Auto Trait Implementations§
impl<T> Freeze for JsOption<T>
impl<T> RefUnwindSafe for JsOption<T>where
T: RefUnwindSafe,
impl<T> Send for JsOption<T>where
T: Send,
impl<T> Sync for JsOption<T>where
T: Sync,
impl<T> Unpin for JsOption<T>where
T: Unpin,
impl<T> UnsafeUnpin for JsOption<T>
impl<T> UnwindSafe for JsOption<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
Source§type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
IntoWasmAbi::AbiSource§fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
IntoWasmAbi::into_abi, except that it may throw and never
return in the case of Err.