Skip to main content

OptionFutureExt

Trait OptionFutureExt 

Source
pub trait OptionFutureExt<T> {
    // Required methods
    fn is_none_or(
        self,
        f: impl FnOnce(&T) -> bool + Send,
    ) -> impl Future<Output = bool> + Send;
    fn is_some_and(
        self,
        f: impl FnOnce(&T) -> bool + Send,
    ) -> impl Future<Output = bool> + Send;
    fn unwrap_or(self, t: T) -> impl Future<Output = T> + Send;
    fn unwrap_or_default(self) -> impl Future<Output = T> + Send
       where T: Default;
    fn unwrap_or_else(
        self,
        f: impl FnOnce() -> T + Send,
    ) -> impl Future<Output = T> + Send;
    fn unwrap_or_else_async<F: Future<Output = T> + Send>(
        self,
        f: impl FnOnce() -> F + Send,
    ) -> impl Future<Output = Option<T>> + Send;
}
Expand description

Adds option-like combinators to futures with optional output.

Each adapter transforms the eventual Option<T> without requiring an intermediate await. Lazy fallbacks run only for absent output.

Required Methods§

Source

fn is_none_or( self, f: impl FnOnce(&T) -> bool + Send, ) -> impl Future<Output = bool> + Send

Tests whether the future yields no value or one matching f.

An absent output returns true without invoking the predicate. A present value is borrowed for the predicate after the future resolves.

Source

fn is_some_and( self, f: impl FnOnce(&T) -> bool + Send, ) -> impl Future<Output = bool> + Send

Tests whether the future yields a value matching f.

An absent output returns false without invoking the predicate. A present value is borrowed for the predicate after the future resolves.

Source

fn unwrap_or(self, t: T) -> impl Future<Output = T> + Send

Returns the future’s value or the supplied fallback.

The fallback is supplied eagerly and used only for absent output. A present value passes through unchanged.

Source

fn unwrap_or_default(self) -> impl Future<Output = T> + Send
where T: Default,

Returns the future’s value or T::default().

The default is constructed only after the future yields None. A present value passes through unchanged.

Source

fn unwrap_or_else( self, f: impl FnOnce() -> T + Send, ) -> impl Future<Output = T> + Send

Returns the future’s value or lazily computes a fallback.

The closure is called only after the future yields None. A present value passes through without invoking it.

Source

fn unwrap_or_else_async<F: Future<Output = T> + Send>( self, f: impl FnOnce() -> F + Send, ) -> impl Future<Output = Option<T>> + Send

Runs an asynchronous fallback only when the future yields None.

Absent output becomes Some of the fallback future’s output. Present output suppresses the fallback and becomes None rather than being returned.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T, Fut> OptionFutureExt<T> for OptionFuture<Fut>
where Fut: Future<Output = T> + Send, T: Send,

Source§

fn is_none_or( self, f: impl FnOnce(&T) -> bool + Send, ) -> impl Future<Output = bool> + Send

Source§

fn is_some_and( self, f: impl FnOnce(&T) -> bool + Send, ) -> impl Future<Output = bool> + Send

Source§

fn unwrap_or(self, t: T) -> impl Future<Output = T> + Send

Source§

fn unwrap_or_default(self) -> impl Future<Output = T> + Send
where T: Default,

Source§

fn unwrap_or_else( self, f: impl FnOnce() -> T + Send, ) -> impl Future<Output = T> + Send

Source§

fn unwrap_or_else_async<F: Future<Output = T> + Send>( self, f: impl FnOnce() -> F + Send, ) -> impl Future<Output = Option<T>> + Send

Implementors§