pub enum Nullable<T> {
Null,
Value(T),
}Expand description
A T or null at the JS boundary.
Nullable<T> is the Rust-side type for a JS value of type T | null, as
Option<T> is for T | undefined, and Option<Nullable<T>> is the union
T | null | undefined. All three are accepted in the same positions: as an
argument or return value of exported and imported functions, as a public
field of an exported struct, and as a closure argument or return value,
including the &/&mut reference forms Option<T> supports.
| Type | JS type | JS → Rust | Rust → JS |
|---|---|---|---|
Option<T> | T | undefined | undefined and null → None; anything else goes to T’s conversion | None → undefined |
Nullable<T> | T | null | null → Null; anything else, undefined included, goes to T’s conversion | Null → null |
Option<Nullable<T>> | T | null | undefined | undefined → None, null → Some(Null), else Some(Value(..)) | None → undefined, Some(Null) → null |
The Option layer of Option<Nullable<T>> treats only undefined as
None, since null has to reach the Nullable layer.
opt.and_then(Nullable::value) collapses the union to an
Option<T>. No other nesting is supported: Nullable<Nullable<T>>,
Nullable<Option<T>> and Option<Option<T>> do not cross the boundary.
Both may also name a slot of a js-sys Function<fn(..)> signature or a
Promise<T> resolution. Where a js-sys container’s type-erased methods
marshal the element itself, as Array::get and Map::get do, the
element must be a handle: use the JS-side JsOption<T> and
JsNullable<T> there, which stay JS values rather than converting.
Variants§
Implementations§
Source§impl<T> Nullable<T>
impl<T> Nullable<T>
Sourcepub fn as_mut(&mut self) -> Nullable<&mut T>
pub fn as_mut(&mut self) -> Nullable<&mut T>
Converts from &mut Nullable<T> to Nullable<&mut T>.
Sourcepub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Nullable<U>
pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Nullable<U>
Maps a Nullable<T> to Nullable<U> by applying a function to a
contained value, leaving Null as it is.
Sourcepub fn into_option(self) -> Option<T>
pub fn into_option(self) -> Option<T>
Returns the contained value as Some, or None for Null.
Sourcepub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T
pub fn unwrap_or_else<F: FnOnce() -> T>(self, f: F) -> T
Returns the contained value or computes it from f.
Sourcepub fn unwrap_or_default(self) -> Twhere
T: Default,
pub fn unwrap_or_default(self) -> Twhere
T: Default,
Returns the contained value or T::default().
Sourcepub fn is_value_and(self, f: impl FnOnce(T) -> bool) -> bool
pub fn is_value_and(self, f: impl FnOnce(T) -> bool) -> bool
Returns true if this is a Value and f holds for it.
Sourcepub fn is_null_or(self, f: impl FnOnce(T) -> bool) -> bool
pub fn is_null_or(self, f: impl FnOnce(T) -> bool) -> bool
Returns true if this is Null, or a Value for which f holds.
Sourcepub fn ok_or<E>(self, err: E) -> Result<T, E>
pub fn ok_or<E>(self, err: E) -> Result<T, E>
Returns the contained value as Ok, or Err(err) for Null.
Sourcepub fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E>
pub fn ok_or_else<E, F: FnOnce() -> E>(self, err: F) -> Result<T, E>
Returns the contained value as Ok, or Err(err()) for Null.
Sourcepub fn and_then<U, F: FnOnce(T) -> Nullable<U>>(self, f: F) -> Nullable<U>
pub fn and_then<U, F: FnOnce(T) -> Nullable<U>>(self, f: F) -> Nullable<U>
Returns Null for Null, otherwise calls f on the contained value.
Sourcepub fn filter<P: FnOnce(&T) -> bool>(self, predicate: P) -> Nullable<T>
pub fn filter<P: FnOnce(&T) -> bool>(self, predicate: P) -> Nullable<T>
Returns the contained value if predicate holds for it, otherwise
Null.
Source§impl<T: DerefMut> Nullable<T>
impl<T: DerefMut> Nullable<T>
Sourcepub fn as_deref_mut(&mut self) -> Nullable<&mut T::Target>
pub fn as_deref_mut(&mut self) -> Nullable<&mut T::Target>
Converts from Nullable<T> (or &mut Nullable<T>) to
Nullable<&mut T::Target>.
Trait Implementations§
impl<T: Copy> Copy for Nullable<T>
impl<T: Eq> Eq for Nullable<T>
Source§impl<T: ErasableGeneric> ErasableGeneric for Nullable<T>
impl<T: ErasableGeneric> ErasableGeneric for Nullable<T>
Source§impl<T: NullableFromWasmAbi> FromWasmAbi for Nullable<T>
impl<T: NullableFromWasmAbi> FromWasmAbi for Nullable<T>
Source§impl<T> IntoIterator for Nullable<T>
impl<T> IntoIterator for Nullable<T>
Source§impl<'a, T> IntoIterator for &'a Nullable<T>
impl<'a, T> IntoIterator for &'a Nullable<T>
Source§impl<'a, T> IntoIterator for &'a mut Nullable<T>
impl<'a, T> IntoIterator for &'a mut Nullable<T>
Source§impl<T: NullableIntoWasmAbi> IntoWasmAbi for Nullable<T>
impl<T: NullableIntoWasmAbi> IntoWasmAbi for Nullable<T>
Source§impl<'a, T: WasmDescribe> IntoWasmAbi for &'a Nullable<T>
impl<'a, T: WasmDescribe> IntoWasmAbi for &'a Nullable<T>
Source§impl<T: NullableFromWasmAbi> OptionFromWasmAbi for Nullable<T>
T’s undefined test may cover its null sentinel too, as a slice’s
does with the null pointer, so Null is ruled out first.
impl<T: NullableFromWasmAbi> OptionFromWasmAbi for Nullable<T>
T’s undefined test may cover its null sentinel too, as a slice’s
does with the null pointer, so Null is ruled out first.
Source§impl<T: NullableIntoWasmAbi> OptionIntoWasmAbi for Nullable<T>
Option<Nullable<T>> is None for T’s undefined ABI value.
impl<T: NullableIntoWasmAbi> OptionIntoWasmAbi for Nullable<T>
Option<Nullable<T>> is None for T’s undefined ABI value.
Source§impl<'a, T: WasmDescribe> OptionIntoWasmAbi for &'a Nullable<T>
impl<'a, T: WasmDescribe> OptionIntoWasmAbi for &'a Nullable<T>
Source§impl<T: Ord> Ord for Nullable<T>
impl<T: Ord> Ord for Nullable<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialOrd> PartialOrd for Nullable<T>
impl<T: PartialOrd> PartialOrd for Nullable<T>
Source§impl<T: Promising> Promising for Nullable<T>
impl<T: Promising> Promising for Nullable<T>
Source§type Resolution = Nullable<<T as Promising>::Resolution>
type Resolution = Nullable<<T as Promising>::Resolution>
impl<T: PartialEq> StructuralPartialEq for Nullable<T>
Source§impl<T: TryFromJsValue> TryFromJsValue for Nullable<T>
impl<T: TryFromJsValue> TryFromJsValue for Nullable<T>
Source§const NULLABLE: bool = true
const NULLABLE: bool = true
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.Source§fn try_from_js_value_ref(value: &JsValue) -> Option<Self>
fn try_from_js_value_ref(value: &JsValue) -> Option<Self>
impl<'a, Target: UpcastFrom<&'a str>> UpcastFrom<&'a str> for Nullable<Target>
impl<Target: UpcastFrom<JsValue>> UpcastFrom<JsValue> for Nullable<Target>
impl<T> UpcastFrom<Null> for Nullable<T>
impl<T, Target> UpcastFrom<Nullable<T>> for Nullable<Target>where
Target: UpcastFrom<T>,
impl<T, Target> UpcastFrom<Nullable<T>> for Option<Target>where
Target: UpcastFrom<T>,
impl<T, Target> UpcastFrom<Nullable<T>> for JsOption<Nullable<Target>>where
Target: UpcastFrom<T>,
impl<T, Target> UpcastFrom<Nullable<T>> for JsNullable<Nullable<Target>>where
Target: UpcastFrom<T>,
impl<T> UpcastFrom<Nullable<T>> for JsValuewhere
JsValue: UpcastFrom<T>,
impl<Target: UpcastFrom<String>> UpcastFrom<String> for Nullable<Target>
impl<Target: UpcastFrom<bool>> UpcastFrom<bool> for Nullable<Target>
impl<Target: UpcastFrom<char>> UpcastFrom<char> for Nullable<Target>
impl<Target: UpcastFrom<f32>> UpcastFrom<f32> for Nullable<Target>
impl<Target: UpcastFrom<f64>> UpcastFrom<f64> for Nullable<Target>
impl<Target: UpcastFrom<i8>> UpcastFrom<i8> for Nullable<Target>
impl<Target: UpcastFrom<i16>> UpcastFrom<i16> for Nullable<Target>
impl<Target: UpcastFrom<i32>> UpcastFrom<i32> for Nullable<Target>
impl<Target: UpcastFrom<i64>> UpcastFrom<i64> for Nullable<Target>
impl<Target: UpcastFrom<i128>> UpcastFrom<i128> for Nullable<Target>
impl<Target: UpcastFrom<isize>> UpcastFrom<isize> for Nullable<Target>
impl<Target: UpcastFrom<u8>> UpcastFrom<u8> for Nullable<Target>
impl<Target: UpcastFrom<u16>> UpcastFrom<u16> for Nullable<Target>
impl<Target: UpcastFrom<u32>> UpcastFrom<u32> for Nullable<Target>
impl<Target: UpcastFrom<u64>> UpcastFrom<u64> for Nullable<Target>
impl<Target: UpcastFrom<u128>> UpcastFrom<u128> for Nullable<Target>
impl<Target: UpcastFrom<usize>> UpcastFrom<usize> for Nullable<Target>
Auto Trait Implementations§
impl<T> Freeze for Nullable<T>where
T: Freeze,
impl<T> RefUnwindSafe for Nullable<T>where
T: RefUnwindSafe,
impl<T> Send for Nullable<T>where
T: Send,
impl<T> Sync for Nullable<T>where
T: Sync,
impl<T> Unpin for Nullable<T>where
T: Unpin,
impl<T> UnsafeUnpin for Nullable<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Nullable<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> 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.