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>§
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>,
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§fn clone(&self) -> JsNullable<T>
fn clone(&self) -> JsNullable<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§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> 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> 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<'a, T> IntoWasmAbi for &'a JsNullable<T>
impl<'a, T> IntoWasmAbi for &'a 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> 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<'a, T> OptionIntoWasmAbi for &'a JsNullable<T>
impl<'a, T> OptionIntoWasmAbi for &'a JsNullable<T>
Source§impl<T: PartialEq> PartialEq for JsNullable<T>
impl<T: PartialEq> 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<T: PartialEq> StructuralPartialEq for JsNullable<T>
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<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<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>
Auto Trait Implementations§
impl<T> Freeze for JsNullable<T>
impl<T> RefUnwindSafe for JsNullable<T>where
T: RefUnwindSafe,
impl<T> Send for JsNullable<T>where
T: Send,
impl<T> Sync for JsNullable<T>where
T: Sync,
impl<T> Unpin for JsNullable<T>where
T: Unpin,
impl<T> UnsafeUnpin for JsNullable<T>
impl<T> UnwindSafe for JsNullable<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,
impl<T> JsGeneric for T
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§impl<S, T> Upcast<T> for S
impl<S, T> Upcast<T> for S
Source§fn upcast(&self) -> &T
fn upcast(&self) -> &T
Source§fn upcast_into(self) -> Twhere
Self: Sized + ErasableGeneric,
T: Sized + ErasableGeneric<Repr = <Self as ErasableGeneric>::Repr>,
fn upcast_into(self) -> Twhere
Self: Sized + ErasableGeneric,
T: Sized + ErasableGeneric<Repr = <Self as ErasableGeneric>::Repr>,
Source§impl<T> VectorFromWasmAbi for Twhere
T: ErasableGeneric<Repr = JsValue> + WasmDescribe,
impl<T> VectorFromWasmAbi for Twhere
T: ErasableGeneric<Repr = JsValue> + WasmDescribe,
Source§impl<T> VectorIntoWasmAbi for Twhere
T: ErasableGeneric<Repr = JsValue> + WasmDescribe,
impl<T> VectorIntoWasmAbi for Twhere
T: ErasableGeneric<Repr = JsValue> + WasmDescribe,
Source§impl<T> VectorRefIntoWasmAbi for T
impl<T> VectorRefIntoWasmAbi for T
Source§fn slice_into_abi(slice: &[T]) -> WasmSlice
fn slice_into_abi(slice: &[T]) -> WasmSlice
Some(slice). The returned
WasmSlice is either a borrow of the input slice (primitive
case) or a buffer JS owns and frees (handle-shaped case).Source§fn slice_none() -> WasmSlicewhere
Self: Sized,
fn slice_none() -> WasmSlicewhere
Self: Sized,
None (used by Option<&[T]>). A null
WasmSlice (ptr == 0) is the convention shared with every
other vector-like ABI in the crate.