Struct fallback::Fallback

source ·
pub struct Fallback<T> { /* private fields */ }
Expand description

Stores two Option, and provides functionality to fallback.

Basically, you provides a function returns Option, and Fallback handles the fallback.

let data = Some("hello");
let base_data = Some("123");
let fallback = Fallback::new(data, base_data);
let num = fallback.and_then(|s| s.parse::<i32>().ok());
assert_eq!(num, Some(123));

And you can map the Fallback:

let data = Some(123);
let base_data = Some(123456);
let fallback = Fallback::new(data, base_data);
let fallback = fallback.map(|i| i.to_string());
let s = fallback.and_then(|s| if s.len() > 3 { Some(s) } else { None });
assert_eq!(s, Some("123456".to_string()));

Implementations§

source§

impl<T> Fallback<T>

source

pub const fn new(data: Option<T>, base_data: Option<T>) -> Self

Creates a new Fallback.

source

pub const fn is_some(&self) -> bool

Returns false if both data and base_data are None.

source

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

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

source

pub fn and_then<V>(self, f: impl FnMut(T) -> Option<V>) -> Option<V>

Fallbacks the data or part of data.

source

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

Fallbacks the total data.

source

pub fn map<V>(self, f: impl FnMut(T) -> V) -> Fallback<V>

Maps to a new Fallback.

source

pub fn unzip(self) -> (Option<T>, Option<T>)

Exacts the data and base_data.

source§

impl<T> Fallback<Option<T>>

source

pub fn flatten(self) -> Fallback<T>

Converts from Fallback<Option<T>> to Fallback<T>.

source§

impl<T> Fallback<T>

source

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

Treats the empty container as None and fallbacks.

source§

impl<T: AsRef<str>> Fallback<T>

source

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

Treats the empty string as None and fallbacks.

source§

impl<T: FallbackSpec> Fallback<T>

source

pub fn spec(self) -> T::SpecType

Get the specialized fallback object.

Trait Implementations§

source§

impl<T> From<Fallback<T>> for Option<T>

source§

fn from(f: Fallback<T>) -> Self

Converts to this type from the input type.
source§

impl<T: IntoIterator> IntoIterator for Fallback<T>

§

type Item = Fallback<<T as IntoIterator>::Item>

The type of the elements being iterated over.
§

type IntoIter = FallbackIter<Fuse<<T as IntoIterator>::IntoIter>>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Fallback<T>
where T: RefUnwindSafe,

§

impl<T> Send for Fallback<T>
where T: Send,

§

impl<T> Sync for Fallback<T>
where T: Sync,

§

impl<T> Unpin for Fallback<T>
where T: Unpin,

§

impl<T> UnwindSafe for Fallback<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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.