pub struct JsNullable<T = JsValue> { /* private fields */ }Expand description
A nullable JS value of type T.
Unlike Option<T>, which is a Rust-side construct, JsNullable<T>
represents a JS value that may be T or null, 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.
This models WebIDL nullable types (T?). Following WebIDL’s
ECMAScript conversion rules, where undefined coerces to null at
nullable positions, both null and undefined are treated as absent.
The canonical empty value produced from Rust is null.
For TypeScript-style T | undefined with strict undefined-only
emptiness, use JsOption<T> instead.
T must implement JsGeneric, meaning it is any type that can be
represented as a JsValue (e.g., JsString, Number, Object, etc.).
JsNullable<T> itself implements JsGeneric, so it can be used in all
generic positions that accept JS types.
Implementations§
Source§impl<T: JsGeneric> JsNullable<T>
impl<T: JsGeneric> JsNullable<T>
Sourcepub fn from_option(opt: Option<T>) -> Self
pub fn from_option(opt: Option<T>) -> Self
Creates a JsNullable<T> from an Option<T>.
Returns JsNullable::wrap(val) if Some(val), otherwise JsNullable::new().
Sourcepub fn as_option(&self) -> Option<T>
pub fn as_option(&self) -> Option<T>
Converts this JsNullable<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 JsNullable<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>§
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>,
causes dependency cycles, use serde-wasm-bindgen or gloo_utils::format::JsValueSerdeExt instead
Invokes 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> AsRef<JsNullable<T>> for JsNullable<T>
impl<T> AsRef<JsNullable<T>> for JsNullable<T>
Source§fn as_ref(&self) -> &JsNullable<T>
fn as_ref(&self) -> &JsNullable<T>
Source§impl<T> AsRef<JsValue> for JsNullable<T>
impl<T> AsRef<JsValue> for JsNullable<T>
Source§impl<T: Clone> Clone for JsNullable<T>
impl<T: Clone> Clone for JsNullable<T>
Source§impl<T: JsGeneric> Default for JsNullable<T>
impl<T: JsGeneric> Default for JsNullable<T>
Source§impl<T> Deref for JsNullable<T>
impl<T> Deref for JsNullable<T>
Source§impl<T> ErasableGeneric for JsNullable<T>
impl<T> ErasableGeneric for JsNullable<T>
Source§impl<T: JsGeneric> From<JsNullable<T>> for Option<Nullable<T>>
impl<T: JsGeneric> From<JsNullable<T>> for Option<Nullable<T>>
Source§fn from(nullable: JsNullable<T>) -> Self
fn from(nullable: JsNullable<T>) -> Self
Source§impl<T> From<JsNullable<T>> for JsValue
impl<T> From<JsNullable<T>> for JsValue
Source§fn from(obj: JsNullable<T>) -> JsValue
fn from(obj: JsNullable<T>) -> JsValue
Source§impl From<JsValue> for JsNullable
impl From<JsValue> for JsNullable
Source§impl<T: JsGeneric> From<Option<Nullable<T>>> for JsNullable<T>
None is undefined, which JsNullable admits as its other empty state.
impl<T: JsGeneric> From<Option<Nullable<T>>> for JsNullable<T>
None is undefined, which JsNullable admits as its other empty state.
Source§impl<T> FromWasmAbi for JsNullable<T>
impl<T> FromWasmAbi for JsNullable<T>
Source§impl<T> IntoJsGeneric for JsNullable<T>where
JsNullable<T>: JsGeneric,
impl<T> IntoJsGeneric for JsNullable<T>where
JsNullable<T>: JsGeneric,
Source§type JsCanon = JsNullable<T>
type JsCanon = JsNullable<T>
JsGeneric form of this type.Source§fn to_js(self) -> JsNullable<T>
fn to_js(self) -> JsNullable<T>
JsGeneric value for self.Source§impl<T> IntoWasmAbi for JsNullable<T>
impl<T> IntoWasmAbi for JsNullable<T>
Source§impl<'__wbg_ref, T> IntoWasmAbi for &'__wbg_ref JsNullable<T>
impl<'__wbg_ref, T> IntoWasmAbi for &'__wbg_ref JsNullable<T>
Source§impl<T> JsCast for JsNullable<T>
impl<T> JsCast for JsNullable<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 JsNullable<T>
impl<T> LongRefFromWasmAbi for JsNullable<T>
Source§type Anchor = JsNullable<T>
type Anchor = JsNullable<T>
RefFromWasmAbi::AnchorSource§unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor
unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor
RefFromWasmAbi::ref_from_abiSource§impl<T> NullableFromWasmAbi for JsNullable<T>
impl<T> NullableFromWasmAbi for JsNullable<T>
Source§impl<T> NullableIntoWasmAbi for JsNullable<T>
impl<T> NullableIntoWasmAbi for JsNullable<T>
Source§impl<'__wbg_ref, T> NullableIntoWasmAbi for &'__wbg_ref JsNullable<T>
impl<'__wbg_ref, T> NullableIntoWasmAbi for &'__wbg_ref JsNullable<T>
Source§impl<T> OptionFromWasmAbi for JsNullable<T>
impl<T> OptionFromWasmAbi for JsNullable<T>
Source§impl<T> OptionIntoWasmAbi for JsNullable<T>
impl<T> OptionIntoWasmAbi for JsNullable<T>
Source§impl<'__wbg_ref, T> OptionIntoWasmAbi for &'__wbg_ref JsNullable<T>
impl<'__wbg_ref, T> OptionIntoWasmAbi for &'__wbg_ref JsNullable<T>
Source§impl<T> PartialEq for JsNullable<T>
impl<T> PartialEq for JsNullable<T>
Source§impl<T> Promising for JsNullable<T>
impl<T> Promising for JsNullable<T>
Source§type Resolution = JsNullable<T>
type Resolution = JsNullable<T>
Source§impl<T> RefFromWasmAbi for JsNullable<T>
impl<T> RefFromWasmAbi for JsNullable<T>
Source§type Abi = <JsValue as RefFromWasmAbi>::Abi
type Abi = <JsValue as RefFromWasmAbi>::Abi
Self are recovered from.Source§type Anchor = ManuallyDrop<JsNullable<T>>
type Anchor = ManuallyDrop<JsNullable<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 UpcastFrom<&str> for JsNullable<JsValue>
impl<T> UpcastFrom<()> for JsNullable<T>
impl<T1, T2, Target1, Target2> UpcastFrom<(T1, T2)> for JsNullable<(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 JsNullable<(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 JsNullable<(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 JsNullable<(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 JsNullable<(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 JsNullable<(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 JsNullable<(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, Target1> UpcastFrom<(T1,)> for JsNullable<(Target1,)>
impl<T, Target> UpcastFrom<*const T> for JsNullable<*const Target>where
Target: UpcastFrom<T>,
impl UpcastFrom<JsError> for JsNullable<JsValue>
impl<T> UpcastFrom<JsNullable<T>> for JsValue
impl<T, U> UpcastFrom<JsNullable<U>> for JsNullable<T>where
T: UpcastFrom<U>,
impl<T, U> UpcastFrom<JsOption<U>> for JsNullable<T>where
T: UpcastFrom<U>,
impl UpcastFrom<JsValue> for JsNullable<JsValue>
impl<T> UpcastFrom<Null> for JsNullable<T>
impl<T, Target> UpcastFrom<Nullable<T>> for JsNullable<Nullable<Target>>where
Target: UpcastFrom<T>,
impl<T, Target> UpcastFrom<Option<T>> for JsNullable<Option<Target>>where
Target: UpcastFrom<T>,
impl<T, E, TargetT, TargetE> UpcastFrom<Result<T, E>> for JsNullable<Result<TargetT, TargetE>>where
TargetT: UpcastFrom<T>,
TargetE: UpcastFrom<E>,
impl UpcastFrom<String> for JsNullable<JsValue>
impl<T> UpcastFrom<Undefined> for JsNullable<T>
impl UpcastFrom<bool> for JsNullable<JsValue>
impl UpcastFrom<char> for JsNullable<JsValue>
impl UpcastFrom<f32> for JsNullable<JsValue>
impl UpcastFrom<f32> for JsNullable<f64>
impl UpcastFrom<f64> for JsNullable<JsValue>
impl UpcastFrom<i8> for JsNullable<JsValue>
impl UpcastFrom<i8> for JsNullable<i16>
impl UpcastFrom<i8> for JsNullable<i32>
impl UpcastFrom<i8> for JsNullable<i64>
impl UpcastFrom<i8> for JsNullable<i128>
impl UpcastFrom<i16> for JsNullable<JsValue>
impl UpcastFrom<i16> for JsNullable<i32>
impl UpcastFrom<i16> for JsNullable<i64>
impl UpcastFrom<i16> for JsNullable<i128>
impl UpcastFrom<i32> for JsNullable<JsValue>
impl UpcastFrom<i32> for JsNullable<isize>
impl UpcastFrom<i32> for JsNullable<i64>
impl UpcastFrom<i32> for JsNullable<i128>
impl UpcastFrom<i64> for JsNullable<JsValue>
impl UpcastFrom<i64> for JsNullable<i128>
impl UpcastFrom<i128> for JsNullable<JsValue>
impl UpcastFrom<isize> for JsNullable<JsValue>
impl UpcastFrom<isize> for JsNullable<i64>
impl UpcastFrom<isize> for JsNullable<i128>
impl UpcastFrom<u8> for JsNullable<JsValue>
impl UpcastFrom<u8> for JsNullable<u16>
impl UpcastFrom<u8> for JsNullable<u32>
impl UpcastFrom<u8> for JsNullable<u64>
impl UpcastFrom<u8> for JsNullable<u128>
impl UpcastFrom<u16> for JsNullable<JsValue>
impl UpcastFrom<u16> for JsNullable<u32>
impl UpcastFrom<u16> for JsNullable<u64>
impl UpcastFrom<u16> for JsNullable<u128>
impl UpcastFrom<u32> for JsNullable<JsValue>
impl UpcastFrom<u32> for JsNullable<usize>
impl UpcastFrom<u32> for JsNullable<u64>
impl UpcastFrom<u32> for JsNullable<u128>
impl UpcastFrom<u64> for JsNullable<JsValue>
impl UpcastFrom<u64> for JsNullable<u128>
impl UpcastFrom<u128> for JsNullable<JsValue>
impl UpcastFrom<usize> for JsNullable<JsValue>
impl UpcastFrom<usize> for JsNullable<u64>
impl UpcastFrom<usize> for JsNullable<u128>
Source§impl<T> VectorRefIntoWasmAbi for JsNullable<T>
impl<T> VectorRefIntoWasmAbi for JsNullable<T>
fn slice_into_abi(slice: &[Self]) -> WasmSlice
Auto Trait Implementations§
impl<T> Freeze for JsNullable<T>
impl<T> RefUnwindSafe for JsNullable<T>
impl<T> Send for JsNullable<T>
impl<T> Sync for JsNullable<T>
impl<T> Unpin for JsNullable<T>
impl<T> UnsafeUnpin for JsNullable<T>
impl<T> UnwindSafe for JsNullable<T>
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,
impl<T> JsGeneric for T
Source§impl<T> MaybeFromWasmAbi for Twhere
T: OptionFromWasmAbi,
impl<T> MaybeFromWasmAbi for Twhere
T: OptionFromWasmAbi,
Source§type MaybeAbi = <T as FromWasmAbi>::Abi
type MaybeAbi = <T as FromWasmAbi>::Abi
Option<Self>.Source§unsafe fn from_maybe_abi(js: <T as FromWasmAbi>::Abi) -> T
unsafe fn from_maybe_abi(js: <T as FromWasmAbi>::Abi) -> T
unsafe fn try_from_maybe_abi( js: <T as FromWasmAbi>::Abi, ) -> Result<T, &'static str>
Source§impl<T> MaybeIntoWasmAbi for Twhere
T: OptionIntoWasmAbi,
impl<T> MaybeIntoWasmAbi for Twhere
T: OptionIntoWasmAbi,
Source§type MaybeAbi = <T as IntoWasmAbi>::Abi
type MaybeAbi = <T as IntoWasmAbi>::Abi
Option<Self>.Source§fn into_maybe_abi(self) -> <T as IntoWasmAbi>::Abi
fn into_maybe_abi(self) -> <T as IntoWasmAbi>::Abi
Option<Self> ABI.Source§impl<T, A> RefIntoWasmAbi for T
impl<T, A> RefIntoWasmAbi for T
Source§fn describe_ref()
fn describe_ref()
&Self.Source§fn ref_into_abi(&self) -> A
fn ref_into_abi(&self) -> A
&self into Self::Abi.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.Source§impl<T> TryFromJsValue for Twhere
T: JsCast,
impl<T> TryFromJsValue for Twhere
T: JsCast,
Source§fn try_from_js_value_ref(val: &JsValue) -> Option<T>
fn try_from_js_value_ref(val: &JsValue) -> Option<T>
Source§const NULLABLE: bool = false
const NULLABLE: bool = false
Self accepts null as a distinct value space, as
Nullable<T> does. An enclosing Option then passes null through
instead of reading it as None, so Option<T> is itself nullable
exactly when T is not. JsValue does not set this: it accepts
null as one value among all of them, not as a distinct space, and
Option<JsValue> reads it as None like the ABI does. This is the
JS-value side of NullableFromWasmAbi, which claims null at the
ABI.