pub enum JsOption<T> {
Some(T),
Null,
Undefined,
}
Expand description
An Option
-like type with two data-less variants in addition to Some
:
Null
and Undefined
.
Variants§
Implementations§
Source§impl<T> JsOption<T>
impl<T> JsOption<T>
Sourcepub fn from_option(opt: Option<T>) -> JsOption<T>
pub fn from_option(opt: Option<T>) -> JsOption<T>
Construct a JsOption
from a regular Option
.
None
will be converted to to Null
.
Sourcepub fn from_implicit_option(opt: Option<T>) -> JsOption<T>
pub fn from_implicit_option(opt: Option<T>) -> JsOption<T>
Construct a JsOption
from a regular Option
.
None
will be converted to Undefined
.
Sourcepub fn into_option(self) -> Option<T>
pub fn into_option(self) -> Option<T>
Convert a JsOption
to Option
.
Sourcepub fn into_nested_option(self) -> Option<Option<T>>
pub fn into_nested_option(self) -> Option<Option<T>>
Convert a JsOption<T>
to Option<Option<T>>
.
Null
is represented as Some(None)
while Undefined
is represented
as None
.
Sourcepub const fn is_undefined(&self) -> bool
pub const fn is_undefined(&self) -> bool
Returns true
if the JsOption
is Undefined
.
Sourcepub fn unwrap(self) -> T
pub fn unwrap(self) -> T
Returns the contained Some
value, consuming the self
value.
§Panics
Panics if the self value equals Null
or Undefined
.
Sourcepub fn unwrap_or(self, default: T) -> T
pub fn unwrap_or(self, default: T) -> T
Returns the contained Some
value or a provided default.
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 Some
value computes is from a closure.
Source§impl<T> JsOption<T>where
T: Default,
impl<T> JsOption<T>where
T: Default,
Sourcepub fn unwrap_or_default(self) -> T
pub fn unwrap_or_default(self) -> T
Returns the contained Some
value or a default.
Trait Implementations§
Source§impl<'de, T> Deserialize<'de> for JsOption<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for JsOption<T>where
T: Deserialize<'de>,
Source§fn deserialize<D>(
deserializer: D,
) -> Result<JsOption<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<JsOption<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Deserialize a JsOption
.
This implementation will never return Undefined
. You need to use
#[serde(default)]
to get Undefined
when the field is not present.
Source§impl<T> Ord for JsOption<T>where
T: Ord,
impl<T> Ord for JsOption<T>where
T: Ord,
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T> PartialOrd for JsOption<T>where
T: PartialOrd,
impl<T> PartialOrd for JsOption<T>where
T: PartialOrd,
Source§impl<T> Serialize for JsOption<T>where
T: Serialize,
impl<T> Serialize for JsOption<T>where
T: Serialize,
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Serialize a JsOption
.
Serialization will fail for JsOption::Undefined
. You need to use
#[skip_serializing_if = "JsOption::is_undefined"]
to stop the field
from being serialized altogether.
impl<T> Copy for JsOption<T>where
T: Copy,
impl<T> Eq for JsOption<T>where
T: Eq,
impl<T> StructuralPartialEq for JsOption<T>
Auto Trait Implementations§
impl<T> Freeze for JsOption<T>where
T: Freeze,
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> 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<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<T> CompatExt for T
impl<T> CompatExt for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T, UT> HandleAlloc<UT> for T
impl<T, UT> HandleAlloc<UT> for T
Source§fn consume_handle(handle: Handle) -> Arc<T>
fn consume_handle(handle: Handle) -> Arc<T>
Arc<>
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more