pub enum MaybeOption<T> {
Unset,
None,
Some(T),
}
Expand description
Like an Option but with an additional Unset variant, that’s why it’s maybe an option.
This type is useful to disambiguate between absent values and those set explicitly to none/null.
§Serde
Serializing requires to skip unset variants, and deserializing requires default:
#[serde(default, skip_serializing_if = "MaybeOption::is_unset")]
Variants§
Implementations§
Source§impl<T> MaybeOption<T>
impl<T> MaybeOption<T>
Sourcepub const fn as_ref(&self) -> MaybeOption<&T>
pub const fn as_ref(&self) -> MaybeOption<&T>
Converts from &MaybeOption<T>
to MaybeOption<&T>
.
§Examples
Calculates the length of an MaybeOption<String>
as an MaybeOption<usize>
without moving the String
. The map
method takes the self
argument by value,
consuming the original, so this technique uses as_ref
to first take an MaybeOption
to a
reference to the value inside the original.
let text: MaybeOption<String> = MaybeOption::Some("Hello, world!".to_string());
// First, cast `Option<String>` to `Option<&String>` with `as_ref`,
// then consume *that* with `map`, leaving `text` on the stack.
let text_length: MaybeOption<usize> = text.as_ref().map(|s| s.len());
println!("still can print text: {text:?}");
Sourcepub fn as_mut(&mut self) -> MaybeOption<&mut T>
pub fn as_mut(&mut self) -> MaybeOption<&mut T>
Converts from &mut MaybeOption<T>
to MaybeOption<&mut T>
.
§Examples
let mut x = MaybeOption::Some(2);
match x.as_mut() {
MaybeOption::Some(v) => *v = 42,
MaybeOption::None => {}
MaybeOption::Unset => {}
}
assert_eq!(x, MaybeOption::Some(42));
Sourcepub fn map<U, F>(self, f: F) -> MaybeOption<U>where
F: FnOnce(T) -> U,
pub fn map<U, F>(self, f: F) -> MaybeOption<U>where
F: FnOnce(T) -> U,
Maps a MaybeOption<T>
to MaybeOption<U>
by applying a function to a contained value (if Some
).
§Examples
Calculates the length of an MaybeOption<String>
as an
MaybeOption<usize>
, consuming the original:
let maybe_some_string = MaybeOption::Some(String::from("Hello, World!"));
// `Option::map` takes self *by value*, consuming `maybe_some_string`
let maybe_some_len = maybe_some_string.map(|s| s.len());
assert_eq!(maybe_some_len, MaybeOption::Some(13));
let x: MaybeOption<&str> = MaybeOption::None;
assert_eq!(x.map(|s| s.len()), MaybeOption::None);
let x: MaybeOption<&str> = MaybeOption::Unset;
assert_eq!(x.map(|s| s.len()), MaybeOption::Unset);
Sourcepub fn map_option<U, F: FnOnce(Option<T>) -> Option<U>>(
self,
f: F,
) -> MaybeOption<U>
pub fn map_option<U, F: FnOnce(Option<T>) -> Option<U>>( self, f: F, ) -> MaybeOption<U>
Maps a MaybeOption<T>
to MaybeOption<U>
by applying a function
to the contained nullable value
Sourcepub fn into_double_option(self) -> Option<Option<T>>
pub fn into_double_option(self) -> Option<Option<T>>
Converts this value into the double-option pattern
Source§impl<T, E> MaybeOption<Result<T, E>>
impl<T, E> MaybeOption<Result<T, E>>
Trait Implementations§
Source§impl<T: Clone> Clone for MaybeOption<T>
impl<T: Clone> Clone for MaybeOption<T>
Source§fn clone(&self) -> MaybeOption<T>
fn clone(&self) -> MaybeOption<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T: Debug> Debug for MaybeOption<T>
impl<T: Debug> Debug for MaybeOption<T>
Source§impl<T> Default for MaybeOption<T>
impl<T> Default for MaybeOption<T>
Source§fn default() -> MaybeOption<T>
fn default() -> MaybeOption<T>
Source§impl<'de, T> Deserialize<'de> for MaybeOption<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for MaybeOption<T>where
T: Deserialize<'de>,
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<T> From<MaybeOption<T>> for MaybeUndefined<T>
impl<T> From<MaybeOption<T>> for MaybeUndefined<T>
Source§fn from(value: MaybeOption<T>) -> Self
fn from(value: MaybeOption<T>) -> Self
Source§impl<T, U> From<MaybeOption<T>> for Option<Option<U>>where
U: From<T>,
impl<T, U> From<MaybeOption<T>> for Option<Option<U>>where
U: From<T>,
Source§fn from(value: MaybeOption<T>) -> Self
fn from(value: MaybeOption<T>) -> Self
Source§impl<T> From<MaybeUndefined<T>> for MaybeOption<T>
impl<T> From<MaybeUndefined<T>> for MaybeOption<T>
Source§fn from(value: MaybeUndefined<T>) -> Self
fn from(value: MaybeUndefined<T>) -> Self
Source§impl<T: Hash> Hash for MaybeOption<T>
impl<T: Hash> Hash for MaybeOption<T>
Source§impl<T: InputType> InputType for MaybeOption<T>
impl<T: InputType> InputType for MaybeOption<T>
Source§type RawValueType = <T as InputType>::RawValueType
type RawValueType = <T as InputType>::RawValueType
Source§fn qualified_type_name() -> String
fn qualified_type_name() -> String
Source§fn create_type_info(registry: &mut Registry) -> String
fn create_type_info(registry: &mut Registry) -> String
Source§fn parse(value: Option<Value>) -> InputValueResult<Self>
fn parse(value: Option<Value>) -> InputValueResult<Self>
Value
. None represents undefined.Source§fn as_raw_value(&self) -> Option<&Self::RawValueType>
fn as_raw_value(&self) -> Option<&Self::RawValueType>
Source§impl<T: PartialEq> PartialEq for MaybeOption<T>
impl<T: PartialEq> PartialEq for MaybeOption<T>
Source§impl<T> Serialize for MaybeOption<T>where
T: Serialize,
impl<T> Serialize for MaybeOption<T>where
T: Serialize,
impl<T: Copy> Copy for MaybeOption<T>
impl<T: Eq> Eq for MaybeOption<T>
impl<T> StructuralPartialEq for MaybeOption<T>
Auto Trait Implementations§
impl<T> Freeze for MaybeOption<T>where
T: Freeze,
impl<T> RefUnwindSafe for MaybeOption<T>where
T: RefUnwindSafe,
impl<T> Send for MaybeOption<T>where
T: Send,
impl<T> Sync for MaybeOption<T>where
T: Sync,
impl<T> Unpin for MaybeOption<T>where
T: Unpin,
impl<T> UnwindSafe for MaybeOption<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> 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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);