Skip to content

[Feature Request] add async_then_some #2965

@cathaysia

Description

@cathaysia

Can you add some function like this?

use std::future::Future;

pub trait AsyncThenSome {
    fn async_then_some<F>(self, future: F) -> impl Future<Output = Option<F::Output>> + Send
    where
        F: Future + Send,
        F::Output: Send;
}

impl AsyncThenSome for bool {
    async fn async_then_some<F>(self, future: F) -> Option<F::Output>
    where
        F: Future + Send,
        F::Output: Send,
    {
        if !self { None } else { Some(future.await) }
    }
}

pub trait AsyncMap<T> {
    fn async_map<F, R>(self, func: F) -> impl Future<Output = Option<R>>
    where
        F: AsyncFn(T) -> R;
}

impl<T> AsyncMap<T> for Option<T> {
    async fn async_map<F, R>(self, func: F) -> Option<R>
    where
        F: AsyncFn(T) -> R,
    {
        match self {
            None => None,
            Some(v) => Some(func(v).await),
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions