Skip to main content

Nullable

Enum Nullable 

Source
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.

TypeJS typeJS → RustRust → JS
Option<T>T | undefinedundefined and null → None; anything else goes to T’s conversionNone → undefined
Nullable<T>T | nullnull → Null; anything else, undefined included, goes to T’s conversionNull → null
Option<Nullable<T>>T | null | undefinedundefined → 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§

§

Null

JS null.

§

Value(T)

A value.

Implementations§

Source§

impl<T> Nullable<T>

Source

pub const fn is_null(&self) -> bool

Returns true if this is Null.

Source

pub const fn is_value(&self) -> bool

Returns true if this is a Value.

Source

pub const fn as_ref(&self) -> Nullable<&T>

Converts from &Nullable<T> to Nullable<&T>.

Source

pub fn as_mut(&mut self) -> Nullable<&mut T>

Converts from &mut Nullable<T> to Nullable<&mut T>.

Source

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.

Source

pub fn value(self) -> Option<T>

Returns the contained value as Some, or None for Null.

Source

pub fn into_option(self) -> Option<T>

Returns the contained value as Some, or None for Null.

Source

pub fn unwrap(self) -> T

Returns the contained value.

§Panics

Panics if this is Null.

Source

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

Returns the contained value.

§Panics

Panics with msg if this is Null.

Source

pub fn unwrap_or(self, default: T) -> T

Returns the contained value or default.

Source

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

Returns the contained value or computes it from f.

Source

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

Returns the contained value or T::default().

Source

pub fn is_value_and(self, f: impl FnOnce(T) -> bool) -> bool

Returns true if this is a Value and f holds for it.

Source

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.

Source

pub fn ok_or<E>(self, err: E) -> Result<T, E>

Returns the contained value as Ok, or Err(err) for Null.

Source

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.

Source

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.

Source

pub fn filter<P: FnOnce(&T) -> bool>(self, predicate: P) -> Nullable<T>

Returns the contained value if predicate holds for it, otherwise Null.

Source

pub fn take(&mut self) -> Nullable<T>

Takes the value out, leaving Null in its place.

Source

pub fn replace(&mut self, value: T) -> Nullable<T>

Replaces the contents with Value(value), returning the old contents.

Source

pub fn iter(&self) -> IntoIter<&T> ⓘ

Returns an iterator over the contained value, if any.

Source

pub fn iter_mut(&mut self) -> IntoIter<&mut T> ⓘ

Returns a mutable iterator over the contained value, if any.

Source§

impl<T: Deref> Nullable<T>

Source

pub fn as_deref(&self) -> Nullable<&T::Target>

Converts from Nullable<T> (or &Nullable<T>) to Nullable<&T::Target>.

Source§

impl<T: DerefMut> Nullable<T>

Source

pub fn as_deref_mut(&mut self) -> Nullable<&mut T::Target>

Converts from Nullable<T> (or &mut Nullable<T>) to Nullable<&mut T::Target>.

Source§

impl<T: Copy> Nullable<&T>

Source

pub fn copied(self) -> Nullable<T>

Maps a Nullable<&T> to a Nullable<T> by copying the referent.

Source§

impl<T: Clone> Nullable<&T>

Source

pub fn cloned(self) -> Nullable<T>

Maps a Nullable<&T> to a Nullable<T> by cloning the referent.

Source§

impl<T: Copy> Nullable<&mut T>

Source

pub fn copied(self) -> Nullable<T>

Maps a Nullable<&mut T> to a Nullable<T> by copying the referent.

Source§

impl<T: Clone> Nullable<&mut T>

Source

pub fn cloned(self) -> Nullable<T>

Maps a Nullable<&mut T> to a Nullable<T> by cloning the referent.

Trait Implementations§

Source§

impl<T: Clone> Clone for Nullable<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Copy> Copy for Nullable<T>

Source§

impl<T: Debug> Debug for Nullable<T>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Nullable<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Eq> Eq for Nullable<T>

Source§

impl<T: ErasableGeneric> ErasableGeneric for Nullable<T>

Source§

type Repr = Nullable<<T as ErasableGeneric>::Repr>

The singular concrete type that all generic variants can be transmuted on. Read more
Source§

impl<T> From<Nullable<T>> for Option<T>

Source§

fn from(nullable: Nullable<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Into<JsValue>> From<Nullable<T>> for JsValue

Source§

fn from(nullable: Nullable<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: JsGeneric> From<Nullable<T>> for JsNullable<T>

Source§

fn from(nullable: Nullable<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<Option<T>> for Nullable<T>

Source§

fn from(option: Option<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<T> for Nullable<T>

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T: NullableFromWasmAbi> FromWasmAbi for Nullable<T>

Source§

type Abi = <T as MaybeFromWasmAbi>::MaybeAbi

The Wasm ABI type that this converts from when coming back out from the ABI boundary.
Source§

unsafe fn from_abi(js: T::MaybeAbi) -> Self

Recover a Self from Self::Abi. Read more
Source§

impl<T: Hash> Hash for Nullable<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> IntoIterator for Nullable<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a Nullable<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<&'a T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut Nullable<T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<&'a mut T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: NullableIntoWasmAbi> IntoWasmAbi for Nullable<T>

Source§

type Abi = <T as MaybeIntoWasmAbi>::MaybeAbi

The Wasm ABI type that this converts into when crossing the ABI boundary.
Source§

fn into_abi(self) -> T::MaybeAbi

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
Source§

impl<'a, T: WasmDescribe> IntoWasmAbi for &'a Nullable<T>

Source§

type Abi = <Nullable<&'a T> as IntoWasmAbi>::Abi

The Wasm ABI type that this converts into when crossing the ABI boundary.
Source§

fn into_abi(self) -> Self::Abi

Convert self into Self::Abi so that it can be sent across the wasm ABI boundary.
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.

Source§

fn is_none(js: &T::MaybeAbi) -> bool

Tests whether the argument is a “none” instance. If so it will be deserialized as None, and otherwise it will be passed to FromWasmAbi.
Source§

impl<T: NullableIntoWasmAbi> OptionIntoWasmAbi for Nullable<T>

Option<Nullable<T>> is None for T’s undefined ABI value.

Source§

fn none() -> T::MaybeAbi

Returns an ABI instance indicating “none”, which JS will interpret as the None branch of this option. Read more
Source§

impl<'a, T: WasmDescribe> OptionIntoWasmAbi for &'a Nullable<T>

Source§

fn none() -> Self::Abi

Returns an ABI instance indicating “none”, which JS will interpret as the None branch of this option. Read more
Source§

impl<T: Ord> Ord for Nullable<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl<T: PartialEq> PartialEq for Nullable<T>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: PartialOrd> PartialOrd for Nullable<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Promising> Promising for Nullable<T>

Source§

type Resolution = Nullable<<T as Promising>::Resolution>

The type that this value resolves to.
Source§

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

Source§

impl<T: TryFromJsValue> TryFromJsValue for Nullable<T>

Source§

const NULLABLE: bool = true

Whether 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>

Performs the conversion.
Source§

fn try_from_js_value(value: JsValue) -> Result<Self, JsValue>

Performs the conversion.
Source§

impl<'a, Target: UpcastFrom<&'a str>> UpcastFrom<&'a str> for Nullable<Target>

Source§

impl<Target: UpcastFrom<JsValue>> UpcastFrom<JsValue> for Nullable<Target>

Source§

impl<T> UpcastFrom<Null> for Nullable<T>

Source§

impl<T, Target> UpcastFrom<Nullable<T>> for Nullable<Target>
where Target: UpcastFrom<T>,

Source§

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

Source§

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

Source§

impl<T, Target> UpcastFrom<Nullable<T>> for JsNullable<Nullable<Target>>
where Target: UpcastFrom<T>,

Source§

impl<T> UpcastFrom<Nullable<T>> for JsValue
where JsValue: UpcastFrom<T>,

Source§

impl<Target: UpcastFrom<String>> UpcastFrom<String> for Nullable<Target>

Source§

impl<Target: UpcastFrom<bool>> UpcastFrom<bool> for Nullable<Target>

Source§

impl<Target: UpcastFrom<char>> UpcastFrom<char> for Nullable<Target>

Source§

impl<Target: UpcastFrom<f32>> UpcastFrom<f32> for Nullable<Target>

Source§

impl<Target: UpcastFrom<f64>> UpcastFrom<f64> for Nullable<Target>

Source§

impl<Target: UpcastFrom<i8>> UpcastFrom<i8> for Nullable<Target>

Source§

impl<Target: UpcastFrom<i16>> UpcastFrom<i16> for Nullable<Target>

Source§

impl<Target: UpcastFrom<i32>> UpcastFrom<i32> for Nullable<Target>

Source§

impl<Target: UpcastFrom<i64>> UpcastFrom<i64> for Nullable<Target>

Source§

impl<Target: UpcastFrom<i128>> UpcastFrom<i128> for Nullable<Target>

Source§

impl<Target: UpcastFrom<isize>> UpcastFrom<isize> for Nullable<Target>

Source§

impl<Target: UpcastFrom<u8>> UpcastFrom<u8> for Nullable<Target>

Source§

impl<Target: UpcastFrom<u16>> UpcastFrom<u16> for Nullable<Target>

Source§

impl<Target: UpcastFrom<u32>> UpcastFrom<u32> for Nullable<Target>

Source§

impl<Target: UpcastFrom<u64>> UpcastFrom<u64> for Nullable<Target>

Source§

impl<Target: UpcastFrom<u128>> UpcastFrom<u128> for Nullable<Target>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeFromWasmAbi for T

Source§

type MaybeAbi = <T as FromWasmAbi>::Abi

The ABI of Option<Self>.
Source§

unsafe fn from_maybe_abi(js: <T as FromWasmAbi>::Abi) -> T

Recovers a value from the Option<Self> ABI, once is_none has ruled None out. Read more
Source§

unsafe fn try_from_maybe_abi( js: <T as FromWasmAbi>::Abi, ) -> Result<T, &'static str>

Source§

fn is_none(js: &<T as FromWasmAbi>::Abi) -> bool

Tests the ABI value for None.
Source§

impl<T> MaybeIntoWasmAbi for T

Source§

type MaybeAbi = <T as IntoWasmAbi>::Abi

The ABI of Option<Self>.
Source§

fn into_maybe_abi(self) -> <T as IntoWasmAbi>::Abi

Converts a value into the Option<Self> ABI.
Source§

fn none() -> <T as IntoWasmAbi>::Abi

Returns the ABI value for None, which JS reads as undefined. Read more
Source§

impl<T, A> RefIntoWasmAbi for T
where A: WasmAbi, &'a T: for<'a> IntoWasmAbi<Abi = A>, T: ?Sized,

Source§

type Abi = A

The Wasm ABI type that &Self converts into.
Source§

fn describe_ref()

Describe &Self.
Source§

fn ref_into_abi(&self) -> A

Convert &self into Self::Abi.
Source§

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

Source§

type Abi = <T as IntoWasmAbi>::Abi

Same as IntoWasmAbi::Abi
Source§

fn return_abi(self) -> <T as ReturnWasmAbi>::Abi

Same as IntoWasmAbi::into_abi, except that it may throw and never return in the case of Err.
Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = <Self as ErasableGeneric>::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = <Self as ErasableGeneric>::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more