async_iter_ext/
option.rs

1pub trait AsyncOptionTools<T> {
2    #[allow(clippy::wrong_self_convention)]
3    fn is_some_and_async<F, Fut>(self, f: F) -> impl Future<Output = bool>
4    where
5        F: FnOnce(T) -> Fut,
6        Fut: Future<Output = bool>;
7
8    #[allow(clippy::wrong_self_convention)]
9    fn is_none_or_async<F, Fut>(self, f: F) -> impl Future<Output = bool>
10    where
11        F: FnOnce(T) -> Fut,
12        Fut: Future<Output = bool>;
13
14    fn map_async<B, F, Fut>(self, f: F) -> impl Future<Output = Option<B>>
15    where
16        F: FnOnce(T) -> Fut,
17        Fut: Future<Output = B>;
18}
19
20impl<T> AsyncOptionTools<T> for Option<T> {
21    async fn is_some_and_async<F, Fut>(self, f: F) -> bool
22    where
23        F: FnOnce(T) -> Fut,
24        Fut: Future<Output = bool>,
25    {
26        if let Some(x) = self {
27            f(x).await
28        } else {
29            false
30        }
31    }
32
33    async fn is_none_or_async<F, Fut>(self, f: F) -> bool
34    where
35        F: FnOnce(T) -> Fut,
36        Fut: Future<Output = bool>,
37    {
38        if let Some(x) = self { f(x).await } else { true }
39    }
40
41    async fn map_async<B, F, Fut>(self, f: F) -> Option<B>
42    where
43        F: FnOnce(T) -> Fut,
44        Fut: Future<Output = B>,
45    {
46        if let Some(x) = self {
47            Some(f(x).await)
48        } else {
49            None
50        }
51    }
52}